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
35 namespace atf {
36
37 // ------------------------------------------------------------------------
38 // The "auto_array" class.
39 // ------------------------------------------------------------------------
40
41 template< class T >
42 struct auto_array_ref {
43 T* m_ptr;
44
45 explicit auto_array_ref(T*);
46 };
47
48 template< class T >
auto_array_ref(T * ptr)49 auto_array_ref< T >::auto_array_ref(T* ptr) :
50 m_ptr(ptr)
51 {
52 }
53
54 template< class T >
55 class auto_array {
56 T* m_ptr;
57
58 public:
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
76 template< class T >
auto_array(T * ptr)77 auto_array< T >::auto_array(T* ptr)
78 throw() :
79 m_ptr(ptr)
80 {
81 }
82
83 template< class T >
auto_array(auto_array<T> & ptr)84 auto_array< T >::auto_array(auto_array< T >& ptr)
85 throw() :
86 m_ptr(ptr.release())
87 {
88 }
89
90 template< class T >
auto_array(auto_array_ref<T> ref)91 auto_array< T >::auto_array(auto_array_ref< T > ref)
92 throw() :
93 m_ptr(ref.m_ptr)
94 {
95 }
96
97 template< class T >
~auto_array(void)98 auto_array< T >::~auto_array(void)
99 throw()
100 {
101 if (m_ptr != NULL)
102 delete [] m_ptr;
103 }
104
105 template< class T >
106 T*
get(void)107 auto_array< T >::get(void)
108 throw()
109 {
110 return m_ptr;
111 }
112
113 template< class T >
114 const T*
get(void) const115 auto_array< T >::get(void)
116 const throw()
117 {
118 return m_ptr;
119 }
120
121 template< class T >
122 T*
release(void)123 auto_array< T >::release(void)
124 throw()
125 {
126 T* ptr = m_ptr;
127 m_ptr = NULL;
128 return ptr;
129 }
130
131 template< class T >
132 void
reset(T * ptr)133 auto_array< T >::reset(T* ptr)
134 throw()
135 {
136 if (m_ptr != NULL)
137 delete [] m_ptr;
138 m_ptr = ptr;
139 }
140
141 template< class T >
142 auto_array< T >&
operator =(auto_array<T> & ptr)143 auto_array< T >::operator=(auto_array< T >& ptr)
144 throw()
145 {
146 reset(ptr.release());
147 return *this;
148 }
149
150 template< class T >
151 auto_array< T >&
operator =(auto_array_ref<T> ref)152 auto_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
162 template< class T >
163 T&
operator [](int pos)164 auto_array< T >::operator[](int pos)
165 throw()
166 {
167 return m_ptr[pos];
168 }
169
170 template< class T >
operator auto_array_ref<T>(void)171 auto_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