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