Chris@16: #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED Chris@16: #define BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // detail/shared_ptr_nmt.hpp - shared_ptr.hpp without member templates Chris@16: // Chris@16: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. Chris@16: // Copyright (c) 2001, 2002 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation. Chris@16: // Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: # include // for std::auto_ptr Chris@16: #endif Chris@16: Chris@16: #include // for std::swap Chris@16: #include // for std::less Chris@16: #include // for std::bad_alloc Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: template class shared_ptr Chris@16: { Chris@16: private: Chris@16: Chris@16: typedef detail::atomic_count count_type; Chris@16: Chris@16: public: Chris@16: Chris@16: typedef T element_type; Chris@16: typedef T value_type; Chris@16: Chris@16: explicit shared_ptr(T * p = 0): px(p) Chris@16: { Chris@16: #ifndef BOOST_NO_EXCEPTIONS Chris@16: Chris@16: try // prevent leak if new throws Chris@16: { Chris@16: pn = new count_type(1); Chris@16: } Chris@16: catch(...) Chris@16: { Chris@16: boost::checked_delete(p); Chris@16: throw; Chris@16: } Chris@16: Chris@16: #else Chris@16: Chris@16: pn = new count_type(1); Chris@16: Chris@16: if(pn == 0) Chris@16: { Chris@16: boost::checked_delete(p); Chris@16: boost::serialization::throw_exception(std::bad_alloc()); Chris@16: } Chris@16: Chris@16: #endif Chris@16: } Chris@16: Chris@16: ~shared_ptr() Chris@16: { Chris@16: if(--*pn == 0) Chris@16: { Chris@16: boost::checked_delete(px); Chris@16: delete pn; Chris@16: } Chris@16: } Chris@16: Chris@16: shared_ptr(shared_ptr const & r): px(r.px) // never throws Chris@16: { Chris@16: pn = r.pn; Chris@16: ++*pn; Chris@16: } Chris@16: Chris@16: shared_ptr & operator=(shared_ptr const & r) Chris@16: { Chris@16: shared_ptr(r).swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_AUTO_PTR Chris@16: Chris@16: explicit shared_ptr(std::auto_ptr< T > & r) Chris@16: { Chris@16: pn = new count_type(1); // may throw Chris@16: px = r.release(); // fix: moved here to stop leak if new throws Chris@16: } Chris@16: Chris@16: shared_ptr & operator=(std::auto_ptr< T > & r) Chris@16: { Chris@16: shared_ptr(r).swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: void reset(T * p = 0) Chris@16: { Chris@16: BOOST_ASSERT(p == 0 || p != px); Chris@16: shared_ptr(p).swap(*this); Chris@16: } Chris@16: Chris@16: T & operator*() const // never throws Chris@16: { Chris@16: BOOST_ASSERT(px != 0); Chris@16: return *px; Chris@16: } Chris@16: Chris@16: T * operator->() const // never throws Chris@16: { Chris@16: BOOST_ASSERT(px != 0); Chris@16: return px; Chris@16: } Chris@16: Chris@16: T * get() const // never throws Chris@16: { Chris@16: return px; Chris@16: } Chris@16: Chris@16: long use_count() const // never throws Chris@16: { Chris@16: return *pn; Chris@16: } Chris@16: Chris@16: bool unique() const // never throws Chris@16: { Chris@16: return *pn == 1; Chris@16: } Chris@16: Chris@16: void swap(shared_ptr< T > & other) // never throws Chris@16: { Chris@16: std::swap(px, other.px); Chris@16: std::swap(pn, other.pn); Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: T * px; // contained pointer Chris@16: count_type * pn; // ptr to reference counter Chris@16: }; Chris@16: Chris@16: template inline bool operator==(shared_ptr< T > const & a, shared_ptr const & b) Chris@16: { Chris@16: return a.get() == b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator!=(shared_ptr< T > const & a, shared_ptr const & b) Chris@16: { Chris@16: return a.get() != b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator<(shared_ptr< T > const & a, shared_ptr< T > const & b) Chris@16: { Chris@16: return std::less()(a.get(), b.get()); Chris@16: } Chris@16: Chris@16: template void swap(shared_ptr< T > & a, shared_ptr< T > & b) Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: // get_pointer() enables boost::mem_fn to recognize shared_ptr Chris@16: Chris@16: template inline T * get_pointer(shared_ptr< T > const & p) Chris@16: { Chris@16: return p.get(); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_132_HPP_INCLUDED