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

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/detail/handler_alloc_helpers.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
 
// handler_alloc_helpers.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_HANDLER_ALLOC_HELPERS_HPP
12
 
#define ASIO_DETAIL_HANDLER_ALLOC_HELPERS_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 <boost/detail/workaround.hpp>
22
 
#include "asio/detail/pop_options.hpp"
23
 
 
24
 
#include "asio/handler_alloc_hook.hpp"
25
 
#include "asio/detail/noncopyable.hpp"
26
 
 
27
 
// Calls to asio_handler_allocate and asio_handler_deallocate must be made from
28
 
// a namespace that does not contain any overloads of these functions. The
29
 
// asio_handler_alloc_helpers namespace is defined here for that purpose.
30
 
namespace asio_handler_alloc_helpers {
31
 
 
32
 
template <typename Handler>
33
 
inline void* allocate(std::size_t s, Handler* h)
34
 
{
35
 
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
36
 
  return ::operator new(s);
37
 
#else
38
 
  using namespace asio;
39
 
  return asio_handler_allocate(s, h);
40
 
#endif
41
 
}
42
 
 
43
 
template <typename Handler>
44
 
inline void deallocate(void* p, std::size_t s, Handler* h)
45
 
{
46
 
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
47
 
  ::operator delete(p);
48
 
#else
49
 
  using namespace asio;
50
 
  asio_handler_deallocate(p, s, h);
51
 
#endif
52
 
}
53
 
 
54
 
} // namespace asio_handler_alloc_helpers
55
 
 
56
 
namespace asio {
57
 
namespace detail {
58
 
 
59
 
// Traits for handler allocation.
60
 
template <typename Handler, typename Object>
61
 
struct handler_alloc_traits
62
 
{
63
 
  typedef Handler handler_type;
64
 
  typedef Object value_type;
65
 
  typedef Object* pointer_type;
66
 
  BOOST_STATIC_CONSTANT(std::size_t, value_size = sizeof(Object));
67
 
};
68
 
 
69
 
template <typename Alloc_Traits>
70
 
class handler_ptr;
71
 
 
72
 
// Helper class to provide RAII on uninitialised handler memory.
73
 
template <typename Alloc_Traits>
74
 
class raw_handler_ptr
75
 
  : private noncopyable
76
 
{
77
 
public:
78
 
  typedef typename Alloc_Traits::handler_type handler_type;
79
 
  typedef typename Alloc_Traits::value_type value_type;
80
 
  typedef typename Alloc_Traits::pointer_type pointer_type;
81
 
  BOOST_STATIC_CONSTANT(std::size_t, value_size = Alloc_Traits::value_size);
82
 
 
83
 
  // Constructor allocates the memory.
84
 
  raw_handler_ptr(handler_type& handler)
85
 
    : handler_(handler),
86
 
      pointer_(static_cast<pointer_type>(
87
 
            asio_handler_alloc_helpers::allocate(value_size, &handler_)))
88
 
  {
89
 
  }
90
 
 
91
 
  // Destructor automatically deallocates memory, unless it has been stolen by
92
 
  // a handler_ptr object.
93
 
  ~raw_handler_ptr()
94
 
  {
95
 
    if (pointer_)
96
 
      asio_handler_alloc_helpers::deallocate(
97
 
          pointer_, value_size, &handler_);
98
 
  }
99
 
 
100
 
private:
101
 
  friend class handler_ptr<Alloc_Traits>;
102
 
  handler_type& handler_;
103
 
  pointer_type pointer_;
104
 
};
105
 
 
106
 
// Helper class to provide RAII on uninitialised handler memory.
107
 
template <typename Alloc_Traits>
108
 
class handler_ptr
109
 
  : private noncopyable
110
 
{
111
 
public:
112
 
  typedef typename Alloc_Traits::handler_type handler_type;
113
 
  typedef typename Alloc_Traits::value_type value_type;
114
 
  typedef typename Alloc_Traits::pointer_type pointer_type;
115
 
  BOOST_STATIC_CONSTANT(std::size_t, value_size = Alloc_Traits::value_size);
116
 
  typedef raw_handler_ptr<Alloc_Traits> raw_ptr_type;
117
 
 
118
 
  // Take ownership of existing memory.
119
 
  handler_ptr(handler_type& handler, pointer_type pointer)
120
 
    : handler_(handler),
121
 
      pointer_(pointer)
122
 
  {
123
 
  }
124
 
 
125
 
  // Construct object in raw memory and take ownership if construction succeeds.
126
 
  handler_ptr(raw_ptr_type& raw_ptr)
127
 
    : handler_(raw_ptr.handler_),
128
 
      pointer_(new (raw_ptr.pointer_) value_type)
129
 
  {
130
 
    raw_ptr.pointer_ = 0;
131
 
  }
132
 
 
133
 
  // Construct object in raw memory and take ownership if construction succeeds.
134
 
  template <typename Arg1>
135
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1)
136
 
