]> git.lyx.org Git - lyx.git/blob - boost/boost/scoped_array.hpp
major boost update
[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 //  See http://www.boost.org/libs/smart_ptr/scoped_array.htm for documentation.
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 #include <cstddef>            // for std::ptrdiff_t
19
20 namespace boost
21 {
22
23 //  scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to
24 //  is guaranteed, either on destruction of the scoped_array or via an explicit
25 //  reset(). Use shared_array or std::vector if your needs are more complex.
26
27 template<typename T> class scoped_array // noncopyable
28 {
29 private:
30
31     T * ptr;
32
33     scoped_array(scoped_array const &);
34     scoped_array & operator=(scoped_array const &);
35
36 public:
37
38     typedef T element_type;
39
40     explicit scoped_array(T * p = 0) : ptr(p) // never throws
41     {
42     }
43
44     ~scoped_array() // never throws
45     {
46         checked_array_delete(ptr);
47     }
48
49     void reset(T * p = 0) // never throws
50     {
51         if (ptr != p)
52         {
53             checked_array_delete(ptr);
54             ptr = p;
55         }
56     }
57
58     T & operator[](std::ptrdiff_t i) const // never throws
59     {
60         BOOST_ASSERT(ptr != 0);
61         BOOST_ASSERT(i >= 0);
62         return ptr[i];
63     }
64
65     T * get() const // never throws
66     {
67         return ptr;
68     }
69
70     void swap(scoped_array & b) // never throws
71     {
72         T * tmp = b.ptr;
73         b.ptr = ptr;
74         ptr = tmp;
75     }
76
77 };
78
79 template<class T> inline void swap(scoped_array<T> & a, scoped_array<T> & b) // never throws
80 {
81     a.swap(b);
82 }
83
84 } // namespace boost
85
86 #endif  // #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED