1 #ifndef BOOST_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED
2 #define BOOST_DETAIL_SHARED_PTR_NMT_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 // Permission to copy, use, modify, sell and distribute this software
11 // is granted provided this copyright notice appears in all copies.
12 // This software is provided "as is" without express or implied
13 // warranty, and with no claim as to its suitability for any purpose.
15 // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation.
18 #include <boost/assert.hpp>
19 #include <boost/checked_delete.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
32 template<class T> class shared_ptr
36 typedef detail::atomic_count count_type;
40 typedef T element_type;
42 explicit shared_ptr(T * p = 0): px(p)
44 try // prevent leak if new throws
46 pn = new count_type(1);
64 shared_ptr(shared_ptr const & r): px(r.px) // never throws
70 shared_ptr & operator=(shared_ptr const & r)
72 shared_ptr(r).swap(*this);
76 #ifndef BOOST_NO_AUTO_PTR
78 explicit shared_ptr(std::auto_ptr<T> & r)
80 pn = new count_type(1); // may throw
81 px = r.release(); // fix: moved here to stop leak if new throws
84 shared_ptr & operator=(std::auto_ptr<T> & r)
86 shared_ptr(r).swap(*this);
94 BOOST_ASSERT(p == 0 || p != px);
95 shared_ptr(p).swap(*this);
98 T & operator*() const // never throws
100 BOOST_ASSERT(px != 0);
104 T * operator->() const // never throws
106 BOOST_ASSERT(px != 0);
110 T * get() const // never throws
115 long use_count() const // never throws
120 bool unique() const // never throws
125 void swap(shared_ptr<T> & other) // never throws
127 std::swap(px, other.px);
128 std::swap(pn, other.pn);
133 T * px; // contained pointer
134 count_type * pn; // ptr to reference counter
137 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
139 return a.get() == b.get();
142 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
144 return a.get() != b.get();
147 template<class T> inline bool operator<(shared_ptr<T> const & a, shared_ptr<T> const & b)
149 return std::less<T*>()(a.get(), b.get());
152 template<class T> void swap(shared_ptr<T> & a, shared_ptr<T> & b)
157 // get_pointer() enables boost::mem_fn to recognize shared_ptr
159 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
166 #endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED