address.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // ip/address.hpp
  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_ADDRESS_HPP
  11. #define ASIO_IP_ADDRESS_HPP
  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 <string>
  17. #include "asio/error_code.hpp"
  18. #include "asio/ip/address_v4.hpp"
  19. #include "asio/ip/address_v6.hpp"
  20. #if !defined(ASIO_NO_IOSTREAM)
  21. # include <iosfwd>
  22. #endif // !defined(ASIO_NO_IOSTREAM)
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace ip {
  26. /// Implements version-independent IP addresses.
  27. /**
  28. * The asio::ip::address class provides the ability to use either IP
  29. * version 4 or version 6 addresses.
  30. *
  31. * @par Thread Safety
  32. * @e Distinct @e objects: Safe.@n
  33. * @e Shared @e objects: Unsafe.
  34. */
  35. class address
  36. {
  37. public:
  38. /// Default constructor.
  39. ASIO_DECL address();
  40. /// Construct an address from an IPv4 address.
  41. ASIO_DECL address(const asio::ip::address_v4& ipv4_address);
  42. /// Construct an address from an IPv6 address.
  43. ASIO_DECL address(const asio::ip::address_v6& ipv6_address);
  44. /// Copy constructor.
  45. ASIO_DECL address(const address& other);
  46. #if defined(ASIO_HAS_MOVE)
  47. /// Move constructor.
  48. ASIO_DECL address(address&& other);
  49. #endif // defined(ASIO_HAS_MOVE)
  50. /// Assign from another address.
  51. ASIO_DECL address& operator=(const address& other);
  52. #if defined(ASIO_HAS_MOVE)
  53. /// Move-assign from another address.
  54. ASIO_DECL address& operator=(address&& other);
  55. #endif // defined(ASIO_HAS_MOVE)
  56. /// Assign from an IPv4 address.
  57. ASIO_DECL address& operator=(
  58. const asio::ip::address_v4& ipv4_address);
  59. /// Assign from an IPv6 address.
  60. ASIO_DECL address& operator=(
  61. const asio::ip::address_v6& ipv6_address);
  62. /// Get whether the address is an IP version 4 address.
  63. bool is_v4() const
  64. {
  65. return type_ == ipv4;
  66. }
  67. /// Get whether the address is an IP version 6 address.
  68. bool is_v6() const
  69. {
  70. return type_ == ipv6;
  71. }
  72. /// Get the address as an IP version 4 address.
  73. ASIO_DECL asio::ip::address_v4 to_v4() const;
  74. /// Get the address as an IP version 6 address.
  75. ASIO_DECL asio::ip::address_v6 to_v6() const;
  76. /// Get the address as a string in dotted decimal format.
  77. ASIO_DECL std::string to_string() const;
  78. /// Get the address as a string in dotted decimal format.
  79. ASIO_DECL std::string to_string(asio::error_code& ec) const;
  80. /// Create an address from an IPv4 address string in dotted decimal form,
  81. /// or from an IPv6 address in hexadecimal notation.
  82. ASIO_DECL static address from_string(const char* str);
  83. /// Create an address from an IPv4 address string in dotted decimal form,
  84. /// or from an IPv6 address in hexadecimal notation.
  85. ASIO_DECL static address from_string(
  86. const char* str, asio::error_code& ec);
  87. /// Create an address from an IPv4 address string in dotted decimal form,
  88. /// or from an IPv6 address in hexadecimal notation.
  89. ASIO_DECL static address from_string(const std::string& str);
  90. /// Create an address from an IPv4 address string in dotted decimal form,
  91. /// or from an IPv6 address in hexadecimal notation.
  92. ASIO_DECL static address from_string(
  93. const std::string& str, asio::error_code& ec);
  94. /// Determine whether the address is a loopback address.
  95. ASIO_DECL bool is_loopback() const;
  96. /// Determine whether the address is unspecified.
  97. ASIO_DECL bool is_unspecified() const;
  98. /// Determine whether the address is a multicast address.
  99. ASIO_DECL bool is_multicast() const;
  100. /// Compare two addresses for equality.
  101. ASIO_DECL friend bool operator==(const address& a1, const address& a2);
  102. /// Compare two addresses for inequality.
  103. friend bool operator!=(const address& a1, const address& a2)
  104. {
  105. return !(a1 == a2);
  106. }
  107. /// Compare addresses for ordering.
  108. ASIO_DECL friend bool operator<(const address& a1, const address& a2);
  109. /// Compare addresses for ordering.
  110. friend bool operator>(const address& a1, const address& a2)
  111. {
  112. return a2 < a1;
  113. }
  114. /// Compare addresses for ordering.
  115. friend bool operator<=(const address& a1, const address& a2)
  116. {
  117. return !(a2 < a1);
  118. }
  119. /// Compare addresses for ordering.
  120. friend bool operator>=(const address& a1, const address& a2)
  121. {
  122. return !(a1 < a2);
  123. }
  124. private:
  125. // The type of the address.
  126. enum { ipv4, ipv6 } type_;
  127. // The underlying IPv4 address.
  128. asio::ip::address_v4 ipv4_address_;
  129. // The underlying IPv6 address.
  130. asio::ip::address_v6 ipv6_address_;
  131. };
  132. #if !defined(ASIO_NO_IOSTREAM)
  133. /// Output an address as a string.
  134. /**
  135. * Used to output a human-readable string for a specified address.
  136. *
  137. * @param os The output stream to which the string will be written.
  138. *
  139. * @param addr The address to be written.
  140. *
  141. * @return The output stream.
  142. *
  143. * @relates asio::ip::address
  144. */
  145. template <typename Elem, typename Traits>
  146. std::basic_ostream<Elem, Traits>& operator<<(
  147. std::basic_ostream<Elem, Traits>& os, const address& addr);
  148. #endif // !defined(ASIO_NO_IOSTREAM)
  149. } // namespace ip
  150. } // namespace asio
  151. #include "asio/detail/pop_options.hpp"
  152. #include "asio/ip/impl/address.hpp"
  153. #if defined(ASIO_HEADER_ONLY)
  154. # include "asio/ip/impl/address.ipp"
  155. #endif // defined(ASIO_HEADER_ONLY)
  156. #endif // ASIO_IP_ADDRESS_HPP