1 #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED
2 #define BOOST_SHARED_ARRAY_HPP_INCLUDED
7 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
8 // Copyright (c) 2001, 2002 Peter Dimov
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_array.htm for documentation.
17 #include <boost/config.hpp> // for broken compiler workarounds
19 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
20 #include <boost/detail/shared_array_nmt.hpp>
23 #include <boost/assert.hpp>
24 #include <boost/checked_delete.hpp>
26 #include <boost/detail/shared_count.hpp>
27 #include <boost/detail/workaround.hpp>
29 #include <cstddef> // for std::ptrdiff_t
30 #include <algorithm> // for std::swap
31 #include <functional> // for std::less
39 // shared_array extends shared_ptr to arrays.
40 // The array pointed to is deleted when the last shared_array pointing to it
41 // is destroyed or reset.
44 template<class T> class shared_array
48 // Borland 5.5.1 specific workarounds
49 typedef checked_array_deleter<T> deleter;
50 typedef shared_array<T> this_type;
54 typedef T element_type;
56 explicit shared_array(T * p = 0): px(p), pn(p, deleter())
61 // Requirements: D's copy constructor must not throw
63 // shared_array will release p by calling d(p)
66 template<class D> shared_array(T * p, D d): px(p), pn(p, d)
70 // generated copy constructor, assignment, destructor are fine
74 BOOST_ASSERT(p == 0 || p != px);
75 this_type(p).swap(*this);
78 template <class D> void reset(T * p, D d)
80 this_type(p, d).swap(*this);
83 T & operator[] (std::ptrdiff_t i) const // never throws
85 BOOST_ASSERT(px != 0);
90 T * get() const // never throws
95 // implicit conversion to "bool"
97 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
99 operator bool () const
104 #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
105 typedef T * (this_type::*unspecified_bool_type)() const;
107 operator unspecified_bool_type() const // never throws
109 return px == 0? 0: &this_type::get;
114 typedef T * this_type::*unspecified_bool_type;
116 operator unspecified_bool_type() const // never throws
118 return px == 0? 0: &this_type::px;
123 bool operator! () const // never throws
128 bool unique() const // never throws
133 long use_count() const // never throws
135 return pn.use_count();
138 void swap(shared_array<T> & other) // never throws
140 std::swap(px, other.px);
146 T * px; // contained pointer
147 detail::shared_count pn; // reference counter
151 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
153 return a.get() == b.get();
156 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
158 return a.get() != b.get();
161 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
163 return std::less<T*>()(a.get(), b.get());
166 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
173 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
175 #endif // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED