blob: 38af73b1c3c493e9e0a9593d3e493eb4440a3720 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
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
Jeff Thompson2277ce52013-08-01 17:34:11 -070021#include <ndnboost/config.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070022
Jeff Thompson3d613fd2013-10-15 15:39:04 -070023#if defined(NDNBOOST_SP_USE_STD_ALLOCATOR) && defined(NDNBOOST_SP_USE_QUICK_ALLOCATOR)
24# error NDNBOOST_SP_USE_STD_ALLOCATOR and NDNBOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
Jeff Thompsonf7d49942013-08-01 16:47:40 -070025#endif
26
Jeff Thompson2277ce52013-08-01 17:34:11 -070027#include <ndnboost/checked_delete.hpp>
28#include <ndnboost/smart_ptr/detail/sp_counted_base.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070029
Jeff Thompson3d613fd2013-10-15 15:39:04 -070030#if defined(NDNBOOST_SP_USE_QUICK_ALLOCATOR)
Jeff Thompson2277ce52013-08-01 17:34:11 -070031#include <ndnboost/smart_ptr/detail/quick_allocator.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070032#endif
33
Jeff Thompson3d613fd2013-10-15 15:39:04 -070034#if defined(NDNBOOST_SP_USE_STD_ALLOCATOR)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070035#include <memory> // std::allocator
36#endif
37
38#include <cstddef> // std::size_t
39
40namespace ndnboost
41{
42
Jeff Thompson3d613fd2013-10-15 15:39:04 -070043#if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070044
45void sp_scalar_constructor_hook( void * px, std::size_t size, void * pn );
46void sp_scalar_destructor_hook( void * px, std::size_t size, void * pn );
47
48#endif
49
50namespace detail
51{
52
53template<class X> class sp_counted_impl_p: public sp_counted_base
54{
55private:
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
64public:
65
66 explicit sp_counted_impl_p( X * px ): px_( px )
67 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070068#if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070069 ndnboost::sp_scalar_constructor_hook( px, sizeof(X), this );
70#endif
71 }
72
73 virtual void dispose() // nothrow
74 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070075#if defined(NDNBOOST_SP_ENABLE_DEBUG_HOOKS)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070076 ndnboost::sp_scalar_destructor_hook( px_, sizeof(X), this );
77#endif
78 ndnboost::checked_delete( px_ );
79 }
80
81 virtual void * get_deleter( detail::sp_typeinfo const & )
82 {
83 return 0;
84 }
85
86 virtual void * get_untyped_deleter()
87 {
88 return 0;
89 }
90
Jeff Thompson3d613fd2013-10-15 15:39:04 -070091#if defined(NDNBOOST_SP_USE_STD_ALLOCATOR)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070092
93 void * operator new( std::size_t )
94 {
95 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
96 }
97
98 void operator delete( void * p )
99 {
100 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
101 }
102
103#endif
104
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700105#if defined(NDNBOOST_SP_USE_QUICK_ALLOCATOR)
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700106
107 void * operator new( std::size_t )
108 {
109 return quick_allocator<this_type>::alloc();
110 }
111
112 void operator delete( void * p )
113 {
114 quick_allocator<this_type>::dealloc( p );
115 }
116
117#endif
118};
119
120//
121// Borland's Codeguard trips up over the -Vx- option here:
122//
123#ifdef __CODEGUARD__
124# pragma option push -Vx-
125#endif
126
127template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
128{
129private:
130
131 P ptr; // copy constructor must not throw
132 D del; // copy constructor must not throw
133
134 sp_counted_impl_pd( sp_counted_impl_pd const & );
135 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
136
137 typedef sp_counted_impl_pd<P, D> this_type;
138
139public:
140
141 // pre: d(p) must not throw
142
143 sp_counted_impl_pd( P p, D & d ): ptr( p ), del( d )
144 {
145 }
146
147 sp_counted_impl_pd( P p ): ptr( p ), del()
148 {
149 }
150
151 virtual void dispose() // nothrow
152 {
153 del( ptr );
154 }
155
156 virtual void * get_deleter( detail::sp_typeinfo const & ti )
157 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700158 return ti == NDNBOOST_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700159 }
160
161 virtual void * get_untyped_deleter()
162 {
163 return &reinterpret_cast<char&>( del );
164 }
165
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700166#if defined(NDNBOOST_SP_USE_STD_ALLOCATOR)
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700167
168 void * operator new( std::size_t )
169 {
170 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
171 }
172
173 void operator delete( void * p )
174 {
175 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
176 }
177
178#endif
179
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700180#if defined(NDNBOOST_SP_USE_QUICK_ALLOCATOR)
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700181
182 void * operator new( std::size_t )
183 {
184 return quick_allocator<this_type>::alloc();
185 }
186
187 void operator delete( void * p )
188 {
189 quick_allocator<this_type>::dealloc( p );
190 }
191
192#endif
193};
194
195template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
196{
197private:
198
199 P p_; // copy constructor must not throw
200 D d_; // copy constructor must not throw
201 A a_; // copy constructor must not throw
202
203 sp_counted_impl_pda( sp_counted_impl_pda const & );
204 sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
205
206 typedef sp_counted_impl_pda<P, D, A> this_type;
207
208public:
209
210 // pre: d( p ) must not throw
211
212 sp_counted_impl_pda( P p, D & d, A a ): p_( p ), d_( d ), a_( a )
213 {
214 }
215
216 sp_counted_impl_pda( P p, A a ): p_( p ), d_(), a_( a )
217 {
218 }
219
220 virtual void dispose() // nothrow
221 {
222 d_( p_ );
223 }
224
225 virtual void destroy() // nothrow
226 {
227 typedef typename A::template rebind< this_type >::other A2;
228
229 A2 a2( a_ );
230
231 this->~this_type();
232 a2.deallocate( this, 1 );
233 }
234
235 virtual void * get_deleter( detail::sp_typeinfo const & ti )
236 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700237 return ti == NDNBOOST_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700238 }
239
240 virtual void * get_untyped_deleter()
241 {
242 return &reinterpret_cast<char&>( d_ );
243 }
244};
245
246#ifdef __CODEGUARD__
247# pragma option pop
248#endif
249
250} // namespace detail
251
252} // namespace ndnboost
253
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700254#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED