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