fix doc example typo
[boost.git] / boost / serialization / detail / shared_ptr_nmt_132.hpp
blobc73e98128e71c6d3027e6aebef725b62f3061f80
1 #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
2 #define BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED
4 //
5 // detail/shared_ptr_nmt.hpp - shared_ptr.hpp without member templates
6 //
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001, 2002 Peter Dimov
9 //
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
24 #endif
26 #include <algorithm> // for std::swap
27 #include <functional> // for std::less
28 #include <new> // for std::bad_alloc
30 namespace boost
33 template<class T> class shared_ptr
35 private:
37 typedef detail::atomic_count count_type;
39 public:
41 typedef T element_type;
42 typedef T value_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);
52 catch(...)
54 boost::checked_delete(p);
55 throw;
58 #else
60 pn = new count_type(1);
62 if(pn == 0)
64 boost::checked_delete(p);
65 boost::serialization::throw_exception(std::bad_alloc());
68 #endif
71 ~shared_ptr()
73 if(--*pn == 0)
75 boost::checked_delete(px);
76 delete pn;
80 shared_ptr(shared_ptr const & r): px(r.px) // never throws
82 pn = r.pn;
83 ++*pn;
86 shared_ptr & operator=(shared_ptr const & r)
88 shared_ptr(r).swap(*this);
89 return *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);
103 return *this;
106 #endif
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);
117 return *px;
120 T * operator->() const // never throws
122 BOOST_ASSERT(px != 0);
123 return px;
126 T * get() const // never throws
128 return px;
131 long use_count() const // never throws
133 return *pn;
136 bool unique() const // never throws
138 return *pn == 1;
141 void swap(shared_ptr<T> & other) // never throws
143 std::swap(px, other.px);
144 std::swap(pn, other.pn);
147 private:
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)
170 a.swap(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)
177 return p.get();
180 } // namespace boost
182 #endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED