2 // detail/socket_option.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_DETAIL_SOCKET_OPTION_HPP
12 #define BOOST_ASIO_DETAIL_SOCKET_OPTION_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
21 #include <boost/asio/detail/socket_types.hpp>
22 #include <boost/asio/detail/throw_exception.hpp>
24 #include <boost/asio/detail/push_options.hpp>
29 namespace socket_option {
31 // Helper template for implementing boolean-based options.
32 template <int Level, int Name>
36 // Default constructor.
42 // Construct with a specific option value.
43 explicit boolean(bool v)
48 // Set the current value of the boolean.
49 boolean& operator=(bool v)
55 // Get the current value of the boolean.
68 bool operator!() const
73 // Get the level of the socket option.
74 template <typename Protocol>
75 int level(const Protocol&) const
80 // Get the name of the socket option.
81 template <typename Protocol>
82 int name(const Protocol&) const
87 // Get the address of the boolean data.
88 template <typename Protocol>
89 int* data(const Protocol&)
94 // Get the address of the boolean data.
95 template <typename Protocol>
96 const int* data(const Protocol&) const
101 // Get the size of the boolean data.
102 template <typename Protocol>
103 std::size_t size(const Protocol&) const
105 return sizeof(value_);
108 // Set the size of the boolean data.
109 template <typename Protocol>
110 void resize(const Protocol&, std::size_t s)
112 // On some platforms (e.g. Windows Vista), the getsockopt function will
113 // return the size of a boolean socket option as one byte, even though a
114 // four byte integer was passed in.
118 value_ = *reinterpret_cast<char*>(&value_) ? 1 : 0;
124 std::length_error ex("boolean socket option resize");
125 boost::asio::detail::throw_exception(ex);
134 // Helper template for implementing integer options.
135 template <int Level, int Name>
139 // Default constructor.
145 // Construct with a specific option value.
146 explicit integer(int v)
151 // Set the value of the int option.
152 integer& operator=(int v)
158 // Get the current value of the int option.
164 // Get the level of the socket option.
165 template <typename Protocol>
166 int level(const Protocol&) const
171 // Get the name of the socket option.
172 template <typename Protocol>
173 int name(const Protocol&) const
178 // Get the address of the int data.
179 template <typename Protocol>
180 int* data(const Protocol&)
185 // Get the address of the int data.
186 template <typename Protocol>
187 const int* data(const Protocol&) const
192 // Get the size of the int data.
193 template <typename Protocol>
194 std::size_t size(const Protocol&) const
196 return sizeof(value_);
199 // Set the size of the int data.
200 template <typename Protocol>
201 void resize(const Protocol&, std::size_t s)
203 if (s != sizeof(value_))
205 std::length_error ex("integer socket option resize");
206 boost::asio::detail::throw_exception(ex);
214 // Helper template for implementing linger options.
215 template <int Level, int Name>
219 // Default constructor.
226 // Construct with specific option values.
227 linger(bool e, int t)
230 timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION(t);
233 // Set the value for whether linger is enabled.
234 void enabled(bool value)
236 value_.l_onoff = value ? 1 : 0;
239 // Get the value for whether linger is enabled.
242 return value_.l_onoff != 0;
245 // Set the value for the linger timeout.
246 void timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION(int value)
249 value_.l_linger = static_cast<u_short>(value);
251 value_.l_linger = value;
255 // Get the value for the linger timeout.
256 int timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION() const
258 return static_cast<int>(value_.l_linger);
261 // Get the level of the socket option.
262 template <typename Protocol>
263 int level(const Protocol&) const
268 // Get the name of the socket option.
269 template <typename Protocol>
270 int name(const Protocol&) const
275 // Get the address of the linger data.
276 template <typename Protocol>
277 detail::linger_type* data(const Protocol&)
282 // Get the address of the linger data.
283 template <typename Protocol>
284 const detail::linger_type* data(const Protocol&) const
289 // Get the size of the linger data.
290 template <typename Protocol>
291 std::size_t size(const Protocol&) const
293 return sizeof(value_);
296 // Set the size of the int data.
297 template <typename Protocol>
298 void resize(const Protocol&, std::size_t s)
300 if (s != sizeof(value_))
302 std::length_error ex("linger socket option resize");
303 boost::asio::detail::throw_exception(ex);
308 detail::linger_type value_;
311 } // namespace socket_option
312 } // namespace detail
316 #include <boost/asio/detail/pop_options.hpp>
318 #endif // BOOST_ASIO_DETAIL_SOCKET_OPTION_HPP