williamr@2
|
1 |
#ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
|
williamr@2
|
2 |
#define BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
|
williamr@2
|
3 |
|
williamr@2
|
4 |
//
|
williamr@2
|
5 |
// detail/shared_array_nmt.hpp - shared_array.hpp without member templates
|
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/assert.hpp>
|
williamr@2
|
18 |
#include <boost/checked_delete.hpp>
|
williamr@2
|
19 |
#include <boost/throw_exception.hpp>
|
williamr@2
|
20 |
#include <boost/detail/atomic_count.hpp>
|
williamr@2
|
21 |
|
williamr@2
|
22 |
#include <cstddef> // for std::ptrdiff_t
|
williamr@2
|
23 |
#include <algorithm> // for std::swap
|
williamr@2
|
24 |
#include <functional> // for std::less
|
williamr@2
|
25 |
#include <new> // for std::bad_alloc
|
williamr@2
|
26 |
|
williamr@2
|
27 |
namespace boost
|
williamr@2
|
28 |
{
|
williamr@2
|
29 |
|
williamr@2
|
30 |
template<class T> class shared_array
|
williamr@2
|
31 |
{
|
williamr@2
|
32 |
private:
|
williamr@2
|
33 |
|
williamr@2
|
34 |
typedef detail::atomic_count count_type;
|
williamr@2
|
35 |
|
williamr@2
|
36 |
public:
|
williamr@2
|
37 |
|
williamr@2
|
38 |
typedef T element_type;
|
williamr@2
|
39 |
|
williamr@2
|
40 |
explicit shared_array(T * p = 0): px(p)
|
williamr@2
|
41 |
{
|
williamr@2
|
42 |
#ifndef BOOST_NO_EXCEPTIONS
|
williamr@2
|
43 |
|
williamr@2
|
44 |
try // prevent leak if new throws
|
williamr@2
|
45 |
{
|
williamr@2
|
46 |
pn = new count_type(1);
|
williamr@2
|
47 |
}
|
williamr@2
|
48 |
catch(...)
|
williamr@2
|
49 |
{
|
williamr@2
|
50 |
boost::checked_array_delete(p);
|
williamr@2
|
51 |
throw;
|
williamr@2
|
52 |
}
|
williamr@2
|
53 |
|
williamr@2
|
54 |
#else
|
williamr@2
|
55 |
|
williamr@2
|
56 |
pn = new count_type(1);
|
williamr@2
|
57 |
|
williamr@2
|
58 |
if(pn == 0)
|
williamr@2
|
59 |
{
|
williamr@2
|
60 |
boost::checked_array_delete(p);
|
williamr@2
|
61 |
boost::throw_exception(std::bad_alloc());
|
williamr@2
|
62 |
}
|
williamr@2
|
63 |
|
williamr@2
|
64 |
#endif
|
williamr@2
|
65 |
}
|
williamr@2
|
66 |
|
williamr@2
|
67 |
~shared_array()
|
williamr@2
|
68 |
{
|
williamr@2
|
69 |
if(--*pn == 0)
|
williamr@2
|
70 |
{
|
williamr@2
|
71 |
boost::checked_array_delete(px);
|
williamr@2
|
72 |
delete pn;
|
williamr@2
|
73 |
}
|
williamr@2
|
74 |
}
|
williamr@2
|
75 |
|
williamr@2
|
76 |
shared_array(shared_array const & r) : px(r.px) // never throws
|
williamr@2
|
77 |
{
|
williamr@2
|
78 |
pn = r.pn;
|
williamr@2
|
79 |
++*pn;
|
williamr@2
|
80 |
}
|
williamr@2
|
81 |
|
williamr@2
|
82 |
shared_array & operator=(shared_array const & r)
|
williamr@2
|
83 |
{
|
williamr@2
|
84 |
shared_array(r).swap(*this);
|
williamr@2
|
85 |
return *this;
|
williamr@2
|
86 |
}
|
williamr@2
|
87 |
|
williamr@2
|
88 |
void reset(T * p = 0)
|
williamr@2
|
89 |
{
|
williamr@2
|
90 |
BOOST_ASSERT(p == 0 || p != px);
|
williamr@2
|
91 |
shared_array(p).swap(*this);
|
williamr@2
|
92 |
}
|
williamr@2
|
93 |
|
williamr@2
|
94 |
T * get() const // never throws
|
williamr@2
|
95 |
{
|
williamr@2
|
96 |
return px;
|
williamr@2
|
97 |
}
|
williamr@2
|
98 |
|
williamr@2
|
99 |
T & operator[](std::ptrdiff_t i) const // never throws
|
williamr@2
|
100 |
{
|
williamr@2
|
101 |
BOOST_ASSERT(px != 0);
|
williamr@2
|
102 |
BOOST_ASSERT(i >= 0);
|
williamr@2
|
103 |
return px[i];
|
williamr@2
|
104 |
}
|
williamr@2
|
105 |
|
williamr@2
|
106 |
long use_count() const // never throws
|
williamr@2
|
107 |
{
|
williamr@2
|
108 |
return *pn;
|
williamr@2
|
109 |
}
|
williamr@2
|
110 |
|
williamr@2
|
111 |
bool unique() const // never throws
|
williamr@2
|
112 |
{
|
williamr@2
|
113 |
return *pn == 1;
|
williamr@2
|
114 |
}
|
williamr@2
|
115 |
|
williamr@2
|
116 |
void swap(shared_array<T> & other) // never throws
|
williamr@2
|
117 |
{
|
williamr@2
|
118 |
std::swap(px, other.px);
|
williamr@2
|
119 |
std::swap(pn, other.pn);
|
williamr@2
|
120 |
}
|
williamr@2
|
121 |
|
williamr@2
|
122 |
private:
|
williamr@2
|
123 |
|
williamr@2
|
124 |
T * px; // contained pointer
|
williamr@2
|
125 |
count_type * pn; // ptr to reference counter
|
williamr@2
|
126 |
|
williamr@2
|
127 |
}; // shared_array
|
williamr@2
|
128 |
|
williamr@2
|
129 |
template<class T, class U> inline bool operator==(shared_array<T> const & a, shared_array<U> const & b)
|
williamr@2
|
130 |
{
|
williamr@2
|
131 |
return a.get() == b.get();
|
williamr@2
|
132 |
}
|
williamr@2
|
133 |
|
williamr@2
|
134 |
template<class T, class U> inline bool operator!=(shared_array<T> const & a, shared_array<U> const & b)
|
williamr@2
|
135 |
{
|
williamr@2
|
136 |
return a.get() != b.get();
|
williamr@2
|
137 |
}
|
williamr@2
|
138 |
|
williamr@2
|
139 |
template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b)
|
williamr@2
|
140 |
{
|
williamr@2
|
141 |
return std::less<T*>()(a.get(), b.get());
|
williamr@2
|
142 |
}
|
williamr@2
|
143 |
|
williamr@2
|
144 |
template<class T> void swap(shared_array<T> & a, shared_array<T> & b)
|
williamr@2
|
145 |
{
|
williamr@2
|
146 |
a.swap(b);
|
williamr@2
|
147 |
}
|
williamr@2
|
148 |
|
williamr@2
|
149 |
} // namespace boost
|
williamr@2
|
150 |
|
williamr@2
|
151 |
#endif // #ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED
|