    : handler_(raw_ptr.handler_),
137
 
      pointer_(new (raw_ptr.pointer_) value_type(a1))
138
 
  {
139
 
    raw_ptr.pointer_ = 0;
140
 
  }
141
 
 
142
 
  // Construct object in raw memory and take ownership if construction succeeds.
143
 
  template <typename Arg1, typename Arg2>
144
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2)
145
 
    : handler_(raw_ptr.handler_),
146
 
      pointer_(new (raw_ptr.pointer_) value_type(a1, a2))
147
 
  {
148
 
    raw_ptr.pointer_ = 0;
149
 
  }
150
 
 
151
 
  // Construct object in raw memory and take ownership if construction succeeds.
152
 
  template <typename Arg1, typename Arg2, typename Arg3>
153
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3)
154
 
    : handler_(raw_ptr.handler_),
155
 
      pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3))
156
 
  {
157
 
    raw_ptr.pointer_ = 0;
158
 
  }
159
 
 
160
 
  // Construct object in raw memory and take ownership if construction succeeds.
161
 
  template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
162
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4)
163
 
    : handler_(raw_ptr.handler_),
164
 
      pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4))
165
 
  {
166
 
    raw_ptr.pointer_ = 0;
167
 
  }
168
 
 
169
 
  // Construct object in raw memory and take ownership if construction succeeds.
170
 
  template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
171
 
      typename Arg5>
172
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
173
 
      Arg5& a5)
174
 
    : handler_(raw_ptr.handler_),
175
 
      pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4, a5))
176
 
  {
177
 
    raw_ptr.pointer_ = 0;
178
 
  }
179
 
 
180
 
  // Construct object in raw memory and take ownership if construction succeeds.
181
 
  template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
182
 
      typename Arg5, typename Arg6>
183
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
184
 
      Arg5& a5, Arg6& a6)
185
 
    : handler_(raw_ptr.handler_),
186
 
      pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4, a5, a6))
187
 
  {
188
 
    raw_ptr.pointer_ = 0;
189
 
  }
190
 
 
191
 
  // Construct object in raw memory and take ownership if construction succeeds.
192
 
  template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
193
 
      typename Arg5, typename Arg6, typename Arg7>
194
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
195
 
      Arg5& a5, Arg6& a6, Arg7& a7)
196
 
    : handler_(raw_ptr.handler_),
197
 
      pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4, a5, a6, a7))
198
 
  {
199
 
    raw_ptr.pointer_ = 0;
200
 
  }
201
 
 
202
 
  // Construct object in raw memory and take ownership if construction succeeds.
203
 
  template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
204
 
      typename Arg5, typename Arg6, typename Arg7, typename Arg8>
205
 
  handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
206
 
      Arg5& a5, Arg6& a6, Arg7& a7, Arg8& a8)
207
 
    : handler_(raw_ptr.handler_),
208
 
      pointer_(new (raw_ptr.pointer_) value_type(
209
 
            a1, a2, a3, a4, a5, a6, a7, a8))
210
 
  {
211
 
    raw_ptr.pointer_ = 0;
212
 
  }
213
 
 
214
 
  // Destructor automatically deallocates memory, unless it has been released.
215
 
  ~handler_ptr()
216
 
  {
217
 
    reset();
218
 
  }
219
 
 
220
 
  // Get the memory.
221
 
  pointer_type get() const
222
 
  {
223
 
    return pointer_;
224
 
  }
225
 
 
226
 
  // Release ownership of the memory.
227
 
  pointer_type release()
228
 
  {
229
 
    pointer_type tmp = pointer_;
230
 
    pointer_ = 0;
231
 
    return tmp;
232
 
  }
233
 
 
234
 
  // Explicitly destroy and deallocate the memory.
235
 
  void reset()
236
 
  {
237
 
    if (pointer_)
238
 
    {
239
 
      pointer_->value_type::~value_type();
240
 
      asio_handler_alloc_helpers::deallocate(
241
 
          pointer_, value_size, &handler_);
242
 
      pointer_ = 0;
243
 
    }
244
 
  }
245
 
 
246
 
private:
247
 
  handler_type& handler_;
248
 
  pointer_type pointer_;
249
 
};
250
 
 
251
 
} // namespace detail
252
 
} // namespace asio
253
 
 
254
 
#include "asio/detail/pop_options.hpp"
255
 
 
256
 
#endif // ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP