MPD  0.20.18
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 
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
socklen_t size_type
static constexpr SocketAddress Null()
An OO wrapper for struct sockaddr.
constexpr SocketAddress(const struct sockaddr *_address, size_type _size)
constexpr int GetFamily() const
constexpr size_type GetSize() const
const struct sockaddr * GetAddress() const
constexpr SocketAddress(std::nullptr_t)
constexpr bool IsNull() const
bool operator!=(const SocketAddress other) const noexcept
gcc_pure bool operator==(const SocketAddress other) const noexcept
#define gcc_pure
Definition: Compiler.h:116
bool IsDefined() const
Does the object have a well-defined address? Check !IsNull() before calling this method.
SocketAddress()=default