]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/ip/impl/address.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / ip / impl / address.ipp
1 //
2 // ip/impl/address.ipp
3 // ~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 BOOST_ASIO_IP_IMPL_ADDRESS_IPP
12 #define BOOST_ASIO_IP_IMPL_ADDRESS_IPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <typeinfo>
20 #include <boost/asio/detail/throw_error.hpp>
21 #include <boost/asio/detail/throw_exception.hpp>
22 #include <boost/asio/error.hpp>
23 #include <boost/asio/ip/address.hpp>
24 #include <boost/asio/ip/bad_address_cast.hpp>
25 #include <boost/system/system_error.hpp>
26
27 #include <boost/asio/detail/push_options.hpp>
28
29 namespace boost {
30 namespace asio {
31 namespace ip {
32
33 address::address()
34 : type_(ipv4),
35 ipv4_address_(),
36 ipv6_address_()
37 {
38 }
39
40 address::address(const boost::asio::ip::address_v4& ipv4_address)
41 : type_(ipv4),
42 ipv4_address_(ipv4_address),
43 ipv6_address_()
44 {
45 }
46
47 address::address(const boost::asio::ip::address_v6& ipv6_address)
48 : type_(ipv6),
49 ipv4_address_(),
50 ipv6_address_(ipv6_address)
51 {
52 }
53
54 address::address(const address& other)
55 : type_(other.type_),
56 ipv4_address_(other.ipv4_address_),
57 ipv6_address_(other.ipv6_address_)
58 {
59 }
60
61 #if defined(BOOST_ASIO_HAS_MOVE)
62 address::address(address&& other)
63 : type_(other.type_),
64 ipv4_address_(other.ipv4_address_),
65 ipv6_address_(other.ipv6_address_)
66 {
67 }
68 #endif // defined(BOOST_ASIO_HAS_MOVE)
69
70 address& address::operator=(const address& other)
71 {
72 type_ = other.type_;
73 ipv4_address_ = other.ipv4_address_;
74 ipv6_address_ = other.ipv6_address_;
75 return *this;
76 }
77
78 #if defined(BOOST_ASIO_HAS_MOVE)
79 address& address::operator=(address&& other)
80 {
81 type_ = other.type_;
82 ipv4_address_ = other.ipv4_address_;
83 ipv6_address_ = other.ipv6_address_;
84 return *this;
85 }
86 #endif // defined(BOOST_ASIO_HAS_MOVE)
87
88 address& address::operator=(const boost::asio::ip::address_v4& ipv4_address)
89 {
90 type_ = ipv4;
91 ipv4_address_ = ipv4_address;
92 ipv6_address_ = boost::asio::ip::address_v6();
93 return *this;
94 }
95
96 address& address::operator=(const boost::asio::ip::address_v6& ipv6_address)
97 {
98 type_ = ipv6;
99 ipv4_address_ = boost::asio::ip::address_v4();
100 ipv6_address_ = ipv6_address;
101 return *this;
102 }
103
104 address make_address(const char* str)
105 {
106 boost::system::error_code ec;
107 address addr = make_address(str, ec);
108 boost::asio::detail::throw_error(ec);
109 return addr;
110 }
111
112 address make_address(const char* str, boost::system::error_code& ec)
113 {
114 boost::asio::ip::address_v6 ipv6_address =
115 boost::asio::ip::make_address_v6(str, ec);
116 if (!ec)
117 return address(ipv6_address);
118
119 boost::asio::ip::address_v4 ipv4_address =
120 boost::asio::ip::make_address_v4(str, ec);
121 if (!ec)
122 return address(ipv4_address);
123
124 return address();
125 }
126
127 address make_address(const std::string& str)
128 {
129 return make_address(str.c_str());
130 }
131
132 address make_address(const std::string& str,
133 boost::system::error_code& ec)
134 {
135 return make_address(str.c_str(), ec);
136 }
137
138 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
139
140 address make_address(string_view str)
141 {
142 return make_address(static_cast<std::string>(str));
143 }
144
145 address make_address(string_view str,
146 boost::system::error_code& ec)
147 {
148 return make_address(static_cast<std::string>(str), ec);
149 }
150
151 #endif // defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
152
153 boost::asio::ip::address_v4 address::to_v4() const
154 {
155 if (type_ != ipv4)
156 {
157 bad_address_cast ex;
158 boost::asio::detail::throw_exception(ex);
159 }
160 return ipv4_address_;
161 }
162
163 boost::asio::ip::address_v6 address::to_v6() const
164 {
165 if (type_ != ipv6)
166 {
167 bad_address_cast ex;
168 boost::asio::detail::throw_exception(ex);
169 }
170 return ipv6_address_;
171 }
172
173 std::string address::to_string() const
174 {
175 if (type_ == ipv6)
176 return ipv6_address_.to_string();
177 return ipv4_address_.to_string();
178 }
179
180 #if !defined(BOOST_ASIO_NO_DEPRECATED)
181 std::string address::to_string(boost::system::error_code& ec) const
182 {
183 if (type_ == ipv6)
184 return ipv6_address_.to_string(ec);
185 return ipv4_address_.to_string(ec);
186 }
187 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
188
189 bool address::is_loopback() const
190 {
191 return (type_ == ipv4)
192 ? ipv4_address_.is_loopback()
193 : ipv6_address_.is_loopback();
194 }
195
196 bool address::is_unspecified() const
197 {
198 return (type_ == ipv4)
199 ? ipv4_address_.is_unspecified()
200 : ipv6_address_.is_unspecified();
201 }
202
203 bool address::is_multicast() const
204 {
205 return (type_ == ipv4)
206 ? ipv4_address_.is_multicast()
207 : ipv6_address_.is_multicast();
208 }
209
210 bool operator==(const address& a1, const address& a2)
211 {
212 if (a1.type_ != a2.type_)
213 return false;
214 if (a1.type_ == address::ipv6)
215 return a1.ipv6_address_ == a2.ipv6_address_;
216 return a1.ipv4_address_ == a2.ipv4_address_;
217 }
218
219 bool operator<(const address& a1, const address& a2)
220 {
221 if (a1.type_ < a2.type_)
222 return true;
223 if (a1.type_ > a2.type_)
224 return false;
225 if (a1.type_ == address::ipv6)
226 return a1.ipv6_address_ < a2.ipv6_address_;
227 return a1.ipv4_address_ < a2.ipv4_address_;
228 }
229
230 } // namespace ip
231 } // namespace asio
232 } // namespace boost
233
234 #include <boost/asio/detail/pop_options.hpp>
235
236 #endif // BOOST_ASIO_IP_IMPL_ADDRESS_IPP