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// See http://www.boost.org/libs/smart_ptr/ for documentation.
12
13#include <boost/smart_ptr/detail/requires_cxx11.hpp>
14#include <boost/config.hpp>
15#include <boost/assert.hpp>
16#include <boost/core/checked_delete.hpp>
17#include <boost/smart_ptr/detail/sp_nullptr_t.hpp>
18#include <boost/smart_ptr/detail/sp_noexcept.hpp>
19
20#include <boost/config/workaround.hpp>
21
22#include <cstddef> // for std::ptrdiff_t
23
24namespace boost
25{
26
27// Debug hooks
28
29#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
30
31void sp_array_constructor_hook(void * p);
32void sp_array_destructor_hook(void * p);
33
34#endif
35
36// scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
37// is guaranteed, either on destruction of the scoped_array or via an explicit
38// reset(). Use shared_array or std::vector if your needs are more complex.
39
40template<class T> class scoped_array // noncopyable
41{
42private:
43
44 T * px;
45
46 scoped_array(scoped_array const &);
47 scoped_array & operator=(scoped_array const &);
48
49 typedef scoped_array<T> this_type;
50
51 void operator==( scoped_array const& ) const;
52 void operator!=( scoped_array const& ) const;
53
54public:
55
56 typedef T element_type;
57
58 explicit scoped_array( T * p = 0 ) BOOST_SP_NOEXCEPT : px( p )
59 {
60#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
61 boost::sp_array_constructor_hook( px );
62#endif
63 }
64
65 ~scoped_array() BOOST_SP_NOEXCEPT
66 {
67#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
68 boost::sp_array_destructor_hook( px );
69#endif
70 boost::checked_array_delete( px );
71 }
72
73 void reset(T * p = 0) BOOST_SP_NOEXCEPT_WITH_ASSERT
74 {
75 BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
76 this_type(p).swap(b&: *this);
77 }
78
79 T & operator[](std::ptrdiff_t i) const BOOST_SP_NOEXCEPT_WITH_ASSERT
80 {
81 BOOST_ASSERT( px != 0 );
82 BOOST_ASSERT( i >= 0 );
83 return px[i];
84 }
85
86 T * get() const BOOST_SP_NOEXCEPT
87 {
88 return px;
89 }
90
91// implicit conversion to "bool"
92#include <boost/smart_ptr/detail/operator_bool.hpp>
93
94 void swap(scoped_array & b) BOOST_SP_NOEXCEPT
95 {
96 T * tmp = b.px;
97 b.px = px;
98 px = tmp;
99 }
100};
101
102#if !defined( BOOST_NO_CXX11_NULLPTR )
103
104template<class T> inline bool operator==( scoped_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
105{
106 return p.get() == 0;
107}
108
109template<class T> inline bool operator==( boost::detail::sp_nullptr_t, scoped_array<T> const & p ) BOOST_SP_NOEXCEPT
110{
111 return p.get() == 0;
112}
113
114template<class T> inline bool operator!=( scoped_array<T> const & p, boost::detail::sp_nullptr_t ) BOOST_SP_NOEXCEPT
115{
116 return p.get() != 0;
117}
118
119template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, scoped_array<T> const & p ) BOOST_SP_NOEXCEPT
120{
121 return p.get() != 0;
122}
123
124#endif
125
126template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) BOOST_SP_NOEXCEPT
127{
128 a.swap(b);
129}
130
131} // namespace boost
132
133#endif // #ifndef BOOST_SMART_PTR_SCOPED_ARRAY_HPP_INCLUDED
134

source code of include/boost/smart_ptr/scoped_array.hpp