MPD: net/SocketAddress.hxx Source File

MPD 0.20.15

SocketAddress.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 SOCKET_ADDRESS_HXX
31 #define SOCKET_ADDRESS_HXX
32
33 #include "Compiler.h"
34
35 #include <cstddef>
36
37 #ifdef _WIN32
38 #include <winsock2.h>
39 #else
40 #include <sys/socket.h>
41 #endif
42
46 class SocketAddress {
47 public:
48 #ifdef _WIN32
49 typedef int size_type;
50 #else
51 typedef socklen_t size_type;
52 #endif
53
54 private:
55 const struct sockaddr *address;
56 size_type size;
57
58 public:
59 SocketAddress() = default;
60
61 constexpr SocketAddress(std::nullptr_t):address(nullptr), size(0) {}
62
63 constexpr SocketAddress(const struct sockaddr *_address,
64 size_type _size)
65 :address(_address), size(_size) {}
66
67 static constexpr SocketAddress Null() {
68 return nullptr;
69 }
70
71 constexpr bool IsNull() const {
72 return address == nullptr;
73 }
74
75 const struct sockaddr *GetAddress() const {
76 return address;
77 }
78
79 constexpr size_type GetSize() const {
80 return size;
81 }
82
83 constexpr int GetFamily() const {
84 return address->sa_family;
85 }
86
91 bool IsDefined() const {
92 return GetFamily() != AF_UNSPEC;
93 }
94
95 gcc_pure
96 bool operator==(const SocketAddress other) const noexcept;
97
98 bool operator!=(const SocketAddress other) const noexcept {
99 return !(*this == other);
100 }
101 };
102
103 #endif
SocketAddress::size_type
socklen_t size_type
Definition: SocketAddress.hxx:51
SocketAddress::Null
static constexpr SocketAddress Null()
Definition: SocketAddress.hxx:67
SocketAddress
An OO wrapper for struct sockaddr.
Definition: SocketAddress.hxx:46
SocketAddress::SocketAddress
constexpr SocketAddress(const struct sockaddr *_address, size_type _size)
Definition: SocketAddress.hxx:63
SocketAddress::GetFamily
constexpr int GetFamily() const
Definition: SocketAddress.hxx:83
SocketAddress::GetSize
constexpr size_type GetSize() const
Definition: SocketAddress.hxx:79
Compiler.h
SocketAddress::GetAddress
const struct sockaddr * GetAddress() const
Definition: SocketAddress.hxx:75
SocketAddress::SocketAddress
constexpr SocketAddress(std::nullptr_t)
Definition: SocketAddress.hxx:61
SocketAddress::IsNull
constexpr bool IsNull() const
Definition: SocketAddress.hxx:71
SocketAddress::operator!=
bool operator!=(const SocketAddress other) const noexcept
Definition: SocketAddress.hxx:98
SocketAddress::operator==
gcc_pure bool operator==(const SocketAddress other) const noexcept
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
SocketAddress::IsDefined
bool IsDefined() const
Does the object have a well-defined address? Check !IsNull() before calling this method.
Definition: SocketAddress.hxx:91
SocketAddress::SocketAddress
SocketAddress()=default