sl@0: #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED sl@0: #define BOOST_SHARED_ARRAY_HPP_INCLUDED sl@0: sl@0: // sl@0: // shared_array.hpp sl@0: // sl@0: // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. sl@0: // Copyright (c) 2001, 2002 Peter Dimov sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: // sl@0: // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation. sl@0: // sl@0: sl@0: #include <boost/config.hpp> // for broken compiler workarounds sl@0: sl@0: #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) sl@0: #include <boost/detail/shared_array_nmt.hpp> sl@0: #else sl@0: sl@0: #include <memory> // TR1 cyclic inclusion fix sl@0: sl@0: #include <boost/assert.hpp> sl@0: #include <boost/checked_delete.hpp> sl@0: sl@0: #include <boost/detail/shared_count.hpp> sl@0: #include <boost/detail/workaround.hpp> sl@0: sl@0: #include <cstddef> // for std::ptrdiff_t sl@0: #include <algorithm> // for std::swap sl@0: #include <functional> // for std::less sl@0: sl@0: namespace boost sl@0: { sl@0: sl@0: // sl@0: // shared_array sl@0: // sl@0: // shared_array extends shared_ptr to arrays. sl@0: // The array pointed to is deleted when the last shared_array pointing to it sl@0: // is destroyed or reset. sl@0: // sl@0: sl@0: template<class T> class shared_array sl@0: { sl@0: private: sl@0: sl@0: // Borland 5.5.1 specific workarounds sl@0: typedef checked_array_deleter<T> deleter; sl@0: typedef shared_array<T> this_type; sl@0: sl@0: public: sl@0: sl@0: typedef T element_type; sl@0: sl@0: explicit shared_array(T * p = 0): px(p), pn(p, deleter()) sl@0: { sl@0: } sl@0: sl@0: // sl@0: // Requirements: D's copy constructor must not throw sl@0: // sl@0: // shared_array will release p by calling d(p) sl@0: // sl@0: sl@0: template<class D> shared_array(T * p, D d): px(p), pn(p, d) sl@0: { sl@0: } sl@0: sl@0: // generated copy constructor, assignment, destructor are fine sl@0: sl@0: void reset(T * p = 0) sl@0: { sl@0: BOOST_ASSERT(p == 0 || p != px); sl@0: this_type(p).swap(*this); sl@0: } sl@0: sl@0: template <class D> void reset(T * p, D d) sl@0: { sl@0: this_type(p, d).swap(*this); sl@0: } sl@0: sl@0: T & operator[] (std::ptrdiff_t i) const // never throws sl@0: { sl@0: BOOST_ASSERT(px != 0); sl@0: BOOST_ASSERT(i >= 0); sl@0: return px[i]; sl@0: } sl@0: sl@0: T * get() const // never throws sl@0: { sl@0: return px; sl@0: } sl@0: sl@0: // implicit conversion to "bool" sl@0: sl@0: #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530) sl@0: sl@0: operator bool () const sl@0: { sl@0: return px != 0; sl@0: } sl@0: sl@0: #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) sl@0: typedef T * (this_type::*unspecified_bool_type)() const; sl@0: sl@0: operator unspecified_bool_type() const // never throws sl@0: { sl@0: return px == 0? 0: &this_type::get; sl@0: } sl@0: sl@0: #else sl@0: sl@0: typedef T * this_type::*unspecified_bool_type; sl@0: sl@0: operator unspecified_bool_type() const // never throws sl@0: { sl@0: return px == 0? 0: &this_type::px; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: bool operator! () const // never throws sl@0: { sl@0: return px == 0; sl@0: } sl@0: sl@0: bool unique() const // never throws sl@0: { sl@0: return pn.unique(); sl@0: } sl@0: sl@0: long use_count() const // never throws sl@0: { sl@0: return pn.use_count(); sl@0: } sl@0: sl@0: void swap(shared_array<T> & other) // never throws sl@0: { sl@0: std::swap(px, other.px); sl@0: pn.swap(other.pn); sl@0: } sl@0: sl@0: private: sl@0: sl@0: T * px; // contained pointer sl@0: detail::shared_count pn; // reference counter sl@0: sl@0: }; // shared_array sl@0: sl@0: template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws sl@0: { sl@0: return a.get() == b.get(); sl@0: } sl@0: sl@0: template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws sl@0: { sl@0: return a.get() != b.get(); sl@0: } sl@0: sl@0: template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws sl@0: { sl@0: return std::less<T*>()(a.get(), b.get()); sl@0: } sl@0: sl@0: template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws sl@0: { sl@0: a.swap(b); sl@0: } sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) sl@0: sl@0: #endif // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED