fix doc example typo
[boost.git] / boost / interprocess / containers / container / detail / destroyers.hpp
blobc16139b179df819c7c2f137fc78c0bfecc35add1
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2009.
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/container for documentation.
11 //////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_CONTAINERS_DESTROYERS_HPP
14 #define BOOST_CONTAINERS_DESTROYERS_HPP
16 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
17 # pragma once
18 #endif
20 #include <boost/interprocess/containers/container/detail/config_begin.hpp>
21 #include <boost/interprocess/containers/container/detail/workaround.hpp>
22 #include <boost/interprocess/containers/container/detail/version_type.hpp>
23 #include <boost/interprocess/containers/container/detail/utilities.hpp>
25 namespace boost {
26 namespace interprocess_container {
27 namespace containers_detail {
29 //!A deleter for scoped_ptr that deallocates the memory
30 //!allocated for an array of objects using a STL allocator.
31 template <class Allocator>
32 struct scoped_array_deallocator
34 typedef typename Allocator::pointer pointer;
35 typedef typename Allocator::size_type size_type;
37 scoped_array_deallocator(pointer p, Allocator& a, size_type length)
38 : m_ptr(p), m_alloc(a), m_length(length) {}
40 ~scoped_array_deallocator()
41 { if (m_ptr) m_alloc.deallocate(m_ptr, m_length); }
43 void release()
44 { m_ptr = 0; }
46 private:
47 pointer m_ptr;
48 Allocator& m_alloc;
49 size_type m_length;
52 template <class Allocator>
53 struct null_scoped_array_deallocator
55 typedef typename Allocator::pointer pointer;
56 typedef typename Allocator::size_type size_type;
58 null_scoped_array_deallocator(pointer, Allocator&, size_type)
61 void release()
66 //!A deleter for scoped_ptr that destroys
67 //!an object using a STL allocator.
68 template <class Allocator>
69 struct scoped_destructor_n
71 typedef typename Allocator::pointer pointer;
72 typedef typename Allocator::value_type value_type;
73 typedef typename Allocator::size_type size_type;
75 pointer m_p;
76 size_type m_n;
78 scoped_destructor_n(pointer p, size_type n)
79 : m_p(p), m_n(n)
82 void release()
83 { m_p = 0; }
85 void increment_size(size_type inc)
86 { m_n += inc; }
88 ~scoped_destructor_n()
90 if(!m_p) return;
91 value_type *raw_ptr = containers_detail::get_pointer(m_p);
92 for(std::size_t i = 0; i < m_n; ++i, ++raw_ptr)
93 raw_ptr->~value_type();
97 //!A deleter for scoped_ptr that destroys
98 //!an object using a STL allocator.
99 template <class Allocator>
100 struct null_scoped_destructor_n
102 typedef typename Allocator::pointer pointer;
103 typedef typename Allocator::size_type size_type;
105 null_scoped_destructor_n(pointer, size_type)
108 void increment_size(size_type)
111 void release()
115 template <class A>
116 class allocator_destroyer
118 typedef typename A::value_type value_type;
119 typedef containers_detail::integral_constant<unsigned,
120 boost::interprocess_container::containers_detail::
121 version<A>::value> alloc_version;
122 typedef containers_detail::integral_constant<unsigned, 1> allocator_v1;
123 typedef containers_detail::integral_constant<unsigned, 2> allocator_v2;
125 private:
126 A & a_;
128 private:
129 void priv_deallocate(const typename A::pointer &p, allocator_v1)
130 { a_.deallocate(p, 1); }
132 void priv_deallocate(const typename A::pointer &p, allocator_v2)
133 { a_.deallocate_one(p); }
135 public:
136 allocator_destroyer(A &a)
137 : a_(a)
140 void operator()(const typename A::pointer &p)
142 containers_detail::get_pointer(p)->~value_type();
143 priv_deallocate(p, alloc_version());
148 } //namespace containers_detail {
149 } //namespace interprocess_container {
150 } //namespace boost {
152 #include <boost/interprocess/containers/container/detail/config_end.hpp>
154 #endif //#ifndef BOOST_CONTAINERS_DESTROYERS_HPP