1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2007 The NetBSD Foundation, Inc.
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10// 1. Redistributions of source code must retain the above copyright
11//    notice, this list of conditions and the following disclaimer.
12// 2. Redistributions in binary form must reproduce the above copyright
13//    notice, this list of conditions and the following disclaimer in the
14//    documentation and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
30#if !defined(_ATF_CXX_AUTO_ARRAY_HPP_)
31#define _ATF_CXX_AUTO_ARRAY_HPP_
32
33#include <cstddef>
34
35namespace atf {
36
37// ------------------------------------------------------------------------
38// The "auto_array" class.
39// ------------------------------------------------------------------------
40
41template< class T >
42struct auto_array_ref {
43    T* m_ptr;
44
45    explicit auto_array_ref(T*);
46};
47
48template< class T >
49auto_array_ref< T >::auto_array_ref(T* ptr) :
50    m_ptr(ptr)
51{
52}
53
54template< class T >
55class auto_array {
56    T* m_ptr;
57
58public:
59    auto_array(T* = NULL) throw();
60    auto_array(auto_array< T >&) throw();
61    auto_array(auto_array_ref< T >) throw();
62    ~auto_array(void) throw();
63
64    T* get(void) throw();
65    const T* get(void) const throw();
66    T* release(void) throw();
67    void reset(T* = NULL) throw();
68
69    auto_array< T >& operator=(auto_array< T >&) throw();
70    auto_array< T >& operator=(auto_array_ref< T >) throw();
71
72    T& operator[](int) throw();
73    operator auto_array_ref< T >(void) throw();
74};
75
76template< class T >
77auto_array< T >::auto_array(T* ptr)
78    throw() :
79    m_ptr(ptr)
80{
81}
82
83template< class T >
84auto_array< T >::auto_array(auto_array< T >& ptr)
85    throw() :
86    m_ptr(ptr.release())
87{
88}
89
90template< class T >
91auto_array< T >::auto_array(auto_array_ref< T > ref)
92    throw() :
93    m_ptr(ref.m_ptr)
94{
95}
96
97template< class T >
98auto_array< T >::~auto_array(void)
99    throw()
100{
101    if (m_ptr != NULL)
102        delete [] m_ptr;
103}
104
105template< class T >
106T*
107auto_array< T >::get(void)
108    throw()
109{
110    return m_ptr;
111}
112
113template< class T >
114const T*
115auto_array< T >::get(void)
116    const throw()
117{
118    return m_ptr;
119}
120
121template< class T >
122T*
123auto_array< T >::release(void)
124    throw()
125{
126    T* ptr = m_ptr;
127    m_ptr = NULL;
128    return ptr;
129}
130
131template< class T >
132void
133auto_array< T >::reset(T* ptr)
134    throw()
135{
136    if (m_ptr != NULL)
137        delete [] m_ptr;
138    m_ptr = ptr;
139}
140
141template< class T >
142auto_array< T >&
143auto_array< T >::operator=(auto_array< T >& ptr)
144    throw()
145{
146    reset(ptr.release());
147    return *this;
148}
149
150template< class T >
151auto_array< T >&
152auto_array< T >::operator=(auto_array_ref< T > ref)
153    throw()
154{
155    if (m_ptr != ref.m_ptr) {
156        delete [] m_ptr;
157        m_ptr = ref.m_ptr;
158    }
159    return *this;
160}
161
162template< class T >
163T&
164auto_array< T >::operator[](int pos)
165    throw()
166{
167    return m_ptr[pos];
168}
169
170template< class T >
171auto_array< T >::operator auto_array_ref< T >(void)
172    throw()
173{
174    return auto_array_ref< T >(release());
175}
176
177} // namespace atf
178
179#endif // !defined(_ATF_CXX_AUTO_ARRAY_HPP_)
180