1 #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
2 #define BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
5 // detail/shared_ptr_nmt.hpp - shared_ptr.hpp without member templates
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001, 2002 Peter Dimov
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)
14 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
17 #include <boost/assert.hpp>
18 #include <boost/checked_delete.hpp>
19 #include <boost/serialization/throw_exception.hpp>
20 #include <boost/detail/atomic_count.hpp>
22 #ifndef BOOST_NO_AUTO_PTR
23 # include <memory> // for std::auto_ptr
26 #include <algorithm> // for std::swap
27 #include <functional> // for std::less
28 #include <new> // for std::bad_alloc
33 template<class T
> class shared_ptr
37 typedef detail::atomic_count count_type
;
41 typedef T element_type
;
44 explicit shared_ptr(T
* p
= 0): px(p
)
46 #ifndef BOOST_NO_EXCEPTIONS
48 try // prevent leak if new throws
50 pn
= new count_type(1);
54 boost::checked_delete(p
);
60 pn
= new count_type(1);
64 boost::checked_delete(p
);
65 boost::serialization::throw_exception(std::bad_alloc());
75 boost::checked_delete(px
);
80 shared_ptr(shared_ptr
const & r
): px(r
.px
) // never throws
86 shared_ptr
& operator=(shared_ptr
const & r
)
88 shared_ptr(r
).swap(*this);
92 #ifndef BOOST_NO_AUTO_PTR
94 explicit shared_ptr(std::auto_ptr
<T
> & r
)
96 pn
= new count_type(1); // may throw
97 px
= r
.release(); // fix: moved here to stop leak if new throws
100 shared_ptr
& operator=(std::auto_ptr
<T
> & r
)
102 shared_ptr(r
).swap(*this);
108 void reset(T
* p
= 0)
110 BOOST_ASSERT(p
== 0 || p
!= px
);
111 shared_ptr(p
).swap(*this);
114 T
& operator*() const // never throws
116 BOOST_ASSERT(px
!= 0);
120 T
* operator->() const // never throws
122 BOOST_ASSERT(px
!= 0);
126 T
* get() const // never throws
131 long use_count() const // never throws
136 bool unique() const // never throws
141 void swap(shared_ptr
<T
> & other
) // never throws
143 std::swap(px
, other
.px
);
144 std::swap(pn
, other
.pn
);
149 T
* px
; // contained pointer
150 count_type
* pn
; // ptr to reference counter
153 template<class T
, class U
> inline bool operator==(shared_ptr
<T
> const & a
, shared_ptr
<U
> const & b
)
155 return a
.get() == b
.get();
158 template<class T
, class U
> inline bool operator!=(shared_ptr
<T
> const & a
, shared_ptr
<U
> const & b
)
160 return a
.get() != b
.get();
163 template<class T
> inline bool operator<(shared_ptr
<T
> const & a
, shared_ptr
<T
> const & b
)
165 return std::less
<T
*>()(a
.get(), b
.get());
168 template<class T
> void swap(shared_ptr
<T
> & a
, shared_ptr
<T
> & b
)
173 // get_pointer() enables boost::mem_fn to recognize shared_ptr
175 template<class T
> inline T
* get_pointer(shared_ptr
<T
> const & p
)
182 #endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED