1 //
2 // generic/basic_endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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 
23 namespace boost {
24 namespace asio {
25 namespace 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  */
42 template <typename Protocol>
43 class basic_endpoint
44 {
45 public:
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.
basic_endpoint()58   basic_endpoint() BOOST_ASIO_NOEXCEPT
59   {
60   }
61 
62   /// Construct an endpoint from the specified socket address.
basic_endpoint(const void * socket_address,std::size_t socket_address_size,int socket_protocol=0)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>
basic_endpoint(const Endpoint & endpoint)71   basic_endpoint(const Endpoint& endpoint)
72     : impl_(endpoint.data(), endpoint.size(), endpoint.protocol().protocol())
73   {
74   }
75 
76   /// Copy constructor.
basic_endpoint(const basic_endpoint & other)77   basic_endpoint(const basic_endpoint& other)
78     : impl_(other.impl_)
79   {
80   }
81 
82 #if defined(BOOST_ASIO_HAS_MOVE)
83   /// Move constructor.
basic_endpoint(basic_endpoint && other)84   basic_endpoint(basic_endpoint&& other)
85     : impl_(other.impl_)
86   {
87   }
88 #endif // defined(BOOST_ASIO_HAS_MOVE)
89 
90   /// Assign from another endpoint.
operator =(const basic_endpoint & other)91   basic_endpoint& operator=(const basic_endpoint& other)
92   {
93     impl_ = other.impl_;
94     return *this;
95   }
96 
97 #if defined(BOOST_ASIO_HAS_MOVE)
98   /// Move-assign from another endpoint.
operator =(basic_endpoint && other)99   basic_endpoint& operator=(basic_endpoint&& other)
100   {
101     impl_ = other.impl_;
102     return *this;
103   }
104 #endif // defined(BOOST_ASIO_HAS_MOVE)
105 
106   /// The protocol associated with the endpoint.
protocol() const107   protocol_type protocol() const
108   {
109     return protocol_type(impl_.family(), impl_.protocol());
110   }
111 
112   /// Get the underlying endpoint in the native type.
data()113   data_type* data()
114   {
115     return impl_.data();
116   }
117 
118   /// Get the underlying endpoint in the native type.
data() const119   const data_type* data() const
120   {
121     return impl_.data();
122   }
123 
124   /// Get the underlying size of the endpoint in the native type.
size() const125   std::size_t size() const
126   {
127     return impl_.size();
128   }
129 
130   /// Set the underlying size of the endpoint in the native type.
resize(std::size_t new_size)131   void resize(std::size_t new_size)
132   {
133     impl_.resize(new_size);
134   }
135 
136   /// Get the capacity of the endpoint in the native type.
capacity() const137   std::size_t capacity() const
138   {
139     return impl_.capacity();
140   }
141 
142   /// Compare two endpoints for equality.
operator ==(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)143   friend bool operator==(const basic_endpoint<Protocol>& e1,
144       const basic_endpoint<Protocol>& e2)
145   {
146     return e1.impl_ == e2.impl_;
147   }
148 
149   /// Compare two endpoints for inequality.
operator !=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)150   friend bool operator!=(const basic_endpoint<Protocol>& e1,
151       const basic_endpoint<Protocol>& e2)
152   {
153     return !(e1.impl_ == e2.impl_);
154   }
155 
156   /// Compare endpoints for ordering.
operator <(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)157   friend bool operator<(const basic_endpoint<Protocol>& e1,
158       const basic_endpoint<Protocol>& e2)
159   {
160     return e1.impl_ < e2.impl_;
161   }
162 
163   /// Compare endpoints for ordering.
operator >(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)164   friend bool operator>(const basic_endpoint<Protocol>& e1,
165       const basic_endpoint<Protocol>& e2)
166   {
167     return e2.impl_ < e1.impl_;
168   }
169 
170   /// Compare endpoints for ordering.
operator <=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)171   friend bool operator<=(const basic_endpoint<Protocol>& e1,
172       const basic_endpoint<Protocol>& e2)
173   {
174     return !(e2 < e1);
175   }
176 
177   /// Compare endpoints for ordering.
operator >=(const basic_endpoint<Protocol> & e1,const basic_endpoint<Protocol> & e2)178   friend bool operator>=(const basic_endpoint<Protocol>& e1,
179       const basic_endpoint<Protocol>& e2)
180   {
181     return !(e1 < e2);
182   }
183 
184 private:
185   // The underlying generic endpoint.
186   boost::asio::generic::detail::endpoint impl_;
187 };
188 
189 } // namespace generic
190 } // namespace asio
191 } // namespace boost
192 
193 #include <boost/asio/detail/pop_options.hpp>
194 
195 #endif // BOOST_ASIO_GENERIC_BASIC_ENDPOINT_HPP
196