libBsdSockets
C++ Wrapper classes to the BSD Socket API
LowLevelAddress.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2013, Komodo Does Inc
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 
13 */
14 
15 #ifndef LOW_LEVEL_ADDRESS_H
16 #define LOW_LEVEL_ADDRESS_H
17 
18 /*
19  * Requires: #include <sys/socket.h>
20  */
21 
22 namespace BsdSockets {
23 
24  /** \class LowLevelAddress
25  *
26  * \brief Interface for the low-level address implementation.
27  *
28  * This class provides access to the low-level sockaddr and socklen_t necessary for
29  * low-level bsd socket implementations. This is also available to the user
30  * if they need it.
31  *
32  * This is an integral part of the Address class refactored to avoid requiring
33  * the definition of the low level types in Address.h.
34  */
36  public:
37  typedef std::shared_ptr<LowLevelAddress> Ptr;
38 
39  /** Virtual destructor to support derived classes */
40  virtual ~LowLevelAddress() { }
41 
42  public:
43  /** @return the low-level sockaddr the Address represents */
44  virtual const struct sockaddr& getSockAddr() const = 0;
45 
46  /** @return the low-level sockaddr the Address represents */
47  virtual struct sockaddr& getSockAddr() = 0;
48 
49  /** @return the length of the low-level the sockaddr the Address represents */
50  virtual socklen_t getSockAddrLen() const = 0;
51  };
52 
53  /** \class LowLevelAddressType
54  *
55  * \brief LowLevelAddress template for the various cast-to-sockaddr implementaitons.
56  *
57  * This is not required, but provides a convenient template to get the job done.
58  */
59  template <class Impl>
61  public:
62  typedef std::shared_ptr<LowLevelAddressType> Ptr;
63 
64  /** Virtual destructor to support derived classes */
65  virtual ~LowLevelAddressType() { }
66 
67  /** Create with a zeroed low-level data */
69  memset(&lowLevelAddress, 0, sizeof(lowLevelAddress));
70  }
71 
72  /** Create as a copy of theLowLevelAddress */
73  LowLevelAddressType(const Impl& theLowLevelAddress) {
74  memcpy(&lowLevelAddress, &theLowLevelAddress, sizeof(lowLevelAddress));
75  }
76 
77  /** Create as a copy of rhs */
79  memcpy(&lowLevelAddress, &rhs.lowLevelAddress, sizeof(lowLevelAddress));
80  }
81 
82  private:
84  LowLevelAddressType& operator=(const LowLevelAddressType& rhs) = delete;
85  LowLevelAddressType& operator=(LowLevelAddressType&& rhs) = delete;
86 
87  public:
88  virtual const struct sockaddr& getSockAddr() const {
89  // This is ugly, but it wouldn't let me reinterpret cast without pointers
90  // and I hate c-style casts...
91  return *reinterpret_cast<const struct sockaddr*>(&lowLevelAddress);
92  }
93 
94  virtual struct sockaddr& getSockAddr() {
95  // This is ugly, but it wouldn't let me reinterpret cast without pointers
96  // and I hate c-style casts...
97  return *reinterpret_cast<struct sockaddr*>(&lowLevelAddress);
98  }
99 
100  virtual socklen_t getSockAddrLen() const {
101  return sizeof(lowLevelAddress);
102  }
103 
104  public:
105  /** The low-level address */
107  };
108 
109 } // namespace BsdSockets
110 
111 #endif // LOW_LEVEL_ADDRESS_H
Interface for the low-level address implementation.
virtual struct sockaddr & getSockAddr()=0
std::shared_ptr< LowLevelAddress > Ptr
virtual socklen_t getSockAddrLen() const =0
virtual const struct sockaddr & getSockAddr() const =0
LowLevelAddress template for the various cast-to-sockaddr implementaitons.
virtual socklen_t getSockAddrLen() const
virtual struct sockaddr & getSockAddr()
std::shared_ptr< LowLevelAddressType > Ptr
LowLevelAddressType(const LowLevelAddressType &rhs)
LowLevelAddressType(const Impl &theLowLevelAddress)
virtual const struct sockaddr & getSockAddr() const
Namespace of the BsdSockets library.
Definition: Address.cpp:20