2 // ip/basic_endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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.
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)
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, unsigned short port_num)
91 : impl_(addr, port_num)
96 basic_endpoint(const basic_endpoint& other)
101 #if defined(BOOST_ASIO_HAS_MOVE)
102 /// Move constructor.
103 basic_endpoint(basic_endpoint&& other)
107 #endif // defined(BOOST_ASIO_HAS_MOVE)
109 /// Assign from another endpoint.
110 basic_endpoint& operator=(const basic_endpoint& other)
116 #if defined(BOOST_ASIO_HAS_MOVE)
117 /// Move-assign from another endpoint.
118 basic_endpoint& operator=(basic_endpoint&& other)
123 #endif // defined(BOOST_ASIO_HAS_MOVE)
125 /// The protocol associated with the endpoint.
126 protocol_type protocol() const
129 return InternetProtocol::v4();
130 return InternetProtocol::v6();
133 /// Get the underlying endpoint in the native type.
139 /// Get the underlying endpoint in the native type.
140 const data_type* data() const
145 /// Get the underlying size of the endpoint in the native type.
146 std::size_t size() const
151 /// Set the underlying size of the endpoint in the native type.
152 void resize(std::size_t new_size)
154 impl_.resize(new_size);
157 /// Get the capacity of the endpoint in the native type.
158 std::size_t capacity() const
160 return impl_.capacity();
163 /// Get the port associated with the endpoint. The port number is always in
164 /// the host's byte order.
165 unsigned short port() const
170 /// Set the port associated with the endpoint. The port number is always in
171 /// the host's byte order.
172 void port(unsigned short port_num)
174 impl_.port(port_num);
177 /// Get the IP address associated with the endpoint.
178 boost::asio::ip::address address() const
180 return impl_.address();
183 /// Set the IP address associated with the endpoint.
184 void address(const boost::asio::ip::address& addr)
189 /// Compare two endpoints for equality.
190 friend bool operator==(const basic_endpoint<InternetProtocol>& e1,
191 const basic_endpoint<InternetProtocol>& e2)
193 return e1.impl_ == e2.impl_;
196 /// Compare two endpoints for inequality.
197 friend bool operator!=(const basic_endpoint<InternetProtocol>& e1,
198 const basic_endpoint<InternetProtocol>& e2)
203 /// Compare endpoints for ordering.
204 friend bool operator<(const basic_endpoint<InternetProtocol>& e1,
205 const basic_endpoint<InternetProtocol>& e2)
207 return e1.impl_ < e2.impl_;
210 /// Compare endpoints for ordering.
211 friend bool operator>(const basic_endpoint<InternetProtocol>& e1,
212 const basic_endpoint<InternetProtocol>& e2)
214 return e2.impl_ < e1.impl_;
217 /// Compare endpoints for ordering.
218 friend bool operator<=(const basic_endpoint<InternetProtocol>& e1,
219 const basic_endpoint<InternetProtocol>& e2)
224 /// Compare endpoints for ordering.
225 friend bool operator>=(const basic_endpoint<InternetProtocol>& e1,
226 const basic_endpoint<InternetProtocol>& e2)
232 // The underlying IP endpoint.
233 boost::asio::ip::detail::endpoint impl_;
236 #if !defined(BOOST_ASIO_NO_IOSTREAM)
238 /// Output an endpoint as a string.
240 * Used to output a human-readable string for a specified endpoint.
242 * @param os The output stream to which the string will be written.
244 * @param endpoint The endpoint to be written.
246 * @return The output stream.
248 * @relates boost::asio::ip::basic_endpoint
250 template <typename Elem, typename Traits, typename InternetProtocol>
251 std::basic_ostream<Elem, Traits>& operator<<(
252 std::basic_ostream<Elem, Traits>& os,
253 const basic_endpoint<InternetProtocol>& endpoint);
255 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
261 #include <boost/asio/detail/pop_options.hpp>
263 #include <boost/asio/ip/impl/basic_endpoint.hpp>
265 #endif // BOOST_ASIO_IP_BASIC_ENDPOINT_HPP