2 // ip/basic_endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IP_BASIC_ENDPOINT_HPP
12 #define BOOST_ASIO_IP_BASIC_ENDPOINT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/ip/address.hpp>
20 #include <boost/asio/ip/detail/endpoint.hpp>
22 #if !defined(BOOST_ASIO_NO_IOSTREAM)
24 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
26 #include <boost/asio/detail/push_options.hpp>
32 /// Describes an endpoint for a version-independent IP socket.
34 * The boost::asio::ip::basic_endpoint class template describes an endpoint that
35 * may be associated with a particular socket.
38 * @e Distinct @e objects: Safe.@n
39 * @e Shared @e objects: Unsafe.
44 template <typename InternetProtocol>
48 /// The protocol type associated with the endpoint.
49 typedef InternetProtocol protocol_type;
51 /// The type of the endpoint structure. This type is dependent on the
52 /// underlying implementation of the socket layer.
53 #if defined(GENERATING_DOCUMENTATION)
54 typedef implementation_defined data_type;
56 typedef boost::asio::detail::socket_addr_type data_type;
59 /// Default constructor.
60 basic_endpoint() BOOST_ASIO_NOEXCEPT
65 /// Construct an endpoint using a port number, specified in the host's byte
66 /// order. The IP address will be the any address (i.e. INADDR_ANY or
67 /// in6addr_any). This constructor would typically be used for accepting new
71 * To initialise an IPv4 TCP endpoint for port 1234, use:
73 * boost::asio::ip::tcp::endpoint ep(boost::asio::ip::tcp::v4(), 1234);
76 * To specify an IPv6 UDP endpoint for port 9876, use:
78 * boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v6(), 9876);
81 basic_endpoint(const InternetProtocol& internet_protocol,
82 unsigned short port_num) BOOST_ASIO_NOEXCEPT
83 : impl_(internet_protocol.family(), port_num)
87 /// Construct an endpoint using a port number and an IP address. This
88 /// constructor may be used for accepting connections on a specific interface
89 /// or for making a connection to a remote endpoint.
90 basic_endpoint(const boost::asio::ip::address& addr,
91 unsigned short port_num) BOOST_ASIO_NOEXCEPT
92 : impl_(addr, port_num)
97 basic_endpoint(const basic_endpoint& other) BOOST_ASIO_NOEXCEPT
102 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
103 /// Move constructor.
104 basic_endpoint(basic_endpoint&& other) BOOST_ASIO_NOEXCEPT
108 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
110 /// Assign from another endpoint.
111 basic_endpoint& operator=(const basic_endpoint& other) BOOST_ASIO_NOEXCEPT
117 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
118 /// Move-assign from another endpoint.
119 basic_endpoint& operator=(basic_endpoint&& other) BOOST_ASIO_NOEXCEPT
124 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
126 /// The protocol associated with the endpoint.
127 protocol_type protocol() const BOOST_ASIO_NOEXCEPT
130 return InternetProtocol::v4();
131 return InternetProtocol::v6();
134 /// Get the underlying endpoint in the native type.
135 data_type* data() BOOST_ASIO_NOEXCEPT
140 /// Get the underlying endpoint in the native type.
141 const data_type* data() const BOOST_ASIO_NOEXCEPT
146 /// Get the underlying size of the endpoint in the native type.
147 std::size_t size() const BOOST_ASIO_NOEXCEPT
152 /// Set the underlying size of the endpoint in the native type.
153 void resize(std::size_t new_size)
155 impl_.resize(new_size);
158 /// Get the capacity of the endpoint in the native type.
159 std::size_t capacity() const BOOST_ASIO_NOEXCEPT
161 return impl_.capacity();
164 /// Get the port associated with the endpoint. The port number is always in
165 /// the host's byte order.
166 unsigned short port() const BOOST_ASIO_NOEXCEPT
171 /// Set the port associated with the endpoint. The port number is always in
172 /// the host's byte order.
173 void port(unsigned short port_num) BOOST_ASIO_NOEXCEPT
175 impl_.port(port_num);
178 /// Get the IP address associated with the endpoint.
179 boost::asio::ip::address address() const BOOST_ASIO_NOEXCEPT
181 return impl_.address();
184 /// Set the IP address associated with the endpoint.
185 void address(const boost::asio::ip::address& addr) BOOST_ASIO_NOEXCEPT
190 /// Compare two endpoints for equality.
191 friend bool operator==(const basic_endpoint<InternetProtocol>& e1,
192 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT
194 return e1.impl_ == e2.impl_;
197 /// Compare two endpoints for inequality.
198 friend bool operator!=(const basic_endpoint<InternetProtocol>& e1,
199 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT
204 /// Compare endpoints for ordering.
205 friend bool operator<(const basic_endpoint<InternetProtocol>& e1,
206 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT
208 return e1.impl_ < e2.impl_;
211 /// Compare endpoints for ordering.
212 friend bool operator>(const basic_endpoint<InternetProtocol>& e1,
213 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT
215 return e2.impl_ < e1.impl_;
218 /// Compare endpoints for ordering.
219 friend bool operator<=(const basic_endpoint<InternetProtocol>& e1,
220 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT
225 /// Compare endpoints for ordering.
226 friend bool operator>=(const basic_endpoint<InternetProtocol>& e1,
227 const basic_endpoint<InternetProtocol>& e2) BOOST_ASIO_NOEXCEPT
233 // The underlying IP endpoint.
234 boost::asio::ip::detail::endpoint impl_;
237 #if !defined(BOOST_ASIO_NO_IOSTREAM)
239 /// Output an endpoint as a string.
241 * Used to output a human-readable string for a specified endpoint.
243 * @param os The output stream to which the string will be written.
245 * @param endpoint The endpoint to be written.
247 * @return The output stream.
249 * @relates boost::asio::ip::basic_endpoint
251 template <typename Elem, typename Traits, typename InternetProtocol>
252 std::basic_ostream<Elem, Traits>& operator<<(
253 std::basic_ostream<Elem, Traits>& os,
254 const basic_endpoint<InternetProtocol>& endpoint);
256 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
262 #include <boost/asio/detail/pop_options.hpp>
264 #include <boost/asio/ip/impl/basic_endpoint.hpp>
266 #endif // BOOST_ASIO_IP_BASIC_ENDPOINT_HPP