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