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