5 // Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IP_ADDRESS_HPP
12 #define BOOST_ASIO_IP_ADDRESS_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/system/error_code.hpp>
21 #include <boost/asio/ip/address_v4.hpp>
22 #include <boost/asio/ip/address_v6.hpp>
24 #if !defined(BOOST_ASIO_NO_IOSTREAM)
26 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
28 #include <boost/asio/detail/push_options.hpp>
34 /// Implements version-independent IP addresses.
36 * The boost::asio::ip::address class provides the ability to use either IP
37 * version 4 or version 6 addresses.
40 * @e Distinct @e objects: Safe.@n
41 * @e Shared @e objects: Unsafe.
46 /// Default constructor.
47 BOOST_ASIO_DECL address();
49 /// Construct an address from an IPv4 address.
50 BOOST_ASIO_DECL address(const boost::asio::ip::address_v4& ipv4_address);
52 /// Construct an address from an IPv6 address.
53 BOOST_ASIO_DECL address(const boost::asio::ip::address_v6& ipv6_address);
56 BOOST_ASIO_DECL address(const address& other);
58 #if defined(BOOST_ASIO_HAS_MOVE)
60 BOOST_ASIO_DECL address(address&& other);
61 #endif // defined(BOOST_ASIO_HAS_MOVE)
63 /// Assign from another address.
64 BOOST_ASIO_DECL address& operator=(const address& other);
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)
71 /// Assign from an IPv4 address.
72 BOOST_ASIO_DECL address& operator=(
73 const boost::asio::ip::address_v4& ipv4_address);
75 /// Assign from an IPv6 address.
76 BOOST_ASIO_DECL address& operator=(
77 const boost::asio::ip::address_v6& ipv6_address);
79 /// Get whether the address is an IP version 4 address.
85 /// Get whether the address is an IP version 6 address.
91 /// Get the address as an IP version 4 address.
92 BOOST_ASIO_DECL boost::asio::ip::address_v4 to_v4() const;
94 /// Get the address as an IP version 6 address.
95 BOOST_ASIO_DECL boost::asio::ip::address_v6 to_v6() const;
97 /// Get the address as a string in dotted decimal format.
98 BOOST_ASIO_DECL std::string to_string() const;
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;
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);
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);
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);
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);
121 /// Determine whether the address is a loopback address.
122 BOOST_ASIO_DECL bool is_loopback() const;
124 /// Determine whether the address is unspecified.
125 BOOST_ASIO_DECL bool is_unspecified() const;
127 /// Determine whether the address is a multicast address.
128 BOOST_ASIO_DECL bool is_multicast() const;
130 /// Compare two addresses for equality.
131 BOOST_ASIO_DECL friend bool operator==(const address& a1, const address& a2);
133 /// Compare two addresses for inequality.
134 friend bool operator!=(const address& a1, const address& a2)
139 /// Compare addresses for ordering.
140 BOOST_ASIO_DECL friend bool operator<(const address& a1, const address& a2);
142 /// Compare addresses for ordering.
143 friend bool operator>(const address& a1, const address& a2)
148 /// Compare addresses for ordering.
149 friend bool operator<=(const address& a1, const address& a2)
154 /// Compare addresses for ordering.
155 friend bool operator>=(const address& a1, const address& a2)
161 // The type of the address.
162 enum { ipv4, ipv6 } type_;
164 // The underlying IPv4 address.
165 boost::asio::ip::address_v4 ipv4_address_;
167 // The underlying IPv6 address.
168 boost::asio::ip::address_v6 ipv6_address_;
171 #if !defined(BOOST_ASIO_NO_IOSTREAM)
173 /// Output an address as a string.
175 * Used to output a human-readable string for a specified address.
177 * @param os The output stream to which the string will be written.
179 * @param addr The address to be written.
181 * @return The output stream.
183 * @relates boost::asio::ip::address
185 template <typename Elem, typename Traits>
186 std::basic_ostream<Elem, Traits>& operator<<(
187 std::basic_ostream<Elem, Traits>& os, const address& addr);
189 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
195 #include <boost/asio/detail/pop_options.hpp>
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)
202 #endif // BOOST_ASIO_IP_ADDRESS_HPP