basic_endpoint.hpp 4.5 KB

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