basic_endpoint.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // local/basic_endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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(BOOST_NO_IOSTREAM)
  21. # include <iosfwd>
  22. #endif // !defined(BOOST_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()
  53. {
  54. }
  55. /// Construct an endpoint using the specified path name.
  56. basic_endpoint(const char* path)
  57. : impl_(path)
  58. {
  59. }
  60. /// Construct an endpoint using the specified path name.
  61. basic_endpoint(const std::string& path)
  62. : impl_(path)
  63. {
  64. }
  65. /// Copy constructor.
  66. basic_endpoint(const basic_endpoint& other)
  67. : impl_(other.impl_)
  68. {
  69. }
  70. /// Assign from another endpoint.
  71. basic_endpoint& operator=(const basic_endpoint& other)
  72. {
  73. impl_ = other.impl_;
  74. return *this;
  75. }
  76. /// The protocol associated with the endpoint.
  77. protocol_type protocol() const
  78. {
  79. return protocol_type();
  80. }
  81. /// Get the underlying endpoint in the native type.
  82. data_type* data()
  83. {
  84. return impl_.data();
  85. }
  86. /// Get the underlying endpoint in the native type.
  87. const data_type* data() const
  88. {
  89. return impl_.data();
  90. }
  91. /// Get the underlying size of the endpoint in the native type.
  92. std::size_t size() const
  93. {
  94. return impl_.size();
  95. }
  96. /// Set the underlying size of the endpoint in the native type.
  97. void resize(std::size_t size)
  98. {
  99. impl_.resize(size);
  100. }
  101. /// Get the capacity of the endpoint in the native type.
  102. std::size_t capacity() const
  103. {
  104. return impl_.capacity();
  105. }
  106. /// Get the path associated with the endpoint.
  107. std::string path() const
  108. {
  109. return impl_.path();
  110. }
  111. /// Set the path associated with the endpoint.
  112. void path(const char* p)
  113. {
  114. impl_.path(p);
  115. }
  116. /// Set the path associated with the endpoint.
  117. void path(const std::string& p)
  118. {
  119. impl_.path(p);
  120. }
  121. /// Compare two endpoints for equality.
  122. friend bool operator==(const basic_endpoint<Protocol>& e1,
  123. const basic_endpoint<Protocol>& e2)
  124. {
  125. return e1.impl_ == e2.impl_;
  126. }
  127. /// Compare two endpoints for inequality.
  128. friend bool operator!=(const basic_endpoint<Protocol>& e1,
  129. const basic_endpoint<Protocol>& e2)
  130. {
  131. return !(e1.impl_ == e2.impl_);
  132. }
  133. /// Compare endpoints for ordering.
  134. friend bool operator<(const basic_endpoint<Protocol>& e1,
  135. const basic_endpoint<Protocol>& e2)
  136. {
  137. return e1.impl_ < e2.impl_;
  138. }
  139. /// Compare endpoints for ordering.
  140. friend bool operator>(const basic_endpoint<Protocol>& e1,
  141. const basic_endpoint<Protocol>& e2)
  142. {
  143. return e2.impl_ < e1.impl_;
  144. }
  145. /// Compare endpoints for ordering.
  146. friend bool operator<=(const basic_endpoint<Protocol>& e1,
  147. const basic_endpoint<Protocol>& e2)
  148. {
  149. return !(e2 < e1);
  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 < e2);
  156. }
  157. private:
  158. // The underlying UNIX domain endpoint.
  159. asio::local::detail::endpoint impl_;
  160. };
  161. /// Output an endpoint as a string.
  162. /**
  163. * Used to output a human-readable string for a specified endpoint.
  164. *
  165. * @param os The output stream to which the string will be written.
  166. *
  167. * @param endpoint The endpoint to be written.
  168. *
  169. * @return The output stream.
  170. *
  171. * @relates asio::local::basic_endpoint
  172. */
  173. template <typename Elem, typename Traits, typename Protocol>
  174. std::basic_ostream<Elem, Traits>& operator<<(
  175. std::basic_ostream<Elem, Traits>& os,
  176. const basic_endpoint<Protocol>& endpoint)
  177. {
  178. os << endpoint.path();
  179. return os;
  180. }
  181. } // namespace local
  182. } // namespace asio
  183. #include "asio/detail/pop_options.hpp"
  184. #endif // defined(ASIO_HAS_LOCAL_SOCKETS)
  185. // || defined(GENERATING_DOCUMENTATION)
  186. #endif // ASIO_LOCAL_BASIC_ENDPOINT_HPP