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