basic_endpoint.hpp 5.6 KB

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