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 
12 #ifndef BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
13 #define BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
14 
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 
19 #include <boost/asio/detail/config.hpp>
20 
21 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
22   || defined(GENERATING_DOCUMENTATION)
23 
24 #include <boost/asio/local/detail/endpoint.hpp>
25 
26 #if !defined(BOOST_ASIO_NO_IOSTREAM)
27 # include <iosfwd>
28 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
29 
30 #include <boost/asio/detail/push_options.hpp>
31 
32 namespace boost {
33 namespace asio {
34 namespace local {
35 
36 /// Describes an endpoint for a UNIX socket.
37 /**
38  * The boost::asio::local::basic_endpoint class template describes an endpoint
39  * that may be associated with a particular UNIX socket.
40  *
41  * @par Thread Safety
42  * @e Distinct @e objects: Safe.@n
43  * @e Shared @e objects: Unsafe.
44  *
45  * @par Concepts:
46  * Endpoint.
47  */
48 template <typename Protocol>
49 class basic_endpoint
50 {
51 public:
52   /// The protocol type associated with the endpoint.
53   typedef Protocol protocol_type;
54 
55   /// The type of the endpoint structure. This type is dependent on the
56   /// underlying implementation of the socket layer.
57 #if defined(GENERATING_DOCUMENTATION)
58   typedef implementation_defined data_type;
59 #else
60   typedef boost::asio::detail::socket_addr_type data_type;
61 #endif
62 
63   /// Default constructor.
basic_endpoint()64   basic_endpoint() BOOST_ASIO_NOEXCEPT
65   {
66   }
67 
68   /// Construct an endpoint using the specified path name.
basic_endpoint(const char * path_name)69   basic_endpoint(const char* path_name)
70     : impl_(path_name)
71   {
72   }
73 
74   /// Construct an endpoint using the specified path name.
basic_endpoint(const std::string & path_name)75   basic_endpoint(const std::string& path_name)
76     : impl_(path_name)
77   {
78   }
79 
80   #if defined(BOOST_ASIO_HAS_STRING_VIEW)
81   /// Construct an endpoint using the specified path name.
basic_endpoint(string_view path_name)82   basic_endpoint(string_view path_name)
83     : impl_(path_name)
84   {
85   }
86   #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
87 
88   /// Copy constructor.
basic_endpoint(const basic_endpoint & other)89   basic_endpoint(const basic_endpoint& other)
90     : impl_(other.impl_)
91   {
92   }
93 
94 #if defined(BOOST_ASIO_HAS_MOVE)
95   /// Move constructor.
basic_endpoint(basic_endpoint && other)96   basic_endpoint(basic_endpoint&& other)
97     : impl_(other.impl_)
98   {
99   }
100 #endif // defined(BOOST_ASIO_HAS_MOVE)
101 
102   /// Assign from another endpoint.
operator =(const basic_endpoint & other)103   basic_endpoint& operator=(const basic_endpoint& other)
104   {
105     impl_ = other.impl_;
106     return *this;
107   }
108 
109 #if defined(BOOST_ASIO_HAS_MOVE)
110   /// Move-assign from another endpoint.
operator =(basic_endpoint && other)111   basic_endpoint& operator=(basic_endpoint&& other)
112   {
113     impl_ = other.impl_;
114     return *this;
115   }
116 #endif // defined(BOOST_ASIO_HAS_MOVE)
117 
118   /// The protocol associated with the endpoint.
protocol() const119   protocol_type protocol() const
120   {
121     return protocol_type();
122   }
123 
124   /// Get the underlying endpoint in the native type.
data()125   data_type* data()
126   {
127     return impl_.data();
128   }
129 
130   /// Get the underlying endpoint in the native type.
data() const131   const data_type* data() const
132   {
133     return impl_.data();
134   }
135 
136   /// Get the underlying size of the endpoint in the native type.
size() const137   std::size_t size() const
138   {
139     return impl_.size();
140   }
141 
142   /// Set the underlying size of the endpoint in the native type.
resize(std::size_t new_size)143   void resize(std::size_t new_size)
144   {
145     impl_.resize(new_size);
146   }
147 
148   /// Get the capacity of the endpoint in the native type.
capacity() const149   std::size_t capacity() const
150   {
151     return impl_.capacity();
152   }
153 
154   /// Get the path associated with the endpoint.
path() const155   std::string path() const
156   {
157     return impl_.path();
158   }
159 
160   /// Set the path associated with the endpoint.
path(const char * p)161   void path(const char* p)
162   {
163     impl_.path(p);
164   }
165 
166   /// Set the path associated with the endpoint.
path(const std::string & p)167   void path(const std::string& p)
168   {
169     impl_.path(p);
170   }
171 
172   /// Compare two endpoints for equality.
operator ==(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)173   friend bool operator==(const basic_endpoint<Protocol>& e1,
174       const basic_endpoint<Protocol>& e2)
175   {
176     return e1.impl_ == e2.impl_;
177   }
178 
179   /// Compare two endpoints for inequality.
operator !=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)180   friend bool operator!=(const basic_endpoint<Protocol>& e1,
181       const basic_endpoint<Protocol>& e2)
182   {
183     return !(e1.impl_ == e2.impl_);
184   }
185 
186   /// Compare endpoints for ordering.
operator <(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)187   friend bool operator<(const basic_endpoint<Protocol>& e1,
188       const basic_endpoint<Protocol>& e2)
189   {
190     return e1.impl_ < e2.impl_;
191   }
192 
193   /// Compare endpoints for ordering.
operator >(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)194   friend bool operator>(const basic_endpoint<Protocol>& e1,
195       const basic_endpoint<Protocol>& e2)
196   {
197     return e2.impl_ < e1.impl_;
198   }
199 
200   /// Compare endpoints for ordering.
operator <=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)201   friend bool operator<=(const basic_endpoint<Protocol>& e1,
202       const basic_endpoint<Protocol>& e2)
203   {
204     return !(e2 < e1);
205   }
206 
207   /// Compare endpoints for ordering.
operator >=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)208   friend bool operator>=(const basic_endpoint<Protocol>& e1,
209       const basic_endpoint<Protocol>& e2)
210   {
211     return !(e1 < e2);
212   }
213 
214 private:
215   // The underlying UNIX domain endpoint.
216   boost::asio::local::detail::endpoint impl_;
217 };
218 
219 /// Output an endpoint as a string.
220 /**
221  * Used to output a human-readable string for a specified endpoint.
222  *
223  * @param os The output stream to which the string will be written.
224  *
225  * @param endpoint The endpoint to be written.
226  *
227  * @return The output stream.
228  *
229  * @relates boost::asio::local::basic_endpoint
230  */
231 template <typename Elem, typename Traits, typename Protocol>
operator <<(std::basic_ostream<Elem,Traits> & os,const basic_endpoint<Protocol> & endpoint)232 std::basic_ostream<Elem, Traits>& operator<<(
233     std::basic_ostream<Elem, Traits>& os,
234     const basic_endpoint<Protocol>& endpoint)
235 {
236   os << endpoint.path();
237   return os;
238 }
239 
240 } // namespace local
241 } // namespace asio
242 } // namespace boost
243 
244 #include <boost/asio/detail/pop_options.hpp>
245 
246 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
247        //   || defined(GENERATING_DOCUMENTATION)
248 
249 #endif // BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
250