1 #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED
2 #define BOOST_SMART_PTR_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/smart_ptr/detail/shared_array_nmt.hpp>
23 #include <memory> // TR1 cyclic inclusion fix
25 #include <boost/assert.hpp>
26 #include <boost/checked_delete.hpp>
28 #include <boost/smart_ptr/detail/shared_count.hpp>
29 #include <boost/detail/workaround.hpp>
31 #include <cstddef> // for std::ptrdiff_t
32 #include <algorithm> // for std::swap
33 #include <functional> // for std::less
41 // shared_array extends shared_ptr to arrays.
42 // The array pointed to is deleted when the last shared_array pointing to it
43 // is destroyed or reset.
46 template<class T> class shared_array
50 // Borland 5.5.1 specific workarounds
51 typedef checked_array_deleter<T> deleter;
52 typedef shared_array<T> this_type;
56 typedef T element_type;
58 explicit shared_array(T * p = 0): px(p), pn(p, deleter())
63 // Requirements: D's copy constructor must not throw
65 // shared_array will release p by calling d(p)
68 template<class D> shared_array(T * p, D d): px(p), pn(p, d)
72 // generated copy constructor, assignment, destructor are fine
76 BOOST_ASSERT(p == 0 || p != px);
77 this_type(p).swap(*this);
80 template <class D> void reset(T * p, D d)
82 this_type(p, d).swap(*this);
85 T & operator[] (std::ptrdiff_t i) const // never throws
87 BOOST_ASSERT(px != 0);
92 T * get() const // never throws
97 // implicit conversion to "bool"
98 #include <boost/smart_ptr/detail/operator_bool.hpp>
100 bool unique() const // never throws
105 long use_count() const // never throws
107 return pn.use_count();
110 void swap(shared_array<T> & other) // never throws
112 std::swap(px, other.px);
118 T * px; // contained pointer
119 detail::shared_count pn; // reference counter
123 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
125 return a.get() == b.get();
128 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
130 return a.get() != b.get();
133 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
135 return std::less<T*>()(a.get(), b.get());
138 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
145 #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
147 #endif // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED