CppCMS

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

sp_counted_impl.h
1 #ifndef BOOSTER_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
2 #define BOOSTER_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
3
4 //
5 // detail/sp_counted_impl.hpp
6 //
7 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
8 // Copyright 2004-2005 Peter Dimov
9 //
10 // Distributed under the Boost Software License, Version 1.0. (See
11 // accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
13 //
14
15 #include <booster/config.h>
16
17 #include <booster/checked_delete.h>
18 #include <booster/smart_ptr/sp_counted_base.h>
19
20 #if defined(BOOSTER_SP_USE_STD_ALLOCATOR)
21 #include <booster/auto_ptr_inc.h> // std::allocator
22 #endif
23
24 #include <cstddef> // std::size_t
25
26 namespace booster
27 {
28
29 namespace detail
30 {
31
32 template<class X> class sp_counted_impl_p: public sp_counted_base
33 {
34 private:
35
36 X * px_;
37
38 sp_counted_impl_p( sp_counted_impl_p const & );
39 sp_counted_impl_p & operator= ( sp_counted_impl_p const & );
40
41 typedef sp_counted_impl_p<X> this_type;
42
43 public:
44
45 explicit sp_counted_impl_p( X * px ): px_( px )
46 {
47 }
48
49 virtual void dispose() // nothrow
50 {
51 booster::checked_delete( px_ );
52 }
53
54 virtual void * get_deleter( detail::sp_typeinfo const & )
55 {
56 return 0;
57 }
58
59 void * operator new( std::size_t )
60 {
61 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
62 }
63
64 void operator delete( void * p )
65 {
66 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
67 }
68 };
69
70 //
71 // Borland's Codeguard trips up over the -Vx- option here:
72 //
73 #ifdef __CODEGUARD__
74 # pragma option push -Vx-
75 #endif
76
77 template<class P, class D> class sp_counted_impl_pd: public sp_counted_base
78 {
79 private:
80
81 P ptr; // copy constructor must not throw
82 D del; // copy constructor must not throw
83
84 sp_counted_impl_pd( sp_counted_impl_pd const & );
85 sp_counted_impl_pd & operator= ( sp_counted_impl_pd const & );
86
87 typedef sp_counted_impl_pd<P, D> this_type;
88
89 public:
90
91 // pre: d(p) must not throw
92
93 sp_counted_impl_pd( P p, D d ): ptr(p), del(d)
94 {
95 }
96
97 virtual void dispose() // nothrow
98 {
99 del( ptr );
100 }
101
102 virtual void * get_deleter( detail::sp_typeinfo const & ti )
103 {
104 return ti == BOOSTER_SP_TYPEID(D)? &reinterpret_cast<char&>( del ): 0;
105 }
106
107 void * operator new( std::size_t )
108 {
109 return std::allocator<this_type>().allocate( 1, static_cast<this_type *>(0) );
110 }
111
112 void operator delete( void * p )
113 {
114 std::allocator<this_type>().deallocate( static_cast<this_type *>(p), 1 );
115 }
116
117 };
118
119 template<class P, class D, class A> class sp_counted_impl_pda: public sp_counted_base
120 {
121 private:
122
123 P p_; // copy constructor must not throw
124 D d_; // copy constructor must not throw
125 A a_; // copy constructor must not throw
126
127 sp_counted_impl_pda( sp_counted_impl_pda const & );
128 sp_counted_impl_pda & operator= ( sp_counted_impl_pda const & );
129
130 typedef sp_counted_impl_pda<P, D, A> this_type;
131
132 public:
133
134 // pre: d( p ) must not throw
135
136 sp_counted_impl_pda( P p, D d, A a ): p_( p ), d_( d ), a_( a )
137 {
138 }
139
140 virtual void dispose() // nothrow
141 {
142 d_( p_ );
143 }
144
145 virtual void destroy() // nothrow
146 {
147 typedef typename A::template rebind< this_type >::other A2;
148
149 A2 a2( a_ );
150
151 this->~this_type();
152 a2.deallocate( this, 1 );
153 }
154
155 virtual void * get_deleter( detail::sp_typeinfo const & ti )
156 {
157 return ti == BOOSTER_SP_TYPEID( D )? &reinterpret_cast<char&>( d_ ): 0;
158 }
159 };
160
161 #ifdef __CODEGUARD__
162 # pragma option pop
163 #endif
164
165 } // namespace detail
166
167 } // namespace boost
168
169 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_IMPL_HPP_INCLUDED
booster::detail::sp_counted_base
Definition: sp_counted_base.h:39
booster::detail::sp_counted_impl_pd
Definition: sp_counted_impl.h:77
booster::detail::sp_counted_impl_p
Definition: sp_counted_impl.h:32
booster::detail::sp_counted_impl_pda
Definition: sp_counted_impl.h:119
booster
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23