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