1 | #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED |
---|
2 | #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED |
---|
3 | |
---|
4 | // MS compatible compilers support #pragma once |
---|
5 | |
---|
6 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
7 | # pragma once |
---|
8 | #endif |
---|
9 | |
---|
10 | // |
---|
11 | // detail/sp_counted_impl.hpp |
---|
12 | // |
---|
13 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. |
---|
14 | // Copyright 2004-2005 Peter Dimov |
---|
15 | // |
---|
16 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
17 | // accompanying file LICENSE_1_0.txt or copy at |
---|
18 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
19 | // |
---|
20 | |
---|
21 | #include <boost/config.hpp> |
---|
22 | |
---|
23 | #if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR) |
---|
24 | # error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible. |
---|
25 | #endif |
---|
26 | |
---|
27 | #include <boost/checked_delete.hpp> |
---|
28 | #include <boost/smart_ptr/detail/sp_counted_base.hpp> |
---|
29 | |
---|
30 | #if defined(BOOST_SP_USE_QUICK_ALLOCATOR) |
---|
31 | #include <boost/smart_ptr/detail/quick_allocator.hpp> |
---|
32 | #endif |
---|
33 | |
---|
34 | #if defined(BOOST_SP_USE_STD_ALLOCATOR) |
---|
35 | #include <memory> // std::allocator |
---|
36 | #endif |
---|
37 | |
---|
38 | #include <cstddef> // std::size_t |
---|
39 | |
---|
40 | namespace boost |
---|
41 | { |
---|
42 | |
---|
43 | #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) |
---|
44 | |
---|
45 | void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn ); |
---|
46 | void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn ); |
---|
47 | |
---|
48 | #endif |
---|
49 | |
---|
50 | namespace detail |
---|
51 | { |
---|
52 | |
---|
53 | template<class X> class sp_counted_impl_p: public sp_counted_base |
---|
54 | { |
---|
55 | private: |
---|
56 | |
---|
57 | X * px_; |
---|
58 | |
---|
59 | sp_counted_impl_p( sp_counted_impl_p const & ); |
---|
60 | sp_counted_impl_p & operator= ( sp_counted_impl_p const & ); |
---|
61 | |
---|
62 | typedef sp_counted_impl_p<X> this_type; |
---|
63 | |
---|
64 | public: |
---|
65 | |
---|
66 | explicit sp_counted_impl_p( X * px ): px_( px ) |
---|
67 | { |
---|
68 | #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) |
---|
69 | boost::sp_scalar_constructor_hook( px, sizeof(X), this ); |
---|
70 | #endif |
---|
71 | } |
---|
72 | |
---|
73 | virtual void dispose() // nothrow |
---|
74 | { |
---|
75 | #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) |
---|
76 | boost::sp_scalar_destructor_hook( px_, sizeof(X), this ); |
---|
77 | #endif |
---|
78 | boost::checked_delete( px_ ); |
---|
79 | } |
---|
80 | |
---|
81 | virtual void * get_deleter( detail::sp_typeinfo const & ) |
---|
82 | { |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | #if defined(BOOST_SP_USE_STD_ALLOCATOR) |
---|
87 | |
---|
88 | void * operator new( std::size_t ) |
---|
89 | { |
---|
90 | return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) ); |
---|
91 | } |
---|
92 | |
---|
93 | void operator delete( void * p ) |
---|
94 | { |
---|
95 | std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 ); |
---|
96 | } |
---|
97 | |
---|
98 | #endif |
---|
99 | |
---|
100 | #if defined(BOOST_SP_USE_QUICK_ALLOCATOR) |
---|
101 | |
---|
102 | void * operator new( std::size_t ) |
---|
103 | { |
---|
104 | return quick_allocator<this_type>::alloc(); |
---|
105 | } |
---|
106 | |
---|
107 | void operator delete( void * p ) |
---|
108 | { |
---|
109 | quick_allocator<this_type>::dealloc( p ); |
---|
110 | } |
---|
111 | |
---|
112 | #endif |
---|
113 | }; |
---|
114 | |
---|
115 | // |
---|
116 | // Borland's Codeguard trips up over the -Vx- option here: |
---|
117 | // |
---|
118 | #ifdef __CODEGUARD__ |
---|
119 | # pragma option push -Vx- |
---|
120 | #endif |
---|
121 | |
---|
122 | template<class P, class D> class sp_counted_impl_pd: public sp_counted_base |
---|
123 | { |
---|
124 | private: |
---|
125 | |
---|
126 | P ptr; // copy constructor must not throw |
---|
127 | D del; // copy constructor must not throw |
---|
128 | |
---|
129 | sp_counted_impl_pd( sp_counted_impl_pd const & ); |
---|
130 | sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & ); |
---|
131 | |
---|
132 | typedef sp_counted_impl_pd<P, D> this_type; |
---|
133 | |
---|
134 | public: |
---|
135 | |
---|
136 | // pre: d(p) must not throw |
---|
137 | |
---|
138 | sp_counted_impl_pd( P p, D d ): ptr(p), del(d) |
---|
139 | { |
---|
140 | } |
---|
141 | |
---|
142 | virtual void dispose() // nothrow |
---|
143 | { |
---|
144 | del( ptr ); |
---|
145 | } |
---|
146 | |
---|
147 | virtual void * get_deleter( detail::sp_typeinfo const & ti ) |
---|
148 | { |
---|
149 | return ti == BOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0; |
---|
150 | } |
---|
151 | |
---|
152 | #if defined(BOOST_SP_USE_STD_ALLOCATOR) |
---|
153 | |
---|
154 | void * operator new( std::size_t ) |
---|
155 | { |
---|
156 | return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) ); |
---|
157 | } |
---|
158 | |
---|
159 | void operator delete( void * p ) |
---|
160 | { |
---|
161 | std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 ); |
---|
162 | } |
---|
163 | |
---|
164 | #endif |
---|
165 | |
---|
166 | #if defined(BOOST_SP_USE_QUICK_ALLOCATOR) |
---|
167 | |
---|
168 | void * operator new( std::size_t ) |
---|
169 | { |
---|
170 | return quick_allocator<this_type>::alloc(); |
---|
171 | } |
---|
172 | |
---|
173 | void operator delete( void * p ) |
---|
174 | { |
---|
175 | quick_allocator<this_type>::dealloc( p ); |
---|
176 | } |
---|
177 | |
---|
178 | #endif |
---|
179 | }; |
---|
180 | |
---|
181 | template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base |
---|
182 | { |
---|
183 | private: |
---|
184 | |
---|
185 | P p_; // copy constructor must not throw |
---|
186 | D d_; // copy constructor must not throw |
---|
187 | A a_; // copy constructor must not throw |
---|
188 | |
---|
189 | sp_counted_impl_pda( sp_counted_impl_pda const & ); |
---|
190 | sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & ); |
---|
191 | |
---|
192 | typedef sp_counted_impl_pda<P, D, A> this_type; |
---|
193 | |
---|
194 | public: |
---|
195 | |
---|
196 | // pre: d( p ) must not throw |
---|
197 | |
---|
198 | sp_counted_impl_pda( P p, D d, A a ): p_( p ), d_( d ), a_( a ) |
---|
199 | { |
---|
200 | } |
---|
201 | |
---|
202 | virtual void dispose() // nothrow |
---|
203 | { |
---|
204 | d_( p_ ); |
---|
205 | } |
---|
206 | |
---|
207 | virtual void destroy() // nothrow |
---|
208 | { |
---|
209 | typedef typename A::template rebind< this_type >::other A2; |
---|
210 | |
---|
211 | A2 a2( a_ ); |
---|
212 | |
---|
213 | this->~this_type(); |
---|
214 | a2.deallocate( this, 1 ); |
---|
215 | } |
---|
216 | |
---|
217 | virtual void * get_deleter( detail::sp_typeinfo const & ti ) |
---|
218 | { |
---|
219 | return ti == BOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0; |
---|
220 | } |
---|
221 | }; |
---|
222 | |
---|
223 | #ifdef __CODEGUARD__ |
---|
224 | # pragma option pop |
---|
225 | #endif |
---|
226 | |
---|
227 | } // namespace detail |
---|
228 | |
---|
229 | } // namespace boost |
---|
230 | |
---|
231 | #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED |
---|