Chris@16: #ifndef BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // detail/shared_array_nmt.hpp - shared_array.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_array.htm for documentation. Chris@16: // Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include // for std::ptrdiff_t 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_array 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: Chris@16: explicit shared_array(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_array_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_array_delete(p); Chris@16: boost::throw_exception(std::bad_alloc()); Chris@16: } Chris@16: Chris@16: #endif Chris@16: } Chris@16: Chris@16: ~shared_array() Chris@16: { Chris@16: if(--*pn == 0) Chris@16: { Chris@16: boost::checked_array_delete(px); Chris@16: delete pn; Chris@16: } Chris@16: } Chris@16: Chris@16: shared_array(shared_array const & r) : px(r.px) // never throws Chris@16: { Chris@16: pn = r.pn; Chris@16: ++*pn; Chris@16: } Chris@16: Chris@16: shared_array & operator=(shared_array const & r) Chris@16: { Chris@16: shared_array(r).swap(*this); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: void reset(T * p = 0) Chris@16: { Chris@16: BOOST_ASSERT(p == 0 || p != px); Chris@16: shared_array(p).swap(*this); Chris@16: } Chris@16: Chris@16: T * get() const // never throws Chris@16: { Chris@16: return px; Chris@16: } Chris@16: Chris@16: T & operator[](std::ptrdiff_t i) const // never throws Chris@16: { Chris@16: BOOST_ASSERT(px != 0); Chris@16: BOOST_ASSERT(i >= 0); Chris@16: return px[i]; 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_array & 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: }; // shared_array Chris@16: Chris@16: template inline bool operator==(shared_array const & a, shared_array const & b) Chris@16: { Chris@16: return a.get() == b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator!=(shared_array const & a, shared_array const & b) Chris@16: { Chris@16: return a.get() != b.get(); Chris@16: } Chris@16: Chris@16: template inline bool operator<(shared_array const & a, shared_array const & b) Chris@16: { Chris@16: return std::less()(a.get(), b.get()); Chris@16: } Chris@16: Chris@16: template void swap(shared_array & a, shared_array & b) Chris@16: { Chris@16: a.swap(b); Chris@16: } Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED