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