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