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 // Permission to copy, use, modify, sell and distribute this software
11 // is granted provided this copyright notice appears in all copies.
12 // This software is provided "as is" without express or implied
13 // warranty, and with no claim as to its suitability for any purpose.
15 // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
18 #include <boost/config.hpp> // for broken compiler workarounds
20 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
21 #include <boost/detail/shared_array_nmt.hpp>
24 #include <boost/assert.hpp>
25 #include <boost/checked_delete.hpp>
27 #include <boost/detail/shared_count.hpp>
28 #include <boost/detail/workaround.hpp>
30 #include <cstddef> // for std::ptrdiff_t
31 #include <algorithm> // for std::swap
32 #include <functional> // for std::less
40 // shared_array extends shared_ptr to arrays.
41 // The array pointed to is deleted when the last shared_array pointing to it
42 // is destroyed or reset.
45 template<class T> class shared_array
49 // Borland 5.5.1 specific workarounds
50 typedef checked_array_deleter<T> deleter;
51 typedef shared_array<T> this_type;
55 typedef T element_type;
57 explicit shared_array(T * p = 0): px(p), pn(p, deleter())
62 // Requirements: D's copy constructor must not throw
64 // shared_array will release p by calling d(p)
67 template<class D> shared_array(T * p, D d): px(p), pn(p, d)
71 // generated copy constructor, assignment, destructor are fine
75 BOOST_ASSERT(p == 0 || p != px);
76 this_type(p).swap(*this);
79 template <class D> void reset(T * p, D d)
81 this_type(p, d).swap(*this);
84 T & operator[] (std::ptrdiff_t i) const // never throws
86 BOOST_ASSERT(px != 0);
91 T * get() const // never throws
96 // implicit conversion to "bool"
98 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
100 operator bool () const
105 #elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
106 typedef T * (this_type::*unspecified_bool_type)() const;
108 operator unspecified_bool_type() const // never throws
110 return px == 0? 0: &this_type::get;
115 typedef T * this_type::*unspecified_bool_type;
117 operator unspecified_bool_type() const // never throws
119 return px == 0? 0: &this_type::px;
124 bool operator! () const // never throws
129 bool unique() const // never throws
134 long use_count() const // never throws
136 return pn.use_count();
139 void swap(shared_array<T> & other) // never throws
141 std::swap(px, other.px);
147 T * px; // contained pointer
148 detail::shared_count pn; // reference counter
152 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
154 return a.get() == b.get();
157 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
159 return a.get() != b.get();
162 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
164 return std::less<T*>()(a.get(), b.get());
167 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
174 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
176 #endif // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED