MPD: net/StaticSocketAddress.hxx Source File

MPD 0.20.15

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
41 class StaticSocketAddress {
42 public:
43 typedef SocketAddress::size_type size_type;
44
45 private:
46 size_type size;
47 struct sockaddr_storage address;
48
49 public:
50 StaticSocketAddress() = default;
51
52 StaticSocketAddress &operator=(SocketAddress other) noexcept;
53
54 operator SocketAddress() const noexcept {
55 return SocketAddress(reinterpret_cast<const struct sockaddr *>(&address),
56 size);
57 }
58
59 struct sockaddr *GetAddress() noexcept {
60 return reinterpret_cast<struct sockaddr *>(&address);
61 }
62
63 const struct sockaddr *GetAddress() const noexcept {
64 return reinterpret_cast<const struct sockaddr *>(&address);
65 }
66
67 constexpr size_type GetCapacity() const noexcept {
68 return sizeof(address);
69 }
70
71 size_type GetSize() const noexcept {
72 return size;
73 }
74
75 void SetSize(size_type _size) noexcept {
76 assert(_size > 0);
77 assert(size_t(_size) <= sizeof(address));
78
79 size = _size;
80 }
81
82 int GetFamily() const noexcept {
83 return address.ss_family;
84 }
85
86 bool IsDefined() const noexcept {
87 return GetFamily() != AF_UNSPEC;
88 }
89
90 void Clear() noexcept {
91 address.ss_family = AF_UNSPEC;
92 }
93
94 gcc_pure
95 bool operator==(SocketAddress other) const noexcept {
96 return (SocketAddress)*this == other;
97 }
98
99 bool operator!=(SocketAddress &other) const noexcept {
100 return !(*this == other);
101 }
102 };
103
104 #endif
SocketAddress::size_type
socklen_t size_type
Definition: SocketAddress.hxx:51
StaticSocketAddress::GetAddress
const struct sockaddr * GetAddress() const noexcept
Definition: StaticSocketAddress.hxx:63
StaticSocketAddress::GetSize
size_type GetSize() const noexcept
Definition: StaticSocketAddress.hxx:71
SocketAddress
An OO wrapper for struct sockaddr.
Definition: SocketAddress.hxx:46
StaticSocketAddress::Clear
void Clear() noexcept
Definition: StaticSocketAddress.hxx:90
SocketAddress.hxx
Compiler.h
StaticSocketAddress
An OO wrapper for struct sockaddr_storage.
Definition: StaticSocketAddress.hxx:41
StaticSocketAddress::size_type
SocketAddress::size_type size_type
Definition: StaticSocketAddress.hxx:43
StaticSocketAddress::operator==
gcc_pure bool operator==(SocketAddress other) const noexcept
Definition: StaticSocketAddress.hxx:95
StaticSocketAddress::SetSize
void SetSize(size_type _size) noexcept
Definition: StaticSocketAddress.hxx:75
StaticSocketAddress::operator!=
bool operator!=(SocketAddress &other) const noexcept
Definition: StaticSocketAddress.hxx:99
StaticSocketAddress::operator=
StaticSocketAddress & operator=(SocketAddress other) noexcept
StaticSocketAddress::StaticSocketAddress
StaticSocketAddress()=default
StaticSocketAddress::IsDefined
bool IsDefined() const noexcept
Definition: StaticSocketAddress.hxx:86
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
StaticSocketAddress::GetFamily
int GetFamily() const noexcept
Definition: StaticSocketAddress.hxx:82
StaticSocketAddress::GetCapacity
constexpr size_type GetCapacity() const noexcept
Definition: StaticSocketAddress.hxx:67
StaticSocketAddress::GetAddress
struct sockaddr * GetAddress() noexcept
Definition: StaticSocketAddress.hxx:59