fix doc example typo
[boost.git] / boost / asio / local / basic_endpoint.hpp
blobdc927b760d50e54d86c4d6c7c8d3c739d03d8a09
1 //
2 // basic_endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Derived from a public domain implementation written by Daniel Casimiro.
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
13 #define BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/asio/detail/push_options.hpp>
22 #include <boost/throw_exception.hpp>
23 #include <cstddef>
24 #include <cstring>
25 #include <ostream>
26 #include <boost/system/system_error.hpp>
27 #include <boost/asio/detail/pop_options.hpp>
29 #include <boost/asio/error.hpp>
30 #include <boost/asio/detail/socket_ops.hpp>
31 #include <boost/asio/detail/socket_types.hpp>
32 #include <boost/asio/detail/throw_error.hpp>
34 #if !defined(BOOST_ASIO_DISABLE_LOCAL_SOCKETS)
35 # if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
36 # define BOOST_ASIO_HAS_LOCAL_SOCKETS 1
37 # endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
38 #endif // !defined(BOOST_ASIO_DISABLE_LOCAL_SOCKETS)
40 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
41 || defined(GENERATING_DOCUMENTATION)
44 namespace boost {
45 namespace asio {
46 namespace local {
48 /// Describes an endpoint for a UNIX socket.
49 /**
50 * The boost::asio::local::basic_endpoint class template describes an endpoint
51 * that may be associated with a particular UNIX socket.
53 * @par Thread Safety
54 * @e Distinct @e objects: Safe.@n
55 * @e Shared @e objects: Unsafe.
57 * @par Concepts:
58 * Endpoint.
60 template <typename Protocol>
61 class basic_endpoint
63 public:
64 /// The protocol type associated with the endpoint.
65 typedef Protocol protocol_type;
67 /// The type of the endpoint structure. This type is dependent on the
68 /// underlying implementation of the socket layer.
69 #if defined(GENERATING_DOCUMENTATION)
70 typedef implementation_defined data_type;
71 #else
72 typedef boost::asio::detail::socket_addr_type data_type;
73 #endif
75 /// Default constructor.
76 basic_endpoint()
78 init("", 0);
81 /// Construct an endpoint using the specified path name.
82 basic_endpoint(const char* path)
84 using namespace std; // For strlen.
85 init(path, strlen(path));
88 /// Construct an endpoint using the specified path name.
89 basic_endpoint(const std::string& path)
91 init(path.data(), path.length());
94 /// Copy constructor.
95 basic_endpoint(const basic_endpoint& other)
96 : data_(other.data_),
97 path_length_(other.path_length_)
101 /// Assign from another endpoint.
102 basic_endpoint& operator=(const basic_endpoint& other)
104 data_ = other.data_;
105 path_length_ = other.path_length_;
106 return *this;
109 /// The protocol associated with the endpoint.
110 protocol_type protocol() const
112 return protocol_type();
115 /// Get the underlying endpoint in the native type.
116 data_type* data()
118 return &data_.base;
121 /// Get the underlying endpoint in the native type.
122 const data_type* data() const
124 return &data_.base;
127 /// Get the underlying size of the endpoint in the native type.
128 std::size_t size() const
130 return path_length_
131 + offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
134 /// Set the underlying size of the endpoint in the native type.
135 void resize(std::size_t size)
137 if (size > sizeof(boost::asio::detail::sockaddr_un_type))
139 boost::system::system_error e(boost::asio::error::invalid_argument);
140 boost::throw_exception(e);
142 else if (size == 0)
144 path_length_ = 0;
146 else
148 path_length_ = size
149 - offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
151 // The path returned by the operating system may be NUL-terminated.
152 if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
153 --path_length_;
157 /// Get the capacity of the endpoint in the native type.
158 std::size_t capacity() const
160 return sizeof(boost::asio::detail::sockaddr_un_type);
163 /// Get the path associated with the endpoint.
164 std::string path() const
166 return std::string(data_.local.sun_path, path_length_);
169 /// Set the path associated with the endpoint.
170 void path(const char* p)
172 using namespace std; // For strlen.
173 init(p, strlen(p));
176 /// Set the path associated with the endpoint.
177 void path(const std::string& p)
179 init(p.data(), p.length());
182 /// Compare two endpoints for equality.
183 friend bool operator==(const basic_endpoint<Protocol>& e1,
184 const basic_endpoint<Protocol>& e2)
186 return e1.path() == e2.path();
189 /// Compare two endpoints for inequality.
190 friend bool operator!=(const basic_endpoint<Protocol>& e1,
191 const basic_endpoint<Protocol>& e2)
193 return e1.path() != e2.path();
196 /// Compare endpoints for ordering.
197 friend bool operator<(const basic_endpoint<Protocol>& e1,
198 const basic_endpoint<Protocol>& e2)
200 return e1.path() < e2.path();
203 private:
204 // The underlying UNIX socket address.
205 union data_union
207 boost::asio::detail::socket_addr_type base;
208 boost::asio::detail::sockaddr_un_type local;
209 } data_;
211 // The length of the path associated with the endpoint.
212 std::size_t path_length_;
214 // Initialise with a specified path.
215 void init(const char* path, std::size_t path_length)
217 if (path_length > sizeof(data_.local.sun_path) - 1)
219 // The buffer is not large enough to store this address.
220 boost::system::error_code ec(boost::asio::error::name_too_long);
221 boost::asio::detail::throw_error(ec);
224 using namespace std; // For memcpy.
225 data_.local = boost::asio::detail::sockaddr_un_type();
226 data_.local.sun_family = AF_UNIX;
227 memcpy(data_.local.sun_path, path, path_length);
228 path_length_ = path_length;
230 // NUL-terminate normal path names. Names that start with a NUL are in the
231 // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
232 if (path_length > 0 && data_.local.sun_path[0] == 0)
233 data_.local.sun_path[path_length] = 0;
237 /// Output an endpoint as a string.
239 * Used to output a human-readable string for a specified endpoint.
241 * @param os The output stream to which the string will be written.
243 * @param endpoint The endpoint to be written.
245 * @return The output stream.
247 * @relates boost::asio::local::basic_endpoint
249 template <typename Elem, typename Traits, typename Protocol>
250 std::basic_ostream<Elem, Traits>& operator<<(
251 std::basic_ostream<Elem, Traits>& os,
252 const basic_endpoint<Protocol>& endpoint)
254 os << endpoint.path();
255 return os;
258 } // namespace local
259 } // namespace asio
260 } // namespace boost
262 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
263 // || defined(GENERATING_DOCUMENTATION)
265 #include <boost/asio/detail/pop_options.hpp>
267 #endif // BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP