MPD: net/AllocatedSocketAddress.hxx Source File

MPD 0.20.18

AllocatedSocketAddress.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 ALLOCATED_SOCKET_ADDRESS_HPP
31 #define ALLOCATED_SOCKET_ADDRESS_HPP
32
33 #include "SocketAddress.hxx"
34 #include "Features.hxx"
35 #include "Compiler.h"
36
37 #include <algorithm>
38
39 #include <stdlib.h>
40
41 struct sockaddr;
42
43 class AllocatedSocketAddress {
44 public:
45 typedef SocketAddress::size_type size_type;
46
47 private:
48 struct sockaddr *address;
49 size_type size;
50
51 AllocatedSocketAddress(struct sockaddr *_address,
52 size_type _size)
53 :address(_address), size(_size) {}
54
55 public:
56 AllocatedSocketAddress():address(nullptr), size(0) {}
57
58 explicit AllocatedSocketAddress(SocketAddress src)
59 :address(nullptr), size(0) {
60 *this = src;
61 }
62
63 AllocatedSocketAddress(const AllocatedSocketAddress &) = delete;
64
65 AllocatedSocketAddress(AllocatedSocketAddress &&src)
66 :address(src.address), size(src.size) {
67 src.address = nullptr;
68 src.size = 0;
69 }
70
71 ~AllocatedSocketAddress() {
72 free(address);
73 }
74
75 AllocatedSocketAddress &operator=(SocketAddress src);
76
77 AllocatedSocketAddress &operator=(const AllocatedSocketAddress &) = delete;
78
79 AllocatedSocketAddress &operator=(AllocatedSocketAddress &&src) noexcept {
80 std::swap(address, src.address);
81 std::swap(size, src.size);
82 return *this;
83 }
84
85 gcc_pure
86 bool operator==(SocketAddress other) const noexcept {
87 return (SocketAddress)*this == other;
88 }
89
90 bool operator!=(SocketAddress &other) const noexcept {
91 return !(*this == other);
92 }
93
94 gcc_const
95 static AllocatedSocketAddress Null() noexcept {
96 return AllocatedSocketAddress(nullptr, 0);
97 }
98
99 bool IsNull() const noexcept {
100 return address == nullptr;
101 }
102
103 size_type GetSize() const noexcept {
104 return size;
105 }
106
107 const struct sockaddr *GetAddress() const noexcept {
108 return address;
109 }
110
111 operator SocketAddress() const noexcept {
112 return SocketAddress(address, size);
113 }
114
115 operator const struct sockaddr *() const noexcept {
116 return address;
117 }
118
119 int GetFamily() const noexcept {
120 return address->sa_family;
121 }
122
127 bool IsDefined() const noexcept {
128 return GetFamily() != AF_UNSPEC;
129 }
130
131 void Clear() noexcept {
132 free(address);
133 address = nullptr;
134 size = 0;
135 }
136
137 #ifdef HAVE_UN
138
143 void SetLocal(const char *path) noexcept;
144 #endif
145
146 private:
147 void SetSize(size_type new_size) noexcept;
148 };
149
150 #endif
AllocatedSocketAddress::IsDefined
bool IsDefined() const noexcept
Does the object have a well-defined address? Check !IsNull() before calling this method.
Definition: AllocatedSocketAddress.hxx:127
SocketAddress::size_type
socklen_t size_type
Definition: SocketAddress.hxx:51
AllocatedSocketAddress::operator==
gcc_pure bool operator==(SocketAddress other) const noexcept
Definition: AllocatedSocketAddress.hxx:86
AllocatedSocketAddress::operator=
AllocatedSocketAddress & operator=(SocketAddress src)
AllocatedSocketAddress::Clear
void Clear() noexcept
Definition: AllocatedSocketAddress.hxx:131
SocketAddress
An OO wrapper for struct sockaddr.
Definition: SocketAddress.hxx:46
AllocatedSocketAddress::~AllocatedSocketAddress
~AllocatedSocketAddress()
Definition: AllocatedSocketAddress.hxx:71
AllocatedSocketAddress::size_type
SocketAddress::size_type size_type
Definition: AllocatedSocketAddress.hxx:45
AllocatedSocketAddress::Null
static gcc_const AllocatedSocketAddress Null() noexcept
Definition: AllocatedSocketAddress.hxx:95
AllocatedSocketAddress::GetFamily
int GetFamily() const noexcept
Definition: AllocatedSocketAddress.hxx:119
AllocatedSocketAddress::AllocatedSocketAddress
AllocatedSocketAddress()
Definition: AllocatedSocketAddress.hxx:56
AllocatedSocketAddress::operator!=
bool operator!=(SocketAddress &other) const noexcept
Definition: AllocatedSocketAddress.hxx:90
SocketAddress.hxx
Compiler.h
AllocatedSocketAddress::GetAddress
const struct sockaddr * GetAddress() const noexcept
Definition: AllocatedSocketAddress.hxx:107
AllocatedSocketAddress::AllocatedSocketAddress
AllocatedSocketAddress(SocketAddress src)
Definition: AllocatedSocketAddress.hxx:58
gcc_const
#define gcc_const
Definition: Compiler.h:109
AllocatedSocketAddress
Definition: AllocatedSocketAddress.hxx:43
AllocatedSocketAddress::AllocatedSocketAddress
AllocatedSocketAddress(AllocatedSocketAddress &&src)
Definition: AllocatedSocketAddress.hxx:65
Features.hxx
AllocatedSocketAddress::IsNull
bool IsNull() const noexcept
Definition: AllocatedSocketAddress.hxx:99
AllocatedSocketAddress::operator=
AllocatedSocketAddress & operator=(AllocatedSocketAddress &&src) noexcept
Definition: AllocatedSocketAddress.hxx:79
AllocatedSocketAddress::GetSize
size_type GetSize() const noexcept
Definition: AllocatedSocketAddress.hxx:103
gcc_pure
#define gcc_pure
Definition: Compiler.h:116