Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / ip / detail / endpoint.hpp
1 //
2 // ip/detail/endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_IP_DETAIL_ENDPOINT_HPP
12 #define BOOST_ASIO_IP_DETAIL_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 <string>
20 #include <boost/asio/detail/socket_types.hpp>
21 #include <boost/asio/detail/winsock_init.hpp>
22 #include <boost/system/error_code.hpp>
23 #include <boost/asio/ip/address.hpp>
24
25 #include <boost/asio/detail/push_options.hpp>
26
27 namespace boost {
28 namespace asio {
29 namespace ip {
30 namespace detail {
31
32 // Helper class for implementating an IP endpoint.
33 class endpoint
34 {
35 public:
36   // Default constructor.
37   BOOST_ASIO_DECL endpoint();
38
39   // Construct an endpoint using a family and port number.
40   BOOST_ASIO_DECL endpoint(int family, unsigned short port_num);
41
42   // Construct an endpoint using an address and port number.
43   BOOST_ASIO_DECL endpoint(const boost::asio::ip::address& addr,
44       unsigned short port_num);
45
46   // Copy constructor.
47   endpoint(const endpoint& other)
48     : data_(other.data_)
49   {
50   }
51
52   // Assign from another endpoint.
53   endpoint& operator=(const endpoint& other)
54   {
55     data_ = other.data_;
56     return *this;
57   }
58
59   // Get the underlying endpoint in the native type.
60   boost::asio::detail::socket_addr_type* data()
61   {
62     return &data_.base;
63   }
64
65   // Get the underlying endpoint in the native type.
66   const boost::asio::detail::socket_addr_type* data() const
67   {
68     return &data_.base;
69   }
70
71   // Get the underlying size of the endpoint in the native type.
72   std::size_t size() const
73   {
74     if (is_v4())
75       return sizeof(boost::asio::detail::sockaddr_in4_type);
76     else
77       return sizeof(boost::asio::detail::sockaddr_in6_type);
78   }
79
80   // Set the underlying size of the endpoint in the native type.
81   BOOST_ASIO_DECL void resize(std::size_t new_size);
82
83   // Get the capacity of the endpoint in the native type.
84   std::size_t capacity() const
85   {
86     return sizeof(data_);
87   }
88
89   // Get the port associated with the endpoint.
90   BOOST_ASIO_DECL unsigned short port() const;
91
92   // Set the port associated with the endpoint.
93   BOOST_ASIO_DECL void port(unsigned short port_num);
94
95   // Get the IP address associated with the endpoint.
96   BOOST_ASIO_DECL boost::asio::ip::address address() const;
97
98   // Set the IP address associated with the endpoint.
99   BOOST_ASIO_DECL void address(const boost::asio::ip::address& addr);
100
101   // Compare two endpoints for equality.
102   BOOST_ASIO_DECL friend bool operator==(
103       const endpoint& e1, const endpoint& e2);
104
105   // Compare endpoints for ordering.
106   BOOST_ASIO_DECL friend bool operator<(
107       const endpoint& e1, const endpoint& e2);
108
109   // Determine whether the endpoint is IPv4.
110   bool is_v4() const
111   {
112     return data_.base.sa_family == BOOST_ASIO_OS_DEF(AF_INET);
113   }
114
115 #if !defined(BOOST_ASIO_NO_IOSTREAM)
116   // Convert to a string.
117   BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
118 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
119
120 private:
121   // The underlying IP socket address.
122   union data_union
123   {
124     boost::asio::detail::socket_addr_type base;
125     boost::asio::detail::sockaddr_in4_type v4;
126     boost::asio::detail::sockaddr_in6_type v6;
127   } data_;
128 };
129
130 } // namespace detail
131 } // namespace ip
132 } // namespace asio
133 } // namespace boost
134
135 #include <boost/asio/detail/pop_options.hpp>
136
137 #if defined(BOOST_ASIO_HEADER_ONLY)
138 # include <boost/asio/ip/detail/impl/endpoint.ipp>
139 #endif // defined(BOOST_ASIO_HEADER_ONLY)
140
141 #endif // BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP