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