]> git.lyx.org Git - lyx.git/blob - boost/boost/shared_array.hpp
update boost to pre-1.30.0
[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
29 #include <cstddef>            // for std::ptrdiff_t
30 #include <algorithm>          // for std::swap
31 #include <functional>         // for std::less
32
33 namespace boost
34 {
35
36 //
37 //  shared_array
38 //
39 //  shared_array extends shared_ptr to arrays.
40 //  The array pointed to is deleted when the last shared_array pointing to it
41 //  is destroyed or reset.
42 //
43
44 template<class T> class shared_array
45 {
46 private:
47
48     // Borland 5.5.1 specific workarounds
49     typedef checked_array_deleter<T> deleter;
50     typedef shared_array<T> this_type;
51
52 public:
53
54     typedef T element_type;
55
56     explicit shared_array(T * p = 0): px(p), pn(p, deleter())
57     {
58     }
59
60     //
61     // Requirements: D's copy constructor must not throw
62     //
63     // shared_array will release p by calling d(p)
64     //
65
66     template<class D> shared_array(T * p, D d): px(p), pn(p, d)
67     {
68     }
69
70 //  generated copy constructor, assignment, destructor are fine
71
72     void reset(T * p = 0)
73     {
74         BOOST_ASSERT(p == 0 || p != px);
75         this_type(p).swap(*this);
76     }
77
78     template <class D> void reset(T * p, D d)
79     {
80         this_type(p, d).swap(*this);
81     }
82
83     T & operator[] (std::ptrdiff_t i) const // never throws
84     {
85         BOOST_ASSERT(px != 0);
86         BOOST_ASSERT(i >= 0);
87         return px[i];
88     }
89     
90     T * get() const // never throws
91     {
92         return px;
93     }
94
95     // implicit conversion to "bool"
96
97     typedef T * (this_type::*unspecified_bool_type)() const;
98
99     operator unspecified_bool_type() const // never throws
100     {
101         return px == 0? 0: &this_type::get;
102     }
103
104     bool operator! () const // never throws
105     {
106         return px == 0;
107     }
108
109     bool unique() const // never throws
110     {
111         return pn.unique();
112     }
113
114     long use_count() const // never throws
115     {
116         return pn.use_count();
117     }
118
119     void swap(shared_array<T> & other) // never throws
120     {
121         std::swap(px, other.px);
122         pn.swap(other.pn);
123     }
124
125 private:
126
127     T * px;                     // contained pointer
128     detail::shared_count pn;    // reference counter
129
130 };  // shared_array
131
132 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws
133 {
134     return a.get() == b.get();
135 }
136
137 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws
138 {
139     return a.get() != b.get();
140 }
141
142 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws
143 {
144     return std::less<T*>()(a.get(), b.get());
145 }
146
147 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws
148 {
149     a.swap(b);
150 }
151
152 } // namespace boost
153
154 #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)
155
156 #endif  // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED