basic_endpoint.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // generic/basic_endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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 BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP
  11. #define BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/generic/detail/endpoint.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace generic {
  21. /// Describes an endpoint for any socket type.
  22. /**
  23. * The boost::asio::generic::basic_endpoint class template describes an endpoint
  24. * that may be associated with any socket type.
  25. *
  26. * @note The socket types sockaddr type must be able to fit into a
  27. * @c sockaddr_storage structure.
  28. *
  29. * @par Thread Safety
  30. * @e Distinct @e objects: Safe.@n
  31. * @e Shared @e objects: Unsafe.
  32. *
  33. * @par Concepts:
  34. * Endpoint.
  35. */
  36. template <typename Protocol>
  37. class basic_endpoint
  38. {
  39. public:
  40. /// The protocol type associated with the endpoint.
  41. typedef Protocol protocol_type;
  42. /// The type of the endpoint structure. This type is dependent on the
  43. /// underlying implementation of the socket layer.
  44. #if defined(GENERATING_DOCUMENTATION)
  45. typedef implementation_defined data_type;
  46. #else
  47. typedef boost::asio::detail::socket_addr_type data_type;
  48. #endif
  49. /// Default constructor.
  50. basic_endpoint() BOOST_ASIO_NOEXCEPT
  51. {
  52. }
  53. /// Construct an endpoint from the specified socket address.
  54. basic_endpoint(const void* socket_address,
  55. std::size_t socket_address_size, int socket_protocol = 0)
  56. : impl_(socket_address, socket_address_size, socket_protocol)
  57. {
  58. }
  59. /// Construct an endpoint from the specific endpoint type.
  60. template <typename Endpoint>
  61. basic_endpoint(const Endpoint& endpoint)
  62. : impl_(endpoint.data(), endpoint.size(), endpoint.protocol().protocol())
  63. {
  64. }
  65. /// Copy constructor.
  66. basic_endpoint(const basic_endpoint& other)
  67. : impl_(other.impl_)
  68. {
  69. }
  70. #if defined(BOOST_ASIO_HAS_MOVE)
  71. /// Move constructor.
  72. basic_endpoint(basic_endpoint&& other)
  73. : impl_(other.impl_)
  74. {
  75. }
  76. #endif // defined(BOOST_ASIO_HAS_MOVE)
  77. /// Assign from another endpoint.
  78. basic_endpoint& operator=(const basic_endpoint& other)
  79. {
  80. impl_ = other.impl_;
  81. return *this;
  82. }
  83. #if defined(BOOST_ASIO_HAS_MOVE)
  84. /// Move-assign from another endpoint.
  85. basic_endpoint& operator=(basic_endpoint&& other)
  86. {
  87. impl_ = other.impl_;
  88. return *this;
  89. }
  90. #endif // defined(BOOST_ASIO_HAS_MOVE)
  91. /// The protocol associated with the endpoint.
  92. protocol_type protocol() const
  93. {
  94. return protocol_type(impl_.family(), impl_.protocol());
  95. }
  96. /// Get the underlying endpoint in the native type.
  97. data_type* data()
  98. {
  99. return impl_.data();
  100. }
  101. /// Get the underlying endpoint in the native type.
  102. const data_type* data() const
  103. {
  104. return impl_.data();
  105. }
  106. /// Get the underlying size of the endpoint in the native type.
  107. std::size_t size() const
  108. {
  109. return impl_.size();
  110. }
  111. /// Set the underlying size of the endpoint in the native type.
  112. void resize(std::size_t new_size)
  113. {
  114. impl_.resize(new_size);
  115. }
  116. /// Get the capacity of the endpoint in the native type.
  117. std::size_t capacity() const
  118. {
  119. return impl_.capacity();
  120. }
  121. /// Compare two endpoints for equality.
  122. friend bool operator==(const basic_endpoint<Protocol>& e1,
  123. const basic_endpoint<Protocol>& e2)
  124. {
  125. return e1.impl_ == e2.impl_;
  126. }
  127. /// Compare two endpoints for inequality.
  128. friend bool operator!=(const basic_endpoint<Protocol>& e1,
  129. const basic_endpoint<Protocol>& e2)
  130. {
  131. return !(e1.impl_ == e2.impl_);
  132. }
  133. /// Compare endpoints for ordering.
  134. friend bool operator<(const basic_endpoint<Protocol>& e1,
  135. const basic_endpoint<Protocol>& e2)
  136. {
  137. return e1.impl_ < e2.impl_;
  138. }
  139. /// Compare endpoints for ordering.
  140. friend bool operator>(const basic_endpoint<Protocol>& e1,
  141. const basic_endpoint<Protocol>& e2)
  142. {
  143. return e2.impl_ < e1.impl_;
  144. }
  145. /// Compare endpoints for ordering.
  146. friend bool operator<=(const basic_endpoint<Protocol>& e1,
  147. const basic_endpoint<Protocol>& e2)
  148. {
  149. return !(e2 < e1);
  150. }
  151. /// Compare endpoints for ordering.
  152. friend bool operator>=(const basic_endpoint<Protocol>& e1,
  153. const basic_endpoint<Protocol>& e2)
  154. {
  155. return !(e1 < e2);
  156. }
  157. private:
  158. // The underlying generic endpoint.
  159. boost::asio::generic::detail::endpoint impl_;
  160. };
  161. } // namespace generic
  162. } // namespace asio
  163. } // namespace boost
  164. #include <boost/asio/detail/pop_options.hpp>
  165. #endif // BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP