MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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) {
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 {
87 return (SocketAddress)*this == other;
88 }
89
90 bool operator!=(SocketAddress &other) const {
91 return !(*this == other);
92 }
93
94 gcc_const
95 static AllocatedSocketAddress Null() {
96 return AllocatedSocketAddress(nullptr, 0);
97 }
98
99 bool IsNull() const {
100 return address == nullptr;
101 }
102
103 size_type GetSize() const {
104 return size;
105 }
106
107 const struct sockaddr *GetAddress() const {
108 return address;
109 }
110
111 operator SocketAddress() const {
112 return SocketAddress(address, size);
113 }
114
115 operator const struct sockaddr *() const {
116 return address;
117 }
118
119 int GetFamily() const {
120 return address->sa_family;
121 }
122
127 bool IsDefined() const {
128 return GetFamily() != AF_UNSPEC;
129 }
130
131 void Clear() {
132 free(address);
133 address = nullptr;
134 size = 0;
135 }
136
137 #ifdef HAVE_UN
138
143 void SetLocal(const char *path);
144 #endif
145
146 private:
147 void SetSize(size_type new_size);
148 };
149
150 #endif
SocketAddress::size_type
socklen_t size_type
Definition: SocketAddress.hxx:51
AllocatedSocketAddress::operator=
AllocatedSocketAddress & operator=(SocketAddress src)
AllocatedSocketAddress::IsDefined
bool IsDefined() const
Does the object have a well-defined address? Check !IsNull() before calling this method.
Definition: AllocatedSocketAddress.hxx:127
AllocatedSocketAddress::operator!=
bool operator!=(SocketAddress &other) const
Definition: AllocatedSocketAddress.hxx:90
AllocatedSocketAddress::Clear
void Clear()
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::GetSize
size_type GetSize() const
Definition: AllocatedSocketAddress.hxx:103
AllocatedSocketAddress::operator=
AllocatedSocketAddress & operator=(AllocatedSocketAddress &&src)
Definition: AllocatedSocketAddress.hxx:79
AllocatedSocketAddress::AllocatedSocketAddress
AllocatedSocketAddress()
Definition: AllocatedSocketAddress.hxx:56
SocketAddress.hxx
Compiler.h
AllocatedSocketAddress::AllocatedSocketAddress
AllocatedSocketAddress(SocketAddress src)
Definition: AllocatedSocketAddress.hxx:58
gcc_const
#define gcc_const
Definition: Compiler.h:109
AllocatedSocketAddress::operator==
gcc_pure bool operator==(SocketAddress other) const
Definition: AllocatedSocketAddress.hxx:86
AllocatedSocketAddress
Definition: AllocatedSocketAddress.hxx:43
AllocatedSocketAddress::AllocatedSocketAddress
AllocatedSocketAddress(AllocatedSocketAddress &&src)
Definition: AllocatedSocketAddress.hxx:65
Features.hxx
AllocatedSocketAddress::GetFamily
int GetFamily() const
Definition: AllocatedSocketAddress.hxx:119
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
AllocatedSocketAddress::GetAddress
const struct sockaddr * GetAddress() const
Definition: AllocatedSocketAddress.hxx:107
AllocatedSocketAddress::IsNull
bool IsNull() const
Definition: AllocatedSocketAddress.hxx:99
AllocatedSocketAddress::Null
static gcc_const AllocatedSocketAddress Null()
Definition: AllocatedSocketAddress.hxx:95