5
// Copyright (c) 2003-2008 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 ASIO_DETAIL_SOCKET_OPTION_HPP
12
#define ASIO_DETAIL_SOCKET_OPTION_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
23
#include <boost/config.hpp>
24
#include "asio/detail/pop_options.hpp"
26
#include "asio/detail/socket_types.hpp"
30
namespace socket_option {
32
// Helper template for implementing boolean-based options.
33
template <int Level, int Name>
37
// Default constructor.
43
// Construct with a specific option value.
44
explicit boolean(bool v)
49
// Set the current value of the boolean.
50
boolean& operator=(bool v)
56
// Get the current value of the boolean.
69
bool operator!() const
74
// Get the level of the socket option.
75
template <typename Protocol>
76
int level(const Protocol&) const
81
// Get the name of the socket option.
82
template <typename Protocol>
83
int name(const Protocol&) const
88
// Get the address of the boolean data.
89
template <typename Protocol>
90
int* data(const Protocol&)
95
// Get the address of the boolean data.
96
template <typename Protocol>
97
const int* data(const Protocol&) const
102
// Get the size of the boolean data.
103
template <typename Protocol>
104
std::size_t size(const Protocol&) const
106
return sizeof(value_);
109
// Set the size of the boolean data.
110
template <typename Protocol>
111
void resize(const Protocol&, std::size_t s)
113
// On some platforms (e.g. Windows Vista), the getsockopt function will
114
// return the size of a boolean socket option as one byte, even though a
115
// four byte integer was passed in.
119
value_ = *reinterpret_cast<char*>(&value_) ? 1 : 0;
124
throw std::length_error("boolean socket option resize");
132
// Helper template for implementing integer options.
133
template <int Level, int Name>
137
// Default constructor.
143
// Construct with a specific option value.
144
explicit integer(int v)
149
// Set the value of the int option.
150
integer& operator=(int v)
156
// Get the current value of the int option.
162
// Get the level of the socket option.
163
template <typename Protocol>
164
int level(const Protocol&) const
169
// Get the name of the socket option.
170
template <typename Protocol>
171
int name(const Protocol&) const
176
// Get the address of the int data.
177
template <typename Protocol>
178
int* data(const Protocol&)
183
// Get the address of the int data.
184
template <typename Protocol>
185
const int* data(const Protocol&) const
190
// Get the size of the int data.
191
template <typename Protocol>
192
std::size_t size(const Protocol&) const
194
return sizeof(value_);
197
// Set the size of the int data.
198
template <typename Protocol>
199
void resize(const Protocol&, std::size_t s)
201
if (s != sizeof(value_))
202
throw std::length_error("integer socket option resize");
209
// Helper template for implementing linger options.
210
template <int Level, int Name>
214
// Default constructor.
221
// Construct with specific option values.
222
linger(bool e, int t)
228
// Set the value for whether linger is enabled.
229
void enabled(bool value)
231
value_.l_onoff = value ? 1 : 0;
234
// Get the value for whether linger is enabled.
237
return value_.l_onoff != 0;
240
// Set the value for the linger timeout.
241
void timeout(int value)
244
value_.l_linger = static_cast<u_short>(value);
246
value_.l_linger = value;
250
// Get the value for the linger timeout.
253
return static_cast<int>(value_.l_linger);
256
// Get the level of the socket option.
257
template <typename Protocol>
258
int level(const Protocol&) const
263
// Get the name of the socket option.
264
template <typename Protocol>
265
int name(const Protocol&) const
270
// Get the address of the linger data.
271
template <typename Protocol>
272
::linger* data(const Protocol&)
277
// Get the address of the linger data.
278
template <typename Protocol>
279
const ::linger* data(const Protocol&) const
284
// Get the size of the linger data.
285
template <typename Protocol>
286
std::size_t size(const Protocol&) const
288
return sizeof(value_);
291
// Set the size of the int data.
292
template <typename Protocol>
293
void resize(const Protocol&, std::size_t s)
295
if (s != sizeof(value_))
296
throw std::length_error("linger socket option resize");
303
} // namespace socket_option
304
} // namespace detail
307
#include "asio/detail/pop_options.hpp"
309
#endif // ASIO_DETAIL_SOCKET_OPTION_HPP