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