]> git.lyx.org Git - lyx.git/blob - boost/boost/smart_ptr/shared_array.hpp
How about if we write a script to do some of this and stop doing it
[lyx.git] / boost / boost / smart_ptr / shared_array.hpp
1 #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED\r
2 #define BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED\r
3 \r
4 //\r
5 //  shared_array.hpp\r
6 //\r
7 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.\r
8 //  Copyright (c) 2001, 2002 Peter Dimov\r
9 //\r
10 //  Distributed under the Boost Software License, Version 1.0. (See\r
11 //  accompanying file LICENSE_1_0.txt or copy at\r
12 //  http://www.boost.org/LICENSE_1_0.txt)\r
13 //\r
14 //  See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation.\r
15 //\r
16 \r
17 #include <boost/config.hpp>   // for broken compiler workarounds\r
18 \r
19 #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)\r
20 #include <boost/smart_ptr/detail/shared_array_nmt.hpp>\r
21 #else\r
22 \r
23 #include <memory>             // TR1 cyclic inclusion fix\r
24 \r
25 #include <boost/assert.hpp>\r
26 #include <boost/checked_delete.hpp>\r
27 \r
28 #include <boost/smart_ptr/detail/shared_count.hpp>\r
29 #include <boost/detail/workaround.hpp>\r
30 \r
31 #include <cstddef>            // for std::ptrdiff_t\r
32 #include <algorithm>          // for std::swap\r
33 #include <functional>         // for std::less\r
34 \r
35 namespace boost\r
36 {\r
37 \r
38 //\r
39 //  shared_array\r
40 //\r
41 //  shared_array extends shared_ptr to arrays.\r
42 //  The array pointed to is deleted when the last shared_array pointing to it\r
43 //  is destroyed or reset.\r
44 //\r
45 \r
46 template<class T> class shared_array\r
47 {\r
48 private:\r
49 \r
50     // Borland 5.5.1 specific workarounds\r
51     typedef checked_array_deleter<T> deleter;\r
52     typedef shared_array<T> this_type;\r
53 \r
54 public:\r
55 \r
56     typedef T element_type;\r
57 \r
58     explicit shared_array(T * p = 0): px(p), pn(p, deleter())\r
59     {\r
60     }\r
61 \r
62     //\r
63     // Requirements: D's copy constructor must not throw\r
64     //\r
65     // shared_array will release p by calling d(p)\r
66     //\r
67 \r
68     template<class D> shared_array(T * p, D d): px(p), pn(p, d)\r
69     {\r
70     }\r
71 \r
72 //  generated copy constructor, assignment, destructor are fine\r
73 \r
74     void reset(T * p = 0)\r
75     {\r
76         BOOST_ASSERT(p == 0 || p != px);\r
77         this_type(p).swap(*this);\r
78     }\r
79 \r
80     template <class D> void reset(T * p, D d)\r
81     {\r
82         this_type(p, d).swap(*this);\r
83     }\r
84 \r
85     T & operator[] (std::ptrdiff_t i) const // never throws\r
86     {\r
87         BOOST_ASSERT(px != 0);\r
88         BOOST_ASSERT(i >= 0);\r
89         return px[i];\r
90     }\r
91     \r
92     T * get() const // never throws\r
93     {\r
94         return px;\r
95     }\r
96 \r
97 // implicit conversion to "bool"\r
98 #include <boost/smart_ptr/detail/operator_bool.hpp>\r
99 \r
100     bool unique() const // never throws\r
101     {\r
102         return pn.unique();\r
103     }\r
104 \r
105     long use_count() const // never throws\r
106     {\r
107         return pn.use_count();\r
108     }\r
109 \r
110     void swap(shared_array<T> & other) // never throws\r
111     {\r
112         std::swap(px, other.px);\r
113         pn.swap(other.pn);\r
114     }\r
115 \r
116 private:\r
117 \r
118     T * px;                     // contained pointer\r
119     detail::shared_count pn;    // reference counter\r
120 \r
121 };  // shared_array\r
122 \r
123 template<class T> inline bool operator==(shared_array<T> const & a, shared_array<T> const & b) // never throws\r
124 {\r
125     return a.get() == b.get();\r
126 }\r
127 \r
128 template<class T> inline bool operator!=(shared_array<T> const & a, shared_array<T> const & b) // never throws\r
129 {\r
130     return a.get() != b.get();\r
131 }\r
132 \r
133 template<class T> inline bool operator<(shared_array<T> const & a, shared_array<T> const & b) // never throws\r
134 {\r
135     return std::less<T*>()(a.get(), b.get());\r
136 }\r
137 \r
138 template<class T> void swap(shared_array<T> & a, shared_array<T> & b) // never throws\r
139 {\r
140     a.swap(b);\r
141 }\r
142 \r
143 } // namespace boost\r
144 \r
145 #endif  // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES)\r
146 \r
147 #endif  // #ifndef BOOST_SMART_PTR_SHARED_ARRAY_HPP_INCLUDED\r