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 #ifndef 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>
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<typename 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<typename 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 <typename 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 bool unique() const // never throws
100 long use_count() const // never throws
102 return pn.use_count();
105 void swap(shared_array<T> & other) // never throws
107 std::swap(px, other.px);
113 T * px; // contained pointer
114 detail::shared_count pn; // reference counter
118 template<typename T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
120 return a.get() == b.get();
123 template<typename T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
125 return a.get() != b.get();
128 template<typename T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
130 return std::less<T*>()(a.get(), b.get());
133 template<typename T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
140 #endif // #ifndef BOOST_MSVC6_MEMBER_TEMPLATES
142 #endif // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED