MPD  0.20.6
StaticSocketAddress.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 Max Kellermann <max.kellermann@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef STATIC_SOCKET_ADDRESS_HXX
31 #define STATIC_SOCKET_ADDRESS_HXX
32 
33 #include "SocketAddress.hxx"
34 #include "Compiler.h"
35 
36 #include <assert.h>
37 
42 public:
44 
45 private:
46  size_type size;
47  struct sockaddr_storage address;
48 
49 public:
50  StaticSocketAddress() = default;
51 
53 
54  operator SocketAddress() const {
55  return SocketAddress(reinterpret_cast<const struct sockaddr *>(&address),
56  size);
57  }
58 
59  struct sockaddr *GetAddress() {
60  return reinterpret_cast<struct sockaddr *>(&address);
61  }
62 
63  const struct sockaddr *GetAddress() const {
64  return reinterpret_cast<const struct sockaddr *>(&address);
65  }
66 
67  constexpr size_type GetCapacity() const {
68  return sizeof(address);
69  }
70 
71  size_type GetSize() const {
72  return size;
73  }
74 
75  void SetSize(size_type _size) {
76  assert(_size > 0);
77  assert(size_t(_size) <= sizeof(address));
78 
79  size = _size;
80  }
81 
82  int GetFamily() const {
83  return address.ss_family;
84  }
85 
86  bool IsDefined() const {
87  return GetFamily() != AF_UNSPEC;
88  }
89 
90  void Clear() {
91  address.ss_family = AF_UNSPEC;
92  }
93 
94  gcc_pure
95  bool operator==(SocketAddress other) const {
96  return (SocketAddress)*this == other;
97  }
98 
99  bool operator!=(SocketAddress &other) const {
100  return !(*this == other);
101  }
102 };
103 
104 #endif
socklen_t size_type
An OO wrapper for struct sockaddr.
bool operator!=(SocketAddress &other) const
An OO wrapper for struct sockaddr_storage.
SocketAddress::size_type size_type
constexpr size_type GetCapacity() const
struct sockaddr * GetAddress()
void SetSize(size_type _size)
StaticSocketAddress()=default
const struct sockaddr * GetAddress() const
gcc_pure bool operator==(SocketAddress other) const
StaticSocketAddress & operator=(SocketAddress other)
#define gcc_pure
Definition: Compiler.h:116
size_type GetSize() const