Chris@16
|
1 #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
|
Chris@16
|
2 #define BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
|
Chris@16
|
3
|
Chris@16
|
4 //
|
Chris@16
|
5 // detail/shared_ptr_nmt.hpp - shared_ptr.hpp without member templates
|
Chris@16
|
6 //
|
Chris@16
|
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
|
Chris@16
|
8 // Copyright (c) 2001, 2002 Peter Dimov
|
Chris@16
|
9 //
|
Chris@16
|
10 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
11 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
12 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
13 //
|
Chris@16
|
14 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
|
Chris@16
|
15 //
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/assert.hpp>
|
Chris@16
|
18 #include <boost/checked_delete.hpp>
|
Chris@16
|
19 #include <boost/serialization/throw_exception.hpp>
|
Chris@16
|
20 #include <boost/detail/atomic_count.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #ifndef BOOST_NO_AUTO_PTR
|
Chris@16
|
23 # include <memory> // for std::auto_ptr
|
Chris@16
|
24 #endif
|
Chris@16
|
25
|
Chris@16
|
26 #include <algorithm> // for std::swap
|
Chris@16
|
27 #include <functional> // for std::less
|
Chris@16
|
28 #include <new> // for std::bad_alloc
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost
|
Chris@16
|
31 {
|
Chris@16
|
32
|
Chris@16
|
33 template<class T> class shared_ptr
|
Chris@16
|
34 {
|
Chris@16
|
35 private:
|
Chris@16
|
36
|
Chris@16
|
37 typedef detail::atomic_count count_type;
|
Chris@16
|
38
|
Chris@16
|
39 public:
|
Chris@16
|
40
|
Chris@16
|
41 typedef T element_type;
|
Chris@16
|
42 typedef T value_type;
|
Chris@16
|
43
|
Chris@16
|
44 explicit shared_ptr(T * p = 0): px(p)
|
Chris@16
|
45 {
|
Chris@16
|
46 #ifndef BOOST_NO_EXCEPTIONS
|
Chris@16
|
47
|
Chris@16
|
48 try // prevent leak if new throws
|
Chris@16
|
49 {
|
Chris@16
|
50 pn = new count_type(1);
|
Chris@16
|
51 }
|
Chris@16
|
52 catch(...)
|
Chris@16
|
53 {
|
Chris@16
|
54 boost::checked_delete(p);
|
Chris@16
|
55 throw;
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 #else
|
Chris@16
|
59
|
Chris@16
|
60 pn = new count_type(1);
|
Chris@16
|
61
|
Chris@16
|
62 if(pn == 0)
|
Chris@16
|
63 {
|
Chris@16
|
64 boost::checked_delete(p);
|
Chris@16
|
65 boost::serialization::throw_exception(std::bad_alloc());
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 #endif
|
Chris@16
|
69 }
|
Chris@16
|
70
|
Chris@16
|
71 ~shared_ptr()
|
Chris@16
|
72 {
|
Chris@16
|
73 if(--*pn == 0)
|
Chris@16
|
74 {
|
Chris@16
|
75 boost::checked_delete(px);
|
Chris@16
|
76 delete pn;
|
Chris@16
|
77 }
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 shared_ptr(shared_ptr const & r): px(r.px) // never throws
|
Chris@16
|
81 {
|
Chris@16
|
82 pn = r.pn;
|
Chris@16
|
83 ++*pn;
|
Chris@16
|
84 }
|
Chris@16
|
85
|
Chris@16
|
86 shared_ptr & operator=(shared_ptr const & r)
|
Chris@16
|
87 {
|
Chris@16
|
88 shared_ptr(r).swap(*this);
|
Chris@16
|
89 return *this;
|
Chris@16
|
90 }
|
Chris@16
|
91
|
Chris@16
|
92 #ifndef BOOST_NO_AUTO_PTR
|
Chris@16
|
93
|
Chris@16
|
94 explicit shared_ptr(std::auto_ptr< T > & r)
|
Chris@16
|
95 {
|
Chris@16
|
96 pn = new count_type(1); // may throw
|
Chris@16
|
97 px = r.release(); // fix: moved here to stop leak if new throws
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 shared_ptr & operator=(std::auto_ptr< T > & r)
|
Chris@16
|
101 {
|
Chris@16
|
102 shared_ptr(r).swap(*this);
|
Chris@16
|
103 return *this;
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 #endif
|
Chris@16
|
107
|
Chris@16
|
108 void reset(T * p = 0)
|
Chris@16
|
109 {
|
Chris@16
|
110 BOOST_ASSERT(p == 0 || p != px);
|
Chris@16
|
111 shared_ptr(p).swap(*this);
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 T & operator*() const // never throws
|
Chris@16
|
115 {
|
Chris@16
|
116 BOOST_ASSERT(px != 0);
|
Chris@16
|
117 return *px;
|
Chris@16
|
118 }
|
Chris@16
|
119
|
Chris@16
|
120 T * operator->() const // never throws
|
Chris@16
|
121 {
|
Chris@16
|
122 BOOST_ASSERT(px != 0);
|
Chris@16
|
123 return px;
|
Chris@16
|
124 }
|
Chris@16
|
125
|
Chris@16
|
126 T * get() const // never throws
|
Chris@16
|
127 {
|
Chris@16
|
128 return px;
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 long use_count() const // never throws
|
Chris@16
|
132 {
|
Chris@16
|
133 return *pn;
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 bool unique() const // never throws
|
Chris@16
|
137 {
|
Chris@16
|
138 return *pn == 1;
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 void swap(shared_ptr< T > & other) // never throws
|
Chris@16
|
142 {
|
Chris@16
|
143 std::swap(px, other.px);
|
Chris@16
|
144 std::swap(pn, other.pn);
|
Chris@16
|
145 }
|
Chris@16
|
146
|
Chris@16
|
147 private:
|
Chris@16
|
148
|
Chris@16
|
149 T * px; // contained pointer
|
Chris@16
|
150 count_type * pn; // ptr to reference counter
|
Chris@16
|
151 };
|
Chris@16
|
152
|
Chris@16
|
153 template<class T, class U> inline bool operator==(shared_ptr< T > const & a, shared_ptr<U> const & b)
|
Chris@16
|
154 {
|
Chris@16
|
155 return a.get() == b.get();
|
Chris@16
|
156 }
|
Chris@16
|
157
|
Chris@16
|
158 template<class T, class U> inline bool operator!=(shared_ptr< T > const & a, shared_ptr<U> const & b)
|
Chris@16
|
159 {
|
Chris@16
|
160 return a.get() != b.get();
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 template<class T> inline bool operator<(shared_ptr< T > const & a, shared_ptr< T > const & b)
|
Chris@16
|
164 {
|
Chris@16
|
165 return std::less<T*>()(a.get(), b.get());
|
Chris@16
|
166 }
|
Chris@16
|
167
|
Chris@16
|
168 template<class T> void swap(shared_ptr< T > & a, shared_ptr< T > & b)
|
Chris@16
|
169 {
|
Chris@16
|
170 a.swap(b);
|
Chris@16
|
171 }
|
Chris@16
|
172
|
Chris@16
|
173 // get_pointer() enables boost::mem_fn to recognize shared_ptr
|
Chris@16
|
174
|
Chris@16
|
175 template<class T> inline T * get_pointer(shared_ptr< T > const & p)
|
Chris@16
|
176 {
|
Chris@16
|
177 return p.get();
|
Chris@16
|
178 }
|
Chris@16
|
179
|
Chris@16
|
180 } // namespace boost
|
Chris@16
|
181
|
Chris@16
|
182 #endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
|