~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/detail/socket_option.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// socket_option.hpp
3
 
// ~~~~~~~~~~~~~~~~~
4
 
//
5
 
// Copyright (c) 2003-2008 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 ASIO_DETAIL_SOCKET_OPTION_HPP
12
 
#define ASIO_DETAIL_SOCKET_OPTION_HPP
13
 
 
14
 
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15
 
# pragma once
16
 
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
 
 
18
 
#include "asio/detail/push_options.hpp"
19
 
 
20
 
#include "asio/detail/push_options.hpp"
21
 
#include <cstddef>
22
 
#include <stdexcept>
23
 
#include <boost/config.hpp>
24
 
#include "asio/detail/pop_options.hpp"
25
 
 
26
 
#include "asio/detail/socket_types.hpp"
27
 
 
28
 
namespace asio {
29
 
namespace detail {
30
 
namespace socket_option {
31
 
 
32
 
// Helper template for implementing boolean-based options.
33
 
template <int Level, int Name>
34
 
class boolean
35
 
{
36
 
public:
37
 
  // Default constructor.
38
 
  boolean()
39
 
    : value_(0)
40
 
  {
41
 
  }
42
 
 
43
 
  // Construct with a specific option value.
44
 
  explicit boolean(bool v)
45
 
    : value_(v ? 1 : 0)
46
 
  {
47
 
  }
48
 
 
49
 
  // Set the current value of the boolean.
50
 
  boolean& operator=(bool v)
51
 
  {
52
 
    value_ = v ? 1 : 0;
53
 
    return *this;
54
 
  }
55
 
 
56
 
  // Get the current value of the boolean.
57
 
  bool value() const
58
 
  {
59
 
    return !!value_;
60
 
  }
61
 
 
62
 
  // Convert to bool.
63
 
  operator bool() const
64
 
  {
65
 
    return !!value_;
66
 
  }
67
 
 
68
 
  // Test for false.
69
 
  bool operator!() const
70
 
  {
71
 
    return !value_;
72
 
  }
73
 
 
74
 
  // Get the level of the socket option.
75
 
  template <typename Protocol>
76
 
  int level(const Protocol&) const
77
 
  {
78
 
    return Level;
79
 
  }
80
 
 
81
 
  // Get the name of the socket option.
82
 
  template <typename Protocol>
83
 
  int name(const Protocol&) const
84
 
  {
85
 
    return Name;
86
 
  }
87
 
 
88
 
  // Get the address of the boolean data.
89
 
  template <typename Protocol>
90
 
  int* data(const Protocol&)
91
 
  {
92
 
    return &value_;
93
 
  }
94
 
 
95
 
  // Get the address of the boolean data.
96
 
  template <typename Protocol>
97
 
  const int* data(const Protocol&) const
98
 
  {
99
 
    return &value_;
100
 
  }
101
 
 
102
 
  // Get the size of the boolean data.
103
 
  template <typename Protocol>
104
 
  std::size_t size(const Protocol&) const
105
 
  {
106
 
    return sizeof(value_);
107
 
  }
108
 
 
109
 
  // Set the size of the boolean data.
110
 
  template <typename Protocol>
111
 
  void resize(const Protocol&, std::size_t s)
112
 
  {
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.
116
 
    switch (s)
117
 
    {
118
 
    case sizeof(char):
119
 
      value_ = *reinterpret_cast<char*>(&value_) ? 1 : 0;
120
 
      break;
121
 
    case sizeof(value_):
122
 
      break;
123
 
    default:
124
 
      throw std::length_error("boolean socket option resize");
125
 
    }
126
 
  }
127
 
 
128
 
private:
129
 
  int value_;
130
 
};
131
 
 
132
 
// Helper template for implementing integer options.
133
 
template <int Level, int Name>
134
 
class integer
135
 
{
136
 
public:
137
 
  // Default constructor.
138
 
  integer()
139
 
    : value_(0)
140
 
  {
141
 
  }
142
 
 
143
 
  // Construct with a specific option value.
144
 
  explicit integer(int v)
145
 
    : value_(v)
146
 
  {
147
 
  }
148
 
 
149
 
  // Set the value of the int option.
150
 
  integer& operator=(int v)
151
 
  {
152
 
    value_ = v;
153
 
    return *this;
154
 
  }
155
 
 
156
 
  // Get the current value of the int option.
157
 
  int value() const
158
 
  {
159
 
    return value_;
160
 
  }
161
 
 
162
 
  // Get the level of the socket option.
163
 
  template <typename Protocol>
164
 
  int level(const Protocol&) const
165
 
  {
166
 
    return Level;
167
 
  }
168
 
 
169
 
  // Get the name of the socket option.
170
 
  template <typename Protocol>
171
 
  int name(const Protocol&) const
172
 
  {
173
 
    return Name;
174
 
  }
175
 
 
176
 
  // Get the address of the int data.
177
 
  template <typename Protocol>
178
 
  int* data(const Protocol&)
179
 
  {
180
 
    return &value_;
181
 
  }
182
 
 
183
 
  // Get the address of the int data.
184
 
  template <typename Protocol>
185
 
  const int* data(const Protocol&) const
186
 
  {
187
 
    return &value_;
188
 
  }
189
 
 
190
 
  // Get the size of the int data.
191
 
  template <typename Protocol>
192
 
  std::size_t size(const Protocol&) const
193
 
  {
194
 
    return sizeof(value_);
195
 
  }
196
 
 
197
 
  // Set the size of the int data.
198
 
  template <typename Protocol>
199
 
  void resize(const Protocol&, std::size_t s)
200
 
  {
201
 
    if (s != sizeof(value_))
202
 
      throw std::length_error("integer socket option resize");
203
 
  }
204
 
 
205
 
private:
206
 
  int value_;
207
 
};
208
 
 
209
 
// Helper template for implementing linger options.
210
 
template <int Level, int Name>
211
 
class linger
212
 
{
213
 
public:
214
 
  // Default constructor.
215
 
  linger()
216
 
  {
217
 
    value_.l_onoff = 0;
218
 
    value_.l_linger = 0;
219
 
  }
220
 
 
221
 
  // Construct with specific option values.
222
 
  linger(bool e, int t)
223
 
  {
224
 
    enabled(e);
225
 
    timeout(t);
226
 
  }
227
 
 
228
 
  // Set the value for whether linger is enabled.
229
 
  void enabled(bool value)
230
 
  {
231
 
    value_.l_onoff = value ? 1 : 0;
232
 
  }
233
 
 
234
 
  // Get the value for whether linger is enabled.
235
 
  bool enabled() const
236
 
  {
237
 
    return value_.l_onoff != 0;
238
 
  }
239
 
 
240
 
  // Set the value for the linger timeout.
241
 
  void timeout(int value)
242
 
  {
243
 
#if defined(WIN32)
244
 
    value_.l_linger = static_cast<u_short>(value);
245
 
#else
246
 
    value_.l_linger = value;
247
 
#endif
248
 
  }
249
 
 
250
 
  // Get the value for the linger timeout.
251
 
  int timeout() const
252
 
  {
253
 
    return static_cast<int>(value_.l_linger);
254
 
  }
255
 
 
256
 
  // Get the level of the socket option.
257
 
  template <typename Protocol>
258
 
  int level(const Protocol&) const
259
 
  {
260
 
    return Level;
261
 
  }
262
 
 
263
 
  // Get the name of the socket option.
264
 
  template <typename Protocol>
265
 
  int name(const Protocol&) const
266
 
  {
267
 
    return Name;
268
 
  }
269
 
 
270
 
  // Get the address of the linger data.
271
 
  template <typename Protocol>
272
 
  ::linger* data(const Protocol&)
273
 
  {
274
 
    return &value_;
275
 
  }
276
 
 
277
 
  // Get the address of the linger data.
278
 
  template <typename Protocol>
279
 
  const ::linger* data(const Protocol&) const
280
 
  {
281
 
    return &value_;
282
 
  }
283
 
 
284
 
  // Get the size of the linger data.
285
 
  template <typename Protocol>
286
 
  std::size_t size(const Protocol&) const
287
 
  {
288
 
    return sizeof(value_);
289
 
  }
290
 
 
291
 
  // Set the size of the int data.
292
 
  template <typename Protocol>
293
 
  void resize(const Protocol&, std::size_t s)
294
 
  {
295
 
    if (s != sizeof(value_))
296
 
      throw std::length_error("linger socket option resize");
297
 
  }
298
 
 
299
 
private:
300
 
  ::linger value_;
301
 
};
302
 
 
303
 
} // namespace socket_option
304
 
} // namespace detail
305
 
} // namespace asio
306
 
 
307
 
#include "asio/detail/pop_options.hpp"
308
 
 
309
 
#endif // ASIO_DETAIL_SOCKET_OPTION_HPP