]> git.lyx.org Git - lyx.git/blob - boost/boost/shared_array.hpp
2009efdfd47ec6a28007f8aa56e0c074b8009cf8
[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 #else
106
107     typedef T * (this_type::*unspecified_bool_type)() const;
108
109     operator unspecified_bool_type() const // never throws
110     {
111         return px == 0? 0: &this_type::get;
112     }
113
114 #endif
115
116     bool operator! () const // never throws
117     {
118         return px == 0;
119     }
120
121     bool unique() const // never throws
122     {
123         return pn.unique();
124     }
125
126     long use_count() const // never throws
127     {
128         return pn.use_count();
129     }
130
131     void swap(shared_array<T> & other) // never throws
132     {
133         std::swap(px, other.px);
134         pn.swap(other.pn);
135     }
136
137 private:
138
139     T * px;                     // contained pointer
140     detail::shared_count pn;    // reference counter
141
142 };  // shared_array
143
144 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
145 {
146     return a.get() == b.get();
147 }
148
149 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
150 {
151     return a.get() != b.get();
152 }
153
154 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
155 {
156     return std::less<T*>()(a.get(), b.get());
157 }
158
159 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
160 {
161     a.swap(b);
162 }
163
164 } // namespace boost
165
166 #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
167
168 #endif  // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED