address.ipp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // ip/impl/address.ipp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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. #ifndef ASIO_IP_IMPL_ADDRESS_IPP
  11. #define ASIO_IP_IMPL_ADDRESS_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <typeinfo>
  17. #include "asio/detail/throw_error.hpp"
  18. #include "asio/detail/throw_exception.hpp"
  19. #include "asio/error.hpp"
  20. #include "asio/ip/address.hpp"
  21. #include "asio/system_error.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace ip {
  25. address::address()
  26. : type_(ipv4),
  27. ipv4_address_(),
  28. ipv6_address_()
  29. {
  30. }
  31. address::address(const asio::ip::address_v4& ipv4_address)
  32. : type_(ipv4),
  33. ipv4_address_(ipv4_address),
  34. ipv6_address_()
  35. {
  36. }
  37. address::address(const asio::ip::address_v6& ipv6_address)
  38. : type_(ipv6),
  39. ipv4_address_(),
  40. ipv6_address_(ipv6_address)
  41. {
  42. }
  43. address::address(const address& other)
  44. : type_(other.type_),
  45. ipv4_address_(other.ipv4_address_),
  46. ipv6_address_(other.ipv6_address_)
  47. {
  48. }
  49. #if defined(ASIO_HAS_MOVE)
  50. address::address(address&& other)
  51. : type_(other.type_),
  52. ipv4_address_(other.ipv4_address_),
  53. ipv6_address_(other.ipv6_address_)
  54. {
  55. }
  56. #endif // defined(ASIO_HAS_MOVE)
  57. address& address::operator=(const address& other)
  58. {
  59. type_ = other.type_;
  60. ipv4_address_ = other.ipv4_address_;
  61. ipv6_address_ = other.ipv6_address_;
  62. return *this;
  63. }
  64. #if defined(ASIO_HAS_MOVE)
  65. address& address::operator=(address&& other)
  66. {
  67. type_ = other.type_;
  68. ipv4_address_ = other.ipv4_address_;
  69. ipv6_address_ = other.ipv6_address_;
  70. return *this;
  71. }
  72. #endif // defined(ASIO_HAS_MOVE)
  73. address& address::operator=(const asio::ip::address_v4& ipv4_address)
  74. {
  75. type_ = ipv4;
  76. ipv4_address_ = ipv4_address;
  77. ipv6_address_ = asio::ip::address_v6();
  78. return *this;
  79. }
  80. address& address::operator=(const asio::ip::address_v6& ipv6_address)
  81. {
  82. type_ = ipv6;
  83. ipv4_address_ = asio::ip::address_v4();
  84. ipv6_address_ = ipv6_address;
  85. return *this;
  86. }
  87. asio::ip::address_v4 address::to_v4() const
  88. {
  89. if (type_ != ipv4)
  90. {
  91. std::bad_cast ex;
  92. asio::detail::throw_exception(ex);
  93. }
  94. return ipv4_address_;
  95. }
  96. asio::ip::address_v6 address::to_v6() const
  97. {
  98. if (type_ != ipv6)
  99. {
  100. std::bad_cast ex;
  101. asio::detail::throw_exception(ex);
  102. }
  103. return ipv6_address_;
  104. }
  105. std::string address::to_string() const
  106. {
  107. if (type_ == ipv6)
  108. return ipv6_address_.to_string();
  109. return ipv4_address_.to_string();
  110. }
  111. std::string address::to_string(asio::error_code& ec) const
  112. {
  113. if (type_ == ipv6)
  114. return ipv6_address_.to_string(ec);
  115. return ipv4_address_.to_string(ec);
  116. }
  117. address address::from_string(const char* str)
  118. {
  119. asio::error_code ec;
  120. address addr = from_string(str, ec);
  121. asio::detail::throw_error(ec);
  122. return addr;
  123. }
  124. address address::from_string(const char* str, asio::error_code& ec)
  125. {
  126. asio::ip::address_v6 ipv6_address =
  127. asio::ip::address_v6::from_string(str, ec);
  128. if (!ec)
  129. {
  130. address tmp;
  131. tmp.type_ = ipv6;
  132. tmp.ipv6_address_ = ipv6_address;
  133. return tmp;
  134. }
  135. asio::ip::address_v4 ipv4_address =
  136. asio::ip::address_v4::from_string(str, ec);
  137. if (!ec)
  138. {
  139. address tmp;
  140. tmp.type_ = ipv4;
  141. tmp.ipv4_address_ = ipv4_address;
  142. return tmp;
  143. }
  144. return address();
  145. }
  146. address address::from_string(const std::string& str)
  147. {
  148. return from_string(str.c_str());
  149. }
  150. address address::from_string(const std::string& str,
  151. asio::error_code& ec)
  152. {
  153. return from_string(str.c_str(), ec);
  154. }
  155. bool address::is_loopback() const
  156. {
  157. return (type_ == ipv4)
  158. ? ipv4_address_.is_loopback()
  159. : ipv6_address_.is_loopback();
  160. }
  161. bool address::is_unspecified() const
  162. {
  163. return (type_ == ipv4)
  164. ? ipv4_address_.is_unspecified()
  165. : ipv6_address_.is_unspecified();
  166. }
  167. bool address::is_multicast() const
  168. {
  169. return (type_ == ipv4)
  170. ? ipv4_address_.is_multicast()
  171. : ipv6_address_.is_multicast();
  172. }
  173. bool operator==(const address& a1, const address& a2)
  174. {
  175. if (a1.type_ != a2.type_)
  176. return false;
  177. if (a1.type_ == address::ipv6)
  178. return a1.ipv6_address_ == a2.ipv6_address_;
  179. return a1.ipv4_address_ == a2.ipv4_address_;
  180. }
  181. bool operator<(const address& a1, const address& a2)
  182. {
  183. if (a1.type_ < a2.type_)
  184. return true;
  185. if (a1.type_ > a2.type_)
  186. return false;
  187. if (a1.type_ == address::ipv6)
  188. return a1.ipv6_address_ < a2.ipv6_address_;
  189. return a1.ipv4_address_ < a2.ipv4_address_;
  190. }
  191. } // namespace ip
  192. } // namespace asio
  193. #include "asio/detail/pop_options.hpp"
  194. #endif // ASIO_IP_IMPL_ADDRESS_IPP