]> git.lyx.org Git - lyx.git/blob - boost/boost/smart_ptr/scoped_array.hpp
How about if we write a script to do some of this and stop doing it
[lyx.git] / boost / boost / smart_ptr / scoped_array.hpp
1 #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED\r
2 #define BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED\r
3 \r
4 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.\r
5 //  Copyright (c) 2001, 2002 Peter Dimov\r
6 //\r
7 //  Distributed under the Boost Software License, Version 1.0. (See\r
8 //  accompanying file LICENSE_1_0.txt or copy at\r
9 //  http://www.boost.org/LICENSE_1_0.txt)\r
10 //\r
11 //  http://www.boost.org/libs/smart_ptr/scoped_array.htm\r
12 //\r
13 \r
14 #include <boost/assert.hpp>\r
15 #include <boost/checked_delete.hpp>\r
16 #include <boost/config.hpp>   // in case ptrdiff_t not in std\r
17 \r
18 #include <boost/detail/workaround.hpp>\r
19 \r
20 #include <cstddef>            // for std::ptrdiff_t\r
21 \r
22 namespace boost\r
23 {\r
24 \r
25 // Debug hooks\r
26 \r
27 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
28 \r
29 void sp_array_constructor_hook(void * p);\r
30 void sp_array_destructor_hook(void * p);\r
31 \r
32 #endif\r
33 \r
34 //  scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to\r
35 //  is guaranteed, either on destruction of the scoped_array or via an explicit\r
36 //  reset(). Use shared_array or std::vector if your needs are more complex.\r
37 \r
38 template<class T> class scoped_array // noncopyable\r
39 {\r
40 private:\r
41 \r
42     T * px;\r
43 \r
44     scoped_array(scoped_array const &);\r
45     scoped_array & operator=(scoped_array const &);\r
46 \r
47     typedef scoped_array<T> this_type;\r
48 \r
49     void operator==( scoped_array const& ) const;\r
50     void operator!=( scoped_array const& ) const;\r
51 \r
52 public:\r
53 \r
54     typedef T element_type;\r
55 \r
56     explicit scoped_array( T * p = 0 ) : px( p ) // never throws\r
57     {\r
58 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
59         boost::sp_array_constructor_hook( px );\r
60 #endif\r
61     }\r
62 \r
63     ~scoped_array() // never throws\r
64     {\r
65 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)\r
66         boost::sp_array_destructor_hook( px );\r
67 #endif\r
68         boost::checked_array_delete( px );\r
69     }\r
70 \r
71     void reset(T * p = 0) // never throws\r
72     {\r
73         BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors\r
74         this_type(p).swap(*this);\r
75     }\r
76 \r
77     T & operator[](std::ptrdiff_t i) const // never throws\r
78     {\r
79         BOOST_ASSERT( px != 0 );\r
80         BOOST_ASSERT( i >= 0 );\r
81         return px[i];\r
82     }\r
83 \r
84     T * get() const // never throws\r
85     {\r
86         return px;\r
87     }\r
88 \r
89 // implicit conversion to "bool"\r
90 #include <boost/smart_ptr/detail/operator_bool.hpp>\r
91 \r
92     void swap(scoped_array & b) // never throws\r
93     {\r
94         T * tmp = b.px;\r
95         b.px = px;\r
96         px = tmp;\r
97     }\r
98 };\r
99 \r
100 template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) // never throws\r
101 {\r
102     a.swap(b);\r
103 }\r
104 \r
105 } // namespace boost\r
106 \r
107 #endif  // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED\r