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