Clementine
address.hpp
1 //
2 // ip/address.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_IP_ADDRESS_HPP
12 #define ASIO_IP_ADDRESS_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include <string>
20 #include "asio/detail/throw_exception.hpp"
21 #include "asio/detail/string_view.hpp"
22 #include "asio/detail/type_traits.hpp"
23 #include "asio/error_code.hpp"
24 #include "asio/ip/address_v4.hpp"
25 #include "asio/ip/address_v6.hpp"
26 #include "asio/ip/bad_address_cast.hpp"
27 
28 #if !defined(ASIO_NO_IOSTREAM)
29 # include <iosfwd>
30 #endif // !defined(ASIO_NO_IOSTREAM)
31 
32 #include "asio/detail/push_options.hpp"
33 
34 namespace asio {
35 namespace ip {
36 
38 
46 class address
47 {
48 public:
50  ASIO_DECL address() ASIO_NOEXCEPT;
51 
53  ASIO_DECL address(
54  const asio::ip::address_v4& ipv4_address) ASIO_NOEXCEPT;
55 
57  ASIO_DECL address(
58  const asio::ip::address_v6& ipv6_address) ASIO_NOEXCEPT;
59 
61  ASIO_DECL address(const address& other) ASIO_NOEXCEPT;
62 
63 #if defined(ASIO_HAS_MOVE)
64  ASIO_DECL address(address&& other) ASIO_NOEXCEPT;
66 #endif // defined(ASIO_HAS_MOVE)
67 
69  ASIO_DECL address& operator=(const address& other) ASIO_NOEXCEPT;
70 
71 #if defined(ASIO_HAS_MOVE)
72  ASIO_DECL address& operator=(address&& other) ASIO_NOEXCEPT;
74 #endif // defined(ASIO_HAS_MOVE)
75 
77  ASIO_DECL address& operator=(
78  const asio::ip::address_v4& ipv4_address) ASIO_NOEXCEPT;
79 
81  ASIO_DECL address& operator=(
82  const asio::ip::address_v6& ipv6_address) ASIO_NOEXCEPT;
83 
85  bool is_v4() const ASIO_NOEXCEPT
86  {
87  return type_ == ipv4;
88  }
89 
91  bool is_v6() const ASIO_NOEXCEPT
92  {
93  return type_ == ipv6;
94  }
95 
97  ASIO_DECL asio::ip::address_v4 to_v4() const;
98 
100  ASIO_DECL asio::ip::address_v6 to_v6() const;
101 
103  ASIO_DECL std::string to_string() const;
104 
105 #if !defined(ASIO_NO_DEPRECATED)
106  ASIO_DECL std::string to_string(asio::error_code& ec) const;
108 
112  static address from_string(const char* str);
113 
117  static address from_string(const char* str, asio::error_code& ec);
118 
122  static address from_string(const std::string& str);
123 
127  static address from_string(
128  const std::string& str, asio::error_code& ec);
129 #endif // !defined(ASIO_NO_DEPRECATED)
130 
132  ASIO_DECL bool is_loopback() const ASIO_NOEXCEPT;
133 
135  ASIO_DECL bool is_unspecified() const ASIO_NOEXCEPT;
136 
138  ASIO_DECL bool is_multicast() const ASIO_NOEXCEPT;
139 
141  ASIO_DECL friend bool operator==(const address& a1,
142  const address& a2) ASIO_NOEXCEPT;
143 
145  friend bool operator!=(const address& a1,
146  const address& a2) ASIO_NOEXCEPT
147  {
148  return !(a1 == a2);
149  }
150 
152  ASIO_DECL friend bool operator<(const address& a1,
153  const address& a2) ASIO_NOEXCEPT;
154 
156  friend bool operator>(const address& a1,
157  const address& a2) ASIO_NOEXCEPT
158  {
159  return a2 < a1;
160  }
161 
163  friend bool operator<=(const address& a1,
164  const address& a2) ASIO_NOEXCEPT
165  {
166  return !(a2 < a1);
167  }
168 
170  friend bool operator>=(const address& a1,
171  const address& a2) ASIO_NOEXCEPT
172  {
173  return !(a1 < a2);
174  }
175 
176 private:
177  // The type of the address.
178  enum { ipv4, ipv6 } type_;
179 
180  // The underlying IPv4 address.
181  asio::ip::address_v4 ipv4_address_;
182 
183  // The underlying IPv6 address.
184  asio::ip::address_v6 ipv6_address_;
185 };
186 
189 
192 ASIO_DECL address make_address(const char* str);
193 
196 
199 ASIO_DECL address make_address(const char* str,
200  asio::error_code& ec) ASIO_NOEXCEPT;
201 
204 
207 ASIO_DECL address make_address(const std::string& str);
208 
211 
214 ASIO_DECL address make_address(const std::string& str,
215  asio::error_code& ec) ASIO_NOEXCEPT;
216 
217 #if defined(ASIO_HAS_STRING_VIEW) \
218  || defined(GENERATING_DOCUMENTATION)
219 
222 
225 ASIO_DECL address make_address(string_view str);
226 
229 
232 ASIO_DECL address make_address(string_view str,
233  asio::error_code& ec) ASIO_NOEXCEPT;
234 
235 #endif // defined(ASIO_HAS_STRING_VIEW)
236  // || defined(GENERATING_DOCUMENTATION)
237 
238 #if !defined(ASIO_NO_IOSTREAM)
239 
241 
252 template <typename Elem, typename Traits>
253 std::basic_ostream<Elem, Traits>& operator<<(
254  std::basic_ostream<Elem, Traits>& os, const address& addr);
255 
256 #endif // !defined(ASIO_NO_IOSTREAM)
257 
258 } // namespace ip
259 } // namespace asio
260 
261 #include "asio/detail/pop_options.hpp"
262 
263 #include "asio/ip/impl/address.hpp"
264 #if defined(ASIO_HEADER_ONLY)
265 # include "asio/ip/impl/address.ipp"
266 #endif // defined(ASIO_HEADER_ONLY)
267 
268 #endif // ASIO_IP_ADDRESS_HPP
ASIO_DECL std::string to_string() const
Get the address as a string.
Definition: address.ipp:177
ASIO_DECL bool is_unspecified() const ASIO_NOEXCEPT
Determine whether the address is unspecified.
Definition: address.ipp:200
friend bool operator>(const address &a1, const address &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address.hpp:156
bool is_v4() const ASIO_NOEXCEPT
Get whether the address is an IP version 4 address.
Definition: address.hpp:85
std::basic_ostream< Elem, Traits > & operator<<(std::basic_ostream< Elem, Traits > &os, const address &addr)
Output an address as a string.
Definition: address.hpp:54
ASIO_DECL asio::ip::address_v6 to_v6() const
Get the address as an IP version 6 address.
Definition: address.ipp:167
Implements IP version 4 style addresses.
Definition: address_v4.hpp:45
static address from_string(const char *str)
(Deprecated: Use make_address().) Create an address from an IPv4 address string in dotted decimal for...
Definition: address.hpp:29
ASIO_DECL bool is_multicast() const ASIO_NOEXCEPT
Determine whether the address is a multicast address.
Definition: address.ipp:207
ASIO_DECL address & operator=(const address &other) ASIO_NOEXCEPT
Assign from another address.
Definition: address.ipp:71
friend bool operator!=(const address &a1, const address &a2) ASIO_NOEXCEPT
Compare two addresses for inequality.
Definition: address.hpp:145
ASIO_DECL address() ASIO_NOEXCEPT
Default constructor.
Definition: address.ipp:32
Implements version-independent IP addresses.
Definition: address.hpp:46
friend bool operator>=(const address &a1, const address &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address.hpp:170
Class to represent an error code value.
Definition: error_code.hpp:80
Implements IP version 6 style addresses.
Definition: address_v6.hpp:47
ASIO_DECL friend bool operator<(const address &a1, const address &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address.ipp:223
ASIO_DECL address make_address(const char *str)
Create an address from an IPv4 address string in dotted decimal form, or from an IPv6 address in hexa...
Definition: address.ipp:107
ASIO_DECL asio::ip::address_v4 to_v4() const
Get the address as an IP version 4 address.
Definition: address.ipp:157
ASIO_DECL bool is_loopback() const ASIO_NOEXCEPT
Determine whether the address is a loopback address.
Definition: address.ipp:193
bool is_v6() const ASIO_NOEXCEPT
Get whether the address is an IP version 6 address.
Definition: address.hpp:91
ASIO_DECL friend bool operator==(const address &a1, const address &a2) ASIO_NOEXCEPT
Compare two addresses for equality.
Definition: address.ipp:214
Definition: any_io_executor.hpp:28
friend bool operator<=(const address &a1, const address &a2) ASIO_NOEXCEPT
Compare addresses for ordering.
Definition: address.hpp:163