Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ASIO_IP_ADDRESS_HPP
00012 #define ASIO_IP_ADDRESS_HPP
00013
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017
00018 #include "asio/detail/push_options.hpp"
00019
00020 #include "asio/detail/push_options.hpp"
00021 #include <iosfwd>
00022 #include <string>
00023 #include <boost/throw_exception.hpp>
00024 #include "asio/detail/pop_options.hpp"
00025
00026 #include "asio/error.hpp"
00027 #include "asio/ip/address_v4.hpp"
00028 #include "asio/ip/address_v6.hpp"
00029 #include "asio/detail/throw_error.hpp"
00030
00031 namespace asio {
00032 namespace ip {
00033
00035
00043 class address
00044 {
00045 public:
00047 address()
00048 : type_(ipv4),
00049 ipv4_address_(),
00050 ipv6_address_()
00051 {
00052 }
00053
00055 address(const asio::ip::address_v4& ipv4_address)
00056 : type_(ipv4),
00057 ipv4_address_(ipv4_address),
00058 ipv6_address_()
00059 {
00060 }
00061
00063 address(const asio::ip::address_v6& ipv6_address)
00064 : type_(ipv6),
00065 ipv4_address_(),
00066 ipv6_address_(ipv6_address)
00067 {
00068 }
00069
00071 address(const address& other)
00072 : type_(other.type_),
00073 ipv4_address_(other.ipv4_address_),
00074 ipv6_address_(other.ipv6_address_)
00075 {
00076 }
00077
00079 address& operator=(const address& other)
00080 {
00081 type_ = other.type_;
00082 ipv4_address_ = other.ipv4_address_;
00083 ipv6_address_ = other.ipv6_address_;
00084 return *this;
00085 }
00086
00088 address& operator=(const asio::ip::address_v4& ipv4_address)
00089 {
00090 type_ = ipv4;
00091 ipv4_address_ = ipv4_address;
00092 ipv6_address_ = asio::ip::address_v6();
00093 return *this;
00094 }
00095
00097 address& operator=(const asio::ip::address_v6& ipv6_address)
00098 {
00099 type_ = ipv6;
00100 ipv4_address_ = asio::ip::address_v4();
00101 ipv6_address_ = ipv6_address;
00102 return *this;
00103 }
00104
00106 bool is_v4() const
00107 {
00108 return type_ == ipv4;
00109 }
00110
00112 bool is_v6() const
00113 {
00114 return type_ == ipv6;
00115 }
00116
00118 asio::ip::address_v4 to_v4() const
00119 {
00120 if (type_ != ipv4)
00121 {
00122 asio::system_error e(
00123 asio::error::address_family_not_supported);
00124 boost::throw_exception(e);
00125 }
00126 return ipv4_address_;
00127 }
00128
00130 asio::ip::address_v6 to_v6() const
00131 {
00132 if (type_ != ipv6)
00133 {
00134 asio::system_error e(
00135 asio::error::address_family_not_supported);
00136 boost::throw_exception(e);
00137 }
00138 return ipv6_address_;
00139 }
00140
00142 std::string to_string() const
00143 {
00144 if (type_ == ipv6)
00145 return ipv6_address_.to_string();
00146 return ipv4_address_.to_string();
00147 }
00148
00150 std::string to_string(asio::error_code& ec) const
00151 {
00152 if (type_ == ipv6)
00153 return ipv6_address_.to_string(ec);
00154 return ipv4_address_.to_string(ec);
00155 }
00156
00159 static address from_string(const char* str)
00160 {
00161 asio::error_code ec;
00162 address addr = from_string(str, ec);
00163 asio::detail::throw_error(ec);
00164 return addr;
00165 }
00166
00169 static address from_string(const char* str, asio::error_code& ec)
00170 {
00171 asio::ip::address_v6 ipv6_address =
00172 asio::ip::address_v6::from_string(str, ec);
00173 if (!ec)
00174 {
00175 address tmp;
00176 tmp.type_ = ipv6;
00177 tmp.ipv6_address_ = ipv6_address;
00178 return tmp;
00179 }
00180
00181 asio::ip::address_v4 ipv4_address =
00182 asio::ip::address_v4::from_string(str, ec);
00183 if (!ec)
00184 {
00185 address tmp;
00186 tmp.type_ = ipv4;
00187 tmp.ipv4_address_ = ipv4_address;
00188 return tmp;
00189 }
00190
00191 return address();
00192 }
00193
00196 static address from_string(const std::string& str)
00197 {
00198 return from_string(str.c_str());
00199 }
00200
00203 static address from_string(const std::string& str,
00204 asio::error_code& ec)
00205 {
00206 return from_string(str.c_str(), ec);
00207 }
00208
00210 friend bool operator==(const address& a1, const address& a2)
00211 {
00212 if (a1.type_ != a2.type_)
00213 return false;
00214 if (a1.type_ == ipv6)
00215 return a1.ipv6_address_ == a2.ipv6_address_;
00216 return a1.ipv4_address_ == a2.ipv4_address_;
00217 }
00218
00220 friend bool operator!=(const address& a1, const address& a2)
00221 {
00222 if (a1.type_ != a2.type_)
00223 return true;
00224 if (a1.type_ == ipv6)
00225 return a1.ipv6_address_ != a2.ipv6_address_;
00226 return a1.ipv4_address_ != a2.ipv4_address_;
00227 }
00228
00230 friend bool operator<(const address& a1, const address& a2)
00231 {
00232 if (a1.type_ < a2.type_)
00233 return true;
00234 if (a1.type_ > a2.type_)
00235 return false;
00236 if (a1.type_ == ipv6)
00237 return a1.ipv6_address_ < a2.ipv6_address_;
00238 return a1.ipv4_address_ < a2.ipv4_address_;
00239 }
00240
00241 private:
00242
00243 enum { ipv4, ipv6 } type_;
00244
00245
00246 asio::ip::address_v4 ipv4_address_;
00247
00248
00249 asio::ip::address_v6 ipv6_address_;
00250 };
00251
00253
00264 template <typename Elem, typename Traits>
00265 std::basic_ostream<Elem, Traits>& operator<<(
00266 std::basic_ostream<Elem, Traits>& os, const address& addr)
00267 {
00268 os << addr.to_string();
00269 return os;
00270 }
00271
00272 }
00273 }
00274
00275 #include "asio/detail/pop_options.hpp"
00276
00277 #endif // ASIO_IP_ADDRESS_HPP