basic_endpoint.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // generic/basic_endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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() 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. /// Move constructor.
  71. basic_endpoint(basic_endpoint&& other)
  72. : impl_(other.impl_)
  73. {
  74. }
  75. /// Assign from another endpoint.
  76. basic_endpoint& operator=(const basic_endpoint& other)
  77. {
  78. impl_ = other.impl_;
  79. return *this;
  80. }
  81. /// Move-assign from another endpoint.
  82. basic_endpoint& operator=(basic_endpoint&& other)
  83. {
  84. impl_ = other.impl_;
  85. return *this;
  86. }
  87. /// The protocol associated with the endpoint.
  88. protocol_type protocol() const
  89. {
  90. return protocol_type(impl_.family(), impl_.protocol());
  91. }
  92. /// Get the underlying endpoint in the native type.
  93. data_type* data()
  94. {
  95. return impl_.data();
  96. }
  97. /// Get the underlying endpoint in the native type.
  98. const data_type* data() const
  99. {
  100. return impl_.data();
  101. }
  102. /// Get the underlying size of the endpoint in the native type.
  103. std::size_t size() const
  104. {
  105. return impl_.size();
  106. }
  107. /// Set the underlying size of the endpoint in the native type.
  108. void resize(std::size_t new_size)
  109. {
  110. impl_.resize(new_size);
  111. }
  112. /// Get the capacity of the endpoint in the native type.
  113. std::size_t capacity() const
  114. {
  115. return impl_.capacity();
  116. }
  117. /// Compare two endpoints for equality.
  118. friend bool operator==(const basic_endpoint<Protocol>& e1,
  119. const basic_endpoint<Protocol>& e2)
  120. {
  121. return e1.impl_ == e2.impl_;
  122. }
  123. /// Compare two endpoints for inequality.
  124. friend bool operator!=(const basic_endpoint<Protocol>& e1,
  125. const basic_endpoint<Protocol>& e2)
  126. {
  127. return !(e1.impl_ == e2.impl_);
  128. }
  129. /// Compare endpoints for ordering.
  130. friend bool operator<(const basic_endpoint<Protocol>& e1,
  131. const basic_endpoint<Protocol>& e2)
  132. {
  133. return e1.impl_ < e2.impl_;
  134. }
  135. /// Compare endpoints for ordering.
  136. friend bool operator>(const basic_endpoint<Protocol>& e1,
  137. const basic_endpoint<Protocol>& e2)
  138. {
  139. return e2.impl_ < e1.impl_;
  140. }
  141. /// Compare endpoints for ordering.
  142. friend bool operator<=(const basic_endpoint<Protocol>& e1,
  143. const basic_endpoint<Protocol>& e2)
  144. {
  145. return !(e2 < e1);
  146. }
  147. /// Compare endpoints for ordering.
  148. friend bool operator>=(const basic_endpoint<Protocol>& e1,
  149. const basic_endpoint<Protocol>& e2)
  150. {
  151. return !(e1 < e2);
  152. }
  153. private:
  154. // The underlying generic endpoint.
  155. boost::asio::generic::detail::endpoint impl_;
  156. };
  157. } // namespace generic
  158. } // namespace asio
  159. } // namespace boost
  160. #include <boost/asio/detail/pop_options.hpp>
  161. #endif // BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP