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 
44 public:
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 
59  :address(nullptr), size(0) {
60  *this = src;
61  }
62 
64 
66  :address(src.address), size(src.size) {
67  src.address = nullptr;
68  src.size = 0;
69  }
70 
72  free(address);
73  }
74 
76 
78 
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
bool IsDefined() const noexcept
Does the object have a well-defined address? Check !IsNull() before calling this method.
socklen_t size_type
gcc_pure bool operator==(SocketAddress other) const noexcept
AllocatedSocketAddress & operator=(SocketAddress src)
An OO wrapper for struct sockaddr.
SocketAddress::size_type size_type
static gcc_const AllocatedSocketAddress Null() noexcept
int GetFamily() const noexcept
bool operator!=(SocketAddress &other) const noexcept
const struct sockaddr * GetAddress() const noexcept
AllocatedSocketAddress(SocketAddress src)
#define gcc_const
Definition: Compiler.h:109
AllocatedSocketAddress(AllocatedSocketAddress &&src)
bool IsNull() const noexcept
AllocatedSocketAddress & operator=(AllocatedSocketAddress &&src) noexcept
size_type GetSize() const noexcept
#define gcc_pure
Definition: Compiler.h:116