basic_endpoint.hpp 5.5 KB

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