]> git.lyx.org Git - lyx.git/blob - boost/boost/scoped_array.hpp
update to boost 1.30.1
[lyx.git] / boost / boost / scoped_array.hpp
1 #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED
2 #define BOOST_SCOPED_ARRAY_HPP_INCLUDED
3
4 //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
5 //  Copyright (c) 2001, 2002 Peter Dimov
6 //
7 //  Permission to copy, use, modify, sell and distribute this software
8 //  is granted provided this copyright notice appears in all copies.
9 //  This software is provided "as is" without express or implied
10 //  warranty, and with no claim as to its suitability for any purpose.
11 //
12 //  http://www.boost.org/libs/smart_ptr/scoped_array.htm
13 //
14
15 #include <boost/assert.hpp>
16 #include <boost/checked_delete.hpp>
17 #include <boost/config.hpp>   // in case ptrdiff_t not in std
18
19 #include <boost/detail/workaround.hpp>
20
21 #include <cstddef>            // for std::ptrdiff_t
22
23 namespace boost
24 {
25
26 // Debug hooks
27
28 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
29
30 void sp_array_constructor_hook(void * p);
31 void sp_array_destructor_hook(void * p);
32
33 #endif
34
35 //  scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
36 //  is guaranteed, either on destruction of the scoped_array or via an explicit
37 //  reset(). Use shared_array or std::vector if your needs are more complex.
38
39 template<class T> class scoped_array // noncopyable
40 {
41 private:
42
43     T * ptr;
44
45     scoped_array(scoped_array const &);
46     scoped_array & operator=(scoped_array const &);
47
48     typedef scoped_array<T> this_type;
49
50 public:
51
52     typedef T element_type;
53
54     explicit scoped_array(T * p = 0) : ptr(p) // never throws
55     {
56 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
57         boost::sp_array_constructor_hook(ptr);
58 #endif
59     }
60
61     ~scoped_array() // never throws
62     {
63 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
64         boost::sp_array_destructor_hook(ptr);
65 #endif
66         boost::checked_array_delete(ptr);
67     }
68
69     void reset(T * p = 0) // never throws
70     {
71         BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors
72         this_type(p).swap(*this);
73     }
74
75     T & operator[](std::ptrdiff_t i) const // never throws
76     {
77         BOOST_ASSERT(ptr != 0);
78         BOOST_ASSERT(i >= 0);
79         return ptr[i];
80     }
81
82     T * get() const // never throws
83     {
84         return ptr;
85     }
86
87     // implicit conversion to "bool"
88
89 #if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
90
91     operator bool () const
92     {
93         return ptr != 0;
94     }
95
96 #else
97
98     typedef T * (this_type::*unspecified_bool_type)() const;
99
100     operator unspecified_bool_type() const // never throws
101     {
102         return ptr == 0? 0: &this_type::get;
103     }
104
105 #endif
106
107     bool operator! () const // never throws
108     {
109         return ptr == 0;
110     }
111
112     void swap(scoped_array & b) // never throws
113     {
114         T * tmp = b.ptr;
115         b.ptr = ptr;
116         ptr = tmp;
117     }
118
119 };
120
121 template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) // never throws
122 {
123     a.swap(b);
124 }
125
126 } // namespace boost
127
128 #endif  // #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED