basic_endpoint.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // local/basic_endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Derived from a public domain implementation written by Daniel Casimiro.
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
  12. #define BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <boost/asio/local/detail/endpoint.hpp>
  20. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  21. # include <iosfwd>
  22. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace local {
  27. /// Describes an endpoint for a UNIX socket.
  28. /**
  29. * The boost::asio::local::basic_endpoint class template describes an endpoint
  30. * that may be associated with a particular UNIX socket.
  31. *
  32. * @par Thread Safety
  33. * @e Distinct @e objects: Safe.@n
  34. * @e Shared @e objects: Unsafe.
  35. *
  36. * @par Concepts:
  37. * Endpoint.
  38. */
  39. template <typename Protocol>
  40. class basic_endpoint
  41. {
  42. public:
  43. /// The protocol type associated with the endpoint.
  44. typedef Protocol protocol_type;
  45. /// The type of the endpoint structure. This type is dependent on the
  46. /// underlying implementation of the socket layer.
  47. #if defined(GENERATING_DOCUMENTATION)
  48. typedef implementation_defined data_type;
  49. #else
  50. typedef boost::asio::detail::socket_addr_type data_type;
  51. #endif
  52. /// Default constructor.
  53. basic_endpoint() noexcept
  54. {
  55. }
  56. /// Construct an endpoint using the specified path name.
  57. basic_endpoint(const char* path_name)
  58. : impl_(path_name)
  59. {
  60. }
  61. /// Construct an endpoint using the specified path name.
  62. basic_endpoint(const std::string& path_name)
  63. : impl_(path_name)
  64. {
  65. }
  66. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  67. /// Construct an endpoint using the specified path name.
  68. basic_endpoint(string_view path_name)
  69. : impl_(path_name)
  70. {
  71. }
  72. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  73. /// Copy constructor.
  74. basic_endpoint(const basic_endpoint& other)
  75. : impl_(other.impl_)
  76. {
  77. }
  78. /// Move constructor.
  79. basic_endpoint(basic_endpoint&& other)
  80. : impl_(other.impl_)
  81. {
  82. }
  83. /// Assign from another endpoint.
  84. basic_endpoint& operator=(const basic_endpoint& other)
  85. {
  86. impl_ = other.impl_;
  87. return *this;
  88. }
  89. /// Move-assign from another endpoint.
  90. basic_endpoint& operator=(basic_endpoint&& other)
  91. {
  92. impl_ = other.impl_;
  93. return *this;
  94. }
  95. /// The protocol associated with the endpoint.
  96. protocol_type protocol() const
  97. {
  98. return protocol_type();
  99. }
  100. /// Get the underlying endpoint in the native type.
  101. data_type* data()
  102. {
  103. return impl_.data();
  104. }
  105. /// Get the underlying endpoint in the native type.
  106. const data_type* data() const
  107. {
  108. return impl_.data();
  109. }
  110. /// Get the underlying size of the endpoint in the native type.
  111. std::size_t size() const
  112. {
  113. return impl_.size();
  114. }
  115. /// Set the underlying size of the endpoint in the native type.
  116. void resize(std::size_t new_size)
  117. {
  118. impl_.resize(new_size);
  119. }
  120. /// Get the capacity of the endpoint in the native type.
  121. std::size_t capacity() const
  122. {
  123. return impl_.capacity();
  124. }
  125. /// Get the path associated with the endpoint.
  126. std::string path() const
  127. {
  128. return impl_.path();
  129. }
  130. /// Set the path associated with the endpoint.
  131. void path(const char* p)
  132. {
  133. impl_.path(p);
  134. }
  135. /// Set the path associated with the endpoint.
  136. void path(const std::string& p)
  137. {
  138. impl_.path(p);
  139. }
  140. /// Compare two endpoints for equality.
  141. friend bool operator==(const basic_endpoint<Protocol>& e1,
  142. const basic_endpoint<Protocol>& e2)
  143. {
  144. return e1.impl_ == e2.impl_;
  145. }
  146. /// Compare two endpoints for inequality.
  147. friend bool operator!=(const basic_endpoint<Protocol>& e1,
  148. const basic_endpoint<Protocol>& e2)
  149. {
  150. return !(e1.impl_ == e2.impl_);
  151. }
  152. /// Compare endpoints for ordering.
  153. friend bool operator<(const basic_endpoint<Protocol>& e1,
  154. const basic_endpoint<Protocol>& e2)
  155. {
  156. return e1.impl_ < e2.impl_;
  157. }
  158. /// Compare endpoints for ordering.
  159. friend bool operator>(const basic_endpoint<Protocol>& e1,
  160. const basic_endpoint<Protocol>& e2)
  161. {
  162. return e2.impl_ < e1.impl_;
  163. }
  164. /// Compare endpoints for ordering.
  165. friend bool operator<=(const basic_endpoint<Protocol>& e1,
  166. const basic_endpoint<Protocol>& e2)
  167. {
  168. return !(e2 < e1);
  169. }
  170. /// Compare endpoints for ordering.
  171. friend bool operator>=(const basic_endpoint<Protocol>& e1,
  172. const basic_endpoint<Protocol>& e2)
  173. {
  174. return !(e1 < e2);
  175. }
  176. private:
  177. // The underlying UNIX domain endpoint.
  178. boost::asio::local::detail::endpoint impl_;
  179. };
  180. /// Output an endpoint as a string.
  181. /**
  182. * Used to output a human-readable string for a specified endpoint.
  183. *
  184. * @param os The output stream to which the string will be written.
  185. *
  186. * @param endpoint The endpoint to be written.
  187. *
  188. * @return The output stream.
  189. *
  190. * @relates boost::asio::local::basic_endpoint
  191. */
  192. template <typename Elem, typename Traits, typename Protocol>
  193. std::basic_ostream<Elem, Traits>& operator<<(
  194. std::basic_ostream<Elem, Traits>& os,
  195. const basic_endpoint<Protocol>& endpoint)
  196. {
  197. os << endpoint.path();
  198. return os;
  199. }
  200. } // namespace local
  201. } // namespace asio
  202. } // namespace boost
  203. #include <boost/asio/detail/pop_options.hpp>
  204. #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  205. // || defined(GENERATING_DOCUMENTATION)
  206. #endif // BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP