1 | #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED
|
---|
2 | #define BOOST_SHARED_ARRAY_HPP_INCLUDED
|
---|
3 |
|
---|
4 | //
|
---|
5 | // shared_array.hpp
|
---|
6 | //
|
---|
7 | // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
|
---|
8 | // Copyright (c) 2001, 2002 Peter Dimov
|
---|
9 | //
|
---|
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)
|
---|
13 | //
|
---|
14 | // See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.
|
---|
15 | //
|
---|
16 |
|
---|
17 | #include <boost/config.hpp> // for broken compiler workarounds
|
---|
18 |
|
---|
19 | #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
|
---|
20 | #include <boost/detail/shared_array_nmt.hpp>
|
---|
21 | #else
|
---|
22 |
|
---|
23 | #include <memory> // TR1 cyclic inclusion fix
|
---|
24 |
|
---|
25 | #include <boost/assert.hpp>
|
---|
26 | #include <boost/checked_delete.hpp>
|
---|
27 |
|
---|
28 | #include <boost/detail/shared_count.hpp>
|
---|
29 | #include <boost/detail/workaround.hpp>
|
---|
30 |
|
---|
31 | #include <cstddef> // for std::ptrdiff_t
|
---|
32 | #include <algorithm> // for std::swap
|
---|
33 | #include <functional> // for std::less
|
---|
34 |
|
---|
35 | namespace boost
|
---|
36 | {
|
---|
37 |
|
---|
38 | //
|
---|
39 | // shared_array
|
---|
40 | //
|
---|
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.
|
---|
44 | //
|
---|
45 |
|
---|
46 | template<class T> class shared_array
|
---|
47 | {
|
---|
48 | private:
|
---|
49 |
|
---|
50 | // Borland 5.5.1 specific workarounds
|
---|
51 | typedef checked_array_deleter<T> deleter;
|
---|
52 | typedef shared_array<T> this_type;
|
---|
53 |
|
---|
54 | public:
|
---|
55 |
|
---|
56 | typedef T element_type;
|
---|
57 |
|
---|
58 | explicit shared_array(T * p = 0): px(p), pn(p, deleter())
|
---|
59 | {
|
---|
60 | }
|
---|
61 |
|
---|
62 | //
|
---|
63 | // Requirements: D's copy constructor must not throw
|
---|
64 | //
|
---|
65 | // shared_array will release p by calling d(p)
|
---|
66 | //
|
---|
67 |
|
---|
68 | template<class D> shared_array(T * p, D d): px(p), pn(p, d)
|
---|
69 | {
|
---|
70 | }
|
---|
71 |
|
---|
72 | // generated copy constructor, assignment, destructor are fine
|
---|
73 |
|
---|
74 | void reset(T * p = 0)
|
---|
75 | {
|
---|
76 | BOOST_ASSERT(p == 0 || p != px);
|
---|
77 | this_type(p).swap(*this);
|
---|
78 | }
|
---|
79 |
|
---|
80 | template <class D> void reset(T * p, D d)
|
---|
81 | {
|
---|
82 | this_type(p, d).swap(*this);
|
---|
83 | }
|
---|
84 |
|
---|
85 | T & operator[] (std::ptrdiff_t i) const // never throws
|
---|
86 | {
|
---|
87 | BOOST_ASSERT(px != 0);
|
---|
88 | BOOST_ASSERT(i >= 0);
|
---|
89 | return px[i];
|
---|
90 | }
|
---|
91 |
|
---|
92 | T * get() const // never throws
|
---|
93 | {
|
---|
94 | return px;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // implicit conversion to "bool"
|
---|
98 |
|
---|
99 | #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
|
---|
100 |
|
---|
101 | operator bool () const
|
---|
102 | {
|
---|
103 | return px != 0;
|
---|
104 | }
|
---|
105 |
|
---|
106 | #elif defined( _MANAGED )
|
---|
107 |
|
---|
108 | static void unspecified_bool( this_type*** )
|
---|
109 | {
|
---|
110 | }
|
---|
111 |
|
---|
112 | typedef void (*unspecified_bool_type)( this_type*** );
|
---|
113 |
|
---|
114 | operator unspecified_bool_type() const // never throws
|
---|
115 | {
|
---|
116 | return px == 0? 0: unspecified_bool;
|
---|
117 | }
|
---|
118 |
|
---|
119 | #elif \
|
---|
120 | ( defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, < 0x3200) ) || \
|
---|
121 | ( defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ < 304) )
|
---|
122 |
|
---|
123 | typedef T * (this_type::*unspecified_bool_type)() const;
|
---|
124 |
|
---|
125 | operator unspecified_bool_type() const // never throws
|
---|
126 | {
|
---|
127 | return px == 0? 0: &this_type::get;
|
---|
128 | }
|
---|
129 |
|
---|
130 | #else
|
---|
131 |
|
---|
132 | typedef T * this_type::*unspecified_bool_type;
|
---|
133 |
|
---|
134 | operator unspecified_bool_type() const // never throws
|
---|
135 | {
|
---|
136 | return px == 0? 0: &this_type::px;
|
---|
137 | }
|
---|
138 |
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | bool operator! () const // never throws
|
---|
142 | {
|
---|
143 | return px == 0;
|
---|
144 | }
|
---|
145 |
|
---|
146 | bool unique() const // never throws
|
---|
147 | {
|
---|
148 | return pn.unique();
|
---|
149 | }
|
---|
150 |
|
---|
151 | long use_count() const // never throws
|
---|
152 | {
|
---|
153 | return pn.use_count();
|
---|
154 | }
|
---|
155 |
|
---|
156 | void swap(shared_array<T> & other) // never throws
|
---|
157 | {
|
---|
158 | std::swap(px, other.px);
|
---|
159 | pn.swap(other.pn);
|
---|
160 | }
|
---|
161 |
|
---|
162 | private:
|
---|
163 |
|
---|
164 | T * px; // contained pointer
|
---|
165 | detail::shared_count pn; // reference counter
|
---|
166 |
|
---|
167 | }; // shared_array
|
---|
168 |
|
---|
169 | template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
|
---|
170 | {
|
---|
171 | return a.get() == b.get();
|
---|
172 | }
|
---|
173 |
|
---|
174 | template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
|
---|
175 | {
|
---|
176 | return a.get() != b.get();
|
---|
177 | }
|
---|
178 |
|
---|
179 | template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
|
---|
180 | {
|
---|
181 | return std::less<T*>()(a.get(), b.get());
|
---|
182 | }
|
---|
183 |
|
---|
184 | template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
|
---|
185 | {
|
---|
186 | a.swap(b);
|
---|
187 | }
|
---|
188 |
|
---|
189 | } // namespace boost
|
---|
190 |
|
---|
191 | #endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
|
---|
192 |
|
---|
193 | #endif // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED
|
---|