Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / asio / ip / address.hpp
1 //
2 // ip/address.hpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2014 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_ADDRESS_HPP
12 #define BOOST_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 <boost/asio/detail/config.hpp>
19 #include <string>
20 #include <boost/system/error_code.hpp>
21 #include <boost/asio/ip/address_v4.hpp>
22 #include <boost/asio/ip/address_v6.hpp>
23
24 #if !defined(BOOST_ASIO_NO_IOSTREAM)
25 # include <iosfwd>
26 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
27
28 #include <boost/asio/detail/push_options.hpp>
29
30 namespace boost {
31 namespace asio {
32 namespace ip {
33
34 /// Implements version-independent IP addresses.
35 /**
36  * The boost::asio::ip::address class provides the ability to use either IP
37  * version 4 or version 6 addresses.
38  *
39  * @par Thread Safety
40  * @e Distinct @e objects: Safe.@n
41  * @e Shared @e objects: Unsafe.
42  */
43 class address
44 {
45 public:
46   /// Default constructor.
47   BOOST_ASIO_DECL address();
48
49   /// Construct an address from an IPv4 address.
50   BOOST_ASIO_DECL address(const boost::asio::ip::address_v4& ipv4_address);
51
52   /// Construct an address from an IPv6 address.
53   BOOST_ASIO_DECL address(const boost::asio::ip::address_v6& ipv6_address);
54
55   /// Copy constructor.
56   BOOST_ASIO_DECL address(const address& other);
57
58 #if defined(BOOST_ASIO_HAS_MOVE)
59   /// Move constructor.
60   BOOST_ASIO_DECL address(address&& other);
61 #endif // defined(BOOST_ASIO_HAS_MOVE)
62
63   /// Assign from another address.
64   BOOST_ASIO_DECL address& operator=(const address& other);
65
66 #if defined(BOOST_ASIO_HAS_MOVE)
67   /// Move-assign from another address.
68   BOOST_ASIO_DECL address& operator=(address&& other);
69 #endif // defined(BOOST_ASIO_HAS_MOVE)
70
71   /// Assign from an IPv4 address.
72   BOOST_ASIO_DECL address& operator=(
73       const boost::asio::ip::address_v4& ipv4_address);
74
75   /// Assign from an IPv6 address.
76   BOOST_ASIO_DECL address& operator=(
77       const boost::asio::ip::address_v6& ipv6_address);
78
79   /// Get whether the address is an IP version 4 address.
80   bool is_v4() const
81   {
82     return type_ == ipv4;
83   }
84
85   /// Get whether the address is an IP version 6 address.
86   bool is_v6() const
87   {
88     return type_ == ipv6;
89   }
90
91   /// Get the address as an IP version 4 address.
92   BOOST_ASIO_DECL boost::asio::ip::address_v4 to_v4() const;
93
94   /// Get the address as an IP version 6 address.
95   BOOST_ASIO_DECL boost::asio::ip::address_v6 to_v6() const;
96
97   /// Get the address as a string in dotted decimal format.
98   BOOST_ASIO_DECL std::string to_string() const;
99
100   /// Get the address as a string in dotted decimal format.
101   BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
102
103   /// Create an address from an IPv4 address string in dotted decimal form,
104   /// or from an IPv6 address in hexadecimal notation.
105   BOOST_ASIO_DECL static address from_string(const char* str);
106
107   /// Create an address from an IPv4 address string in dotted decimal form,
108   /// or from an IPv6 address in hexadecimal notation.
109   BOOST_ASIO_DECL static address from_string(
110       const char* str, boost::system::error_code& ec);
111
112   /// Create an address from an IPv4 address string in dotted decimal form,
113   /// or from an IPv6 address in hexadecimal notation.
114   BOOST_ASIO_DECL static address from_string(const std::string& str);
115
116   /// Create an address from an IPv4 address string in dotted decimal form,
117   /// or from an IPv6 address in hexadecimal notation.
118   BOOST_ASIO_DECL static address from_string(
119       const std::string& str, boost::system::error_code& ec);
120
121   /// Determine whether the address is a loopback address.
122   BOOST_ASIO_DECL bool is_loopback() const;
123
124   /// Determine whether the address is unspecified.
125   BOOST_ASIO_DECL bool is_unspecified() const;
126
127   /// Determine whether the address is a multicast address.
128   BOOST_ASIO_DECL bool is_multicast() const;
129
130   /// Compare two addresses for equality.
131   BOOST_ASIO_DECL friend bool operator==(const address& a1, const address& a2);
132
133   /// Compare two addresses for inequality.
134   friend bool operator!=(const address& a1, const address& a2)
135   {
136     return !(a1 == a2);
137   }
138
139   /// Compare addresses for ordering.
140   BOOST_ASIO_DECL friend bool operator<(const address& a1, const address& a2);
141
142   /// Compare addresses for ordering.
143   friend bool operator>(const address& a1, const address& a2)
144   {
145     return a2 < a1;
146   }
147
148   /// Compare addresses for ordering.
149   friend bool operator<=(const address& a1, const address& a2)
150   {
151     return !(a2 < a1);
152   }
153
154   /// Compare addresses for ordering.
155   friend bool operator>=(const address& a1, const address& a2)
156   {
157     return !(a1 < a2);
158   }
159
160 private:
161   // The type of the address.
162   enum { ipv4, ipv6 } type_;
163
164   // The underlying IPv4 address.
165   boost::asio::ip::address_v4 ipv4_address_;
166
167   // The underlying IPv6 address.
168   boost::asio::ip::address_v6 ipv6_address_;
169 };
170
171 #if !defined(BOOST_ASIO_NO_IOSTREAM)
172
173 /// Output an address as a string.
174 /**
175  * Used to output a human-readable string for a specified address.
176  *
177  * @param os The output stream to which the string will be written.
178  *
179  * @param addr The address to be written.
180  *
181  * @return The output stream.
182  *
183  * @relates boost::asio::ip::address
184  */
185 template <typename Elem, typename Traits>
186 std::basic_ostream<Elem, Traits>& operator<<(
187     std::basic_ostream<Elem, Traits>& os, const address& addr);
188
189 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
190
191 } // namespace ip
192 } // namespace asio
193 } // namespace boost
194
195 #include <boost/asio/detail/pop_options.hpp>
196
197 #include <boost/asio/ip/impl/address.hpp>
198 #if defined(BOOST_ASIO_HEADER_ONLY)
199 # include <boost/asio/ip/impl/address.ipp>
200 #endif // defined(BOOST_ASIO_HEADER_ONLY)
201
202 #endif // BOOST_ASIO_IP_ADDRESS_HPP