• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // local/basic_endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 
12 #ifndef ASIO_LOCAL_BASIC_ENDPOINT_HPP
13 #define ASIO_LOCAL_BASIC_ENDPOINT_HPP
14 
15 
16 #include "asio/detail/config.hpp"
17 
18 
19 #include "asio/local/detail/endpoint.hpp"
20 
21 
22 #include "asio/detail/push_options.hpp"
23 
24 namespace asio {
25 namespace local {
26 
27 /// Describes an endpoint for a UNIX socket.
28 /**
29  * The 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 
46   /// The type of the endpoint structure. This type is dependent on the
47   /// underlying implementation of the socket layer.
48   typedef asio::detail::socket_addr_type data_type;
49 
50   /// Default constructor.
basic_endpoint()51   basic_endpoint()
52   {
53   }
54 
55   /// Construct an endpoint using the specified path name.
basic_endpoint(const char * path_name)56   basic_endpoint(const char* path_name)
57     : impl_(path_name)
58   {
59   }
60 
61   /// Construct an endpoint using the specified path name.
basic_endpoint(const std::string & path_name)62   basic_endpoint(const std::string& path_name)
63     : impl_(path_name)
64   {
65   }
66 
67   /// Copy constructor.
basic_endpoint(const basic_endpoint & other)68   basic_endpoint(const basic_endpoint& other)
69     : impl_(other.impl_)
70   {
71   }
72 
73   /// Move constructor.
basic_endpoint(basic_endpoint && other)74   basic_endpoint(basic_endpoint&& other)
75     : impl_(other.impl_)
76   {
77   }
78 
79   /// Assign from another endpoint.
operator =(const basic_endpoint & other)80   basic_endpoint& operator=(const basic_endpoint& other)
81   {
82     impl_ = other.impl_;
83     return *this;
84   }
85 
86   /// Move-assign from another endpoint.
operator =(basic_endpoint && other)87   basic_endpoint& operator=(basic_endpoint&& other)
88   {
89     impl_ = other.impl_;
90     return *this;
91   }
92 
93   /// The protocol associated with the endpoint.
protocol() const94   protocol_type protocol() const
95   {
96     return protocol_type();
97   }
98 
99   /// Get the underlying endpoint in the native type.
data()100   data_type* data()
101   {
102     return impl_.data();
103   }
104 
105   /// Get the underlying endpoint in the native type.
data() const106   const data_type* data() const
107   {
108     return impl_.data();
109   }
110 
111   /// Get the underlying size of the endpoint in the native type.
size() const112   std::size_t size() const
113   {
114     return impl_.size();
115   }
116 
117   /// Set the underlying size of the endpoint in the native type.
resize(std::size_t new_size)118   void resize(std::size_t new_size)
119   {
120     impl_.resize(new_size);
121   }
122 
123   /// Get the capacity of the endpoint in the native type.
capacity() const124   std::size_t capacity() const
125   {
126     return impl_.capacity();
127   }
128 
129   /// Get the path associated with the endpoint.
path() const130   std::string path() const
131   {
132     return impl_.path();
133   }
134 
135   /// Set the path associated with the endpoint.
path(const char * p)136   void path(const char* p)
137   {
138     impl_.path(p);
139   }
140 
141   /// Set the path associated with the endpoint.
path(const std::string & p)142   void path(const std::string& p)
143   {
144     impl_.path(p);
145   }
146 
147   /// Compare two endpoints for equality.
operator ==(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)148   friend bool operator==(const basic_endpoint<Protocol>& e1,
149       const basic_endpoint<Protocol>& e2)
150   {
151     return e1.impl_ == e2.impl_;
152   }
153 
154   /// Compare two endpoints for inequality.
operator !=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)155   friend bool operator!=(const basic_endpoint<Protocol>& e1,
156       const basic_endpoint<Protocol>& e2)
157   {
158     return !(e1.impl_ == e2.impl_);
159   }
160 
161   /// Compare endpoints for ordering.
operator <(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)162   friend bool operator<(const basic_endpoint<Protocol>& e1,
163       const basic_endpoint<Protocol>& e2)
164   {
165     return e1.impl_ < e2.impl_;
166   }
167 
168   /// Compare endpoints for ordering.
operator >(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)169   friend bool operator>(const basic_endpoint<Protocol>& e1,
170       const basic_endpoint<Protocol>& e2)
171   {
172     return e2.impl_ < e1.impl_;
173   }
174 
175   /// Compare endpoints for ordering.
operator <=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)176   friend bool operator<=(const basic_endpoint<Protocol>& e1,
177       const basic_endpoint<Protocol>& e2)
178   {
179     return !(e2 < e1);
180   }
181 
182   /// Compare endpoints for ordering.
operator >=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)183   friend bool operator>=(const basic_endpoint<Protocol>& e1,
184       const basic_endpoint<Protocol>& e2)
185   {
186     return !(e1 < e2);
187   }
188 
189 private:
190   // The underlying UNIX domain endpoint.
191   asio::local::detail::endpoint impl_;
192 };
193 
194 /// Output an endpoint as a string.
195 /**
196  * Used to output a human-readable string for a specified endpoint.
197  *
198  * @param os The output stream to which the string will be written.
199  *
200  * @param endpoint The endpoint to be written.
201  *
202  * @return The output stream.
203  *
204  * @relates asio::local::basic_endpoint
205  */
206 template <typename Elem, typename Traits, typename Protocol>
operator <<(std::basic_ostream<Elem,Traits> & os,const basic_endpoint<Protocol> & endpoint)207 std::basic_ostream<Elem, Traits>& operator<<(
208     std::basic_ostream<Elem, Traits>& os,
209     const basic_endpoint<Protocol>& endpoint)
210 {
211   os << endpoint.path();
212   return os;
213 }
214 
215 } // namespace local
216 } // namespace asio
217 
218 #include "asio/detail/pop_options.hpp"
219 
220        //   || defined(GENERATING_DOCUMENTATION)
221 
222 #endif // ASIO_LOCAL_BASIC_ENDPOINT_HPP
223