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/throw_exception.hpp>
21 #include <boost/detail/atomic_count.hpp>
23 #ifndef BOOST_NO_AUTO_PTR
24 # include <memory> // for std::auto_ptr
27 #include <algorithm> // for std::swap
28 #include <functional> // for std::less
29 #include <new> // for std::bad_alloc
34 template<class T> class shared_ptr
38 typedef detail::atomic_count count_type;
42 typedef T element_type;
45 explicit shared_ptr(T * p = 0): px(p)
47 #ifndef BOOST_NO_EXCEPTIONS
49 try // prevent leak if new throws
51 pn = new count_type(1);
55 boost::checked_delete(p);
61 pn = new count_type(1);
65 boost::checked_delete(p);
66 boost::throw_exception(std::bad_alloc());
76 boost::checked_delete(px);
81 shared_ptr(shared_ptr const & r): px(r.px) // never throws
87 shared_ptr & operator=(shared_ptr const & r)
89 shared_ptr(r).swap(*this);
93 #ifndef BOOST_NO_AUTO_PTR
95 explicit shared_ptr(std::auto_ptr<T> & r)
97 pn = new count_type(1); // may throw
98 px = r.release(); // fix: moved here to stop leak if new throws
101 shared_ptr & operator=(std::auto_ptr<T> & r)
103 shared_ptr(r).swap(*this);
109 void reset(T * p = 0)
111 BOOST_ASSERT(p == 0 || p != px);
112 shared_ptr(p).swap(*this);
115 T & operator*() const // never throws
117 BOOST_ASSERT(px != 0);
121 T * operator->() const // never throws
123 BOOST_ASSERT(px != 0);
127 T * get() const // never throws
132 long use_count() const // never throws
137 bool unique() const // never throws
142 void swap(shared_ptr<T> & other) // never throws
144 std::swap(px, other.px);
145 std::swap(pn, other.pn);
150 T * px; // contained pointer
151 count_type * pn; // ptr to reference counter
154 template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b)
156 return a.get() == b.get();
159 template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b)
161 return a.get() != b.get();
164 template<class T> inline bool operator<(shared_ptr<T> const & a, shared_ptr<T> const & b)
166 return std::less<T*>()(a.get(), b.get());
169 template<class T> void swap(shared_ptr<T> & a, shared_ptr<T> & b)
174 // get_pointer() enables boost::mem_fn to recognize shared_ptr
176 template<class T> inline T * get_pointer(shared_ptr<T> const & p)
183 #endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED