1//
2// generic/basic_endpoint.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6//
7// Distributed under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9//
10
11#ifndef BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP
12#define BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/config.hpp>
19#include <boost/asio/generic/detail/endpoint.hpp>
20
21#include <boost/asio/detail/push_options.hpp>
22
23namespace boost {
24namespace asio {
25namespace generic {
26
27/// Describes an endpoint for any socket type.
28/**
29 * The boost::asio::generic::basic_endpoint class template describes an endpoint
30 * that may be associated with any socket type.
31 *
32 * @note The socket types sockaddr type must be able to fit into a
33 * @c sockaddr_storage structure.
34 *
35 * @par Thread Safety
36 * @e Distinct @e objects: Safe.@n
37 * @e Shared @e objects: Unsafe.
38 *
39 * @par Concepts:
40 * Endpoint.
41 */
42template <typename Protocol>
43class basic_endpoint
44{
45public:
46 /// The protocol type associated with the endpoint.
47 typedef Protocol protocol_type;
48
49 /// The type of the endpoint structure. This type is dependent on the
50 /// underlying implementation of the socket layer.
51#if defined(GENERATING_DOCUMENTATION)
52 typedef implementation_defined data_type;
53#else
54 typedef boost::asio::detail::socket_addr_type data_type;
55#endif
56
57 /// Default constructor.
58 basic_endpoint() noexcept
59 {
60 }
61
62 /// Construct an endpoint from the specified socket address.
63 basic_endpoint(const void* socket_address,
64 std::size_t socket_address_size, int socket_protocol = 0)
65 : impl_(socket_address, socket_address_size, socket_protocol)
66 {
67 }
68
69 /// Construct an endpoint from the specific endpoint type.
70 template <typename Endpoint>
71 basic_endpoint(const Endpoint& endpoint)
72 : impl_(endpoint.data(), endpoint.size(), endpoint.protocol().protocol())
73 {
74 }
75
76 /// Copy constructor.
77 basic_endpoint(const basic_endpoint& other)
78 : impl_(other.impl_)
79 {
80 }
81
82 /// Move constructor.
83 basic_endpoint(basic_endpoint&& other)
84 : impl_(other.impl_)
85 {
86 }
87
88 /// Assign from another endpoint.
89 basic_endpoint& operator=(const basic_endpoint& other)
90 {
91 impl_ = other.impl_;
92 return *this;
93 }
94
95 /// Move-assign from another endpoint.
96 basic_endpoint& operator=(basic_endpoint&& other)
97 {
98 impl_ = other.impl_;
99 return *this;
100 }
101
102 /// The protocol associated with the endpoint.
103 protocol_type protocol() const
104 {
105 return protocol_type(impl_.family(), impl_.protocol());
106 }
107
108 /// Get the underlying endpoint in the native type.
109 data_type* data()
110 {
111 return impl_.data();
112 }
113
114 /// Get the underlying endpoint in the native type.
115 const data_type* data() const
116 {
117 return impl_.data();
118 }
119
120 /// Get the underlying size of the endpoint in the native type.
121 std::size_t size() const
122 {
123 return impl_.size();
124 }
125
126 /// Set the underlying size of the endpoint in the native type.
127 void resize(std::size_t new_size)
128 {
129 impl_.resize(new_size);
130 }
131
132 /// Get the capacity of the endpoint in the native type.
133 std::size_t capacity() const
134 {
135 return impl_.capacity();
136 }
137
138 /// Compare two endpoints for equality.
139 friend bool operator==(const basic_endpoint<Protocol>& e1,
140 const basic_endpoint<Protocol>& e2)
141 {
142 return e1.impl_ == e2.impl_;
143 }
144
145 /// Compare two endpoints for inequality.
146 friend bool operator!=(const basic_endpoint<Protocol>& e1,
147 const basic_endpoint<Protocol>& e2)
148 {
149 return !(e1.impl_ == e2.impl_);
150 }
151
152 /// Compare endpoints for ordering.
153 friend bool operator<(const basic_endpoint<Protocol>& e1,
154 const basic_endpoint<Protocol>& e2)
155 {
156 return e1.impl_ < e2.impl_;
157 }
158
159 /// Compare endpoints for ordering.
160 friend bool operator>(const basic_endpoint<Protocol>& e1,
161 const basic_endpoint<Protocol>& e2)
162 {
163 return e2.impl_ < e1.impl_;
164 }
165
166 /// Compare endpoints for ordering.
167 friend bool operator<=(const basic_endpoint<Protocol>& e1,
168 const basic_endpoint<Protocol>& e2)
169 {
170 return !(e2 < e1);
171 }
172
173 /// Compare endpoints for ordering.
174 friend bool operator>=(const basic_endpoint<Protocol>& e1,
175 const basic_endpoint<Protocol>& e2)
176 {
177 return !(e1 < e2);
178 }
179
180private:
181 // The underlying generic endpoint.
182 boost::asio::generic::detail::endpoint impl_;
183};
184
185} // namespace generic
186} // namespace asio
187} // namespace boost
188
189#include <boost/asio/detail/pop_options.hpp>
190
191#endif // BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP
192

source code of boost/libs/asio/include/boost/asio/generic/basic_endpoint.hpp