basic_endpoint.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // local/basic_endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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() BOOST_ASIO_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. #if defined(BOOST_ASIO_HAS_MOVE)
  79. /// Move constructor.
  80. basic_endpoint(basic_endpoint&& other)
  81. : impl_(other.impl_)
  82. {
  83. }
  84. #endif // defined(BOOST_ASIO_HAS_MOVE)
  85. /// Assign from another endpoint.
  86. basic_endpoint& operator=(const basic_endpoint& other)
  87. {
  88. impl_ = other.impl_;
  89. return *this;
  90. }
  91. #if defined(BOOST_ASIO_HAS_MOVE)
  92. /// Move-assign from another endpoint.
  93. basic_endpoint& operator=(basic_endpoint&& other)
  94. {
  95. impl_ = other.impl_;
  96. return *this;
  97. }
  98. #endif // defined(BOOST_ASIO_HAS_MOVE)
  99. /// The protocol associated with the endpoint.
  100. protocol_type protocol() const
  101. {
  102. return protocol_type();
  103. }
  104. /// Get the underlying endpoint in the native type.
  105. data_type* data()
  106. {
  107. return impl_.data();
  108. }
  109. /// Get the underlying endpoint in the native type.
  110. const data_type* data() const
  111. {
  112. return impl_.data();
  113. }
  114. /// Get the underlying size of the endpoint in the native type.
  115. std::size_t size() const
  116. {
  117. return impl_.size();
  118. }
  119. /// Set the underlying size of the endpoint in the native type.
  120. void resize(std::size_t new_size)
  121. {
  122. impl_.resize(new_size);
  123. }
  124. /// Get the capacity of the endpoint in the native type.
  125. std::size_t capacity() const
  126. {
  127. return impl_.capacity();
  128. }
  129. /// Get the path associated with the endpoint.
  130. std::string path() const
  131. {
  132. return impl_.path();
  133. }
  134. /// Set the path associated with the endpoint.
  135. void path(const char* p)
  136. {
  137. impl_.path(p);
  138. }
  139. /// Set the path associated with the endpoint.
  140. void path(const std::string& p)
  141. {
  142. impl_.path(p);
  143. }
  144. /// Compare two endpoints for equality.
  145. friend bool operator==(const basic_endpoint<Protocol>& e1,
  146. const basic_endpoint<Protocol>& e2)
  147. {
  148. return e1.impl_ == e2.impl_;
  149. }
  150. /// Compare two endpoints for inequality.
  151. friend bool operator!=(const basic_endpoint<Protocol>& e1,
  152. const basic_endpoint<Protocol>& e2)
  153. {
  154. return !(e1.impl_ == e2.impl_);
  155. }
  156. /// Compare endpoints for ordering.
  157. friend bool operator<(const basic_endpoint<Protocol>& e1,
  158. const basic_endpoint<Protocol>& e2)
  159. {
  160. return e1.impl_ < e2.impl_;
  161. }
  162. /// Compare endpoints for ordering.
  163. friend bool operator>(const basic_endpoint<Protocol>& e1,
  164. const basic_endpoint<Protocol>& e2)
  165. {
  166. return e2.impl_ < e1.impl_;
  167. }
  168. /// Compare endpoints for ordering.
  169. friend bool operator<=(const basic_endpoint<Protocol>& e1,
  170. const basic_endpoint<Protocol>& e2)
  171. {
  172. return !(e2 < e1);
  173. }
  174. /// Compare endpoints for ordering.
  175. friend bool operator>=(const basic_endpoint<Protocol>& e1,
  176. const basic_endpoint<Protocol>& e2)
  177. {
  178. return !(e1 < e2);
  179. }
  180. private:
  181. // The underlying UNIX domain endpoint.
  182. boost::asio::local::detail::endpoint impl_;
  183. };
  184. /// Output an endpoint as a string.
  185. /**
  186. * Used to output a human-readable string for a specified endpoint.
  187. *
  188. * @param os The output stream to which the string will be written.
  189. *
  190. * @param endpoint The endpoint to be written.
  191. *
  192. * @return The output stream.
  193. *
  194. * @relates boost::asio::local::basic_endpoint
  195. */
  196. template <typename Elem, typename Traits, typename Protocol>
  197. std::basic_ostream<Elem, Traits>& operator<<(
  198. std::basic_ostream<Elem, Traits>& os,
  199. const basic_endpoint<Protocol>& endpoint)
  200. {
  201. os << endpoint.path();
  202. return os;
  203. }
  204. } // namespace local
  205. } // namespace asio
  206. } // namespace boost
  207. #include <boost/asio/detail/pop_options.hpp>
  208. #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  209. // || defined(GENERATING_DOCUMENTATION)
  210. #endif // BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP