auto_array.hpp revision 260029
1230557Sjimharris//
2230557Sjimharris// Automated Testing Framework (atf)
3230557Sjimharris//
4230557Sjimharris// Copyright (c) 2007 The NetBSD Foundation, Inc.
5230557Sjimharris// All rights reserved.
6230557Sjimharris//
7230557Sjimharris// Redistribution and use in source and binary forms, with or without
8230557Sjimharris// modification, are permitted provided that the following conditions
9230557Sjimharris// are met:
10230557Sjimharris// 1. Redistributions of source code must retain the above copyright
11230557Sjimharris//    notice, this list of conditions and the following disclaimer.
12230557Sjimharris// 2. Redistributions in binary form must reproduce the above copyright
13230557Sjimharris//    notice, this list of conditions and the following disclaimer in the
14230557Sjimharris//    documentation and/or other materials provided with the distribution.
15230557Sjimharris//
16230557Sjimharris// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17230557Sjimharris// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18230557Sjimharris// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19230557Sjimharris// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20230557Sjimharris// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21230557Sjimharris// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22230557Sjimharris// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23230557Sjimharris// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24230557Sjimharris// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25230557Sjimharris// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26230557Sjimharris// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27230557Sjimharris// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28230557Sjimharris//
29230557Sjimharris
30230557Sjimharris#if !defined(_ATF_CXX_AUTO_ARRAY_HPP_)
31230557Sjimharris#define _ATF_CXX_AUTO_ARRAY_HPP_
32230557Sjimharris
33230557Sjimharris#include <cstddef>
34230557Sjimharris
35230557Sjimharrisnamespace atf {
36230557Sjimharris
37230557Sjimharris// ------------------------------------------------------------------------
38230557Sjimharris// The "auto_array" class.
39230557Sjimharris// ------------------------------------------------------------------------
40230557Sjimharris
41230557Sjimharristemplate< class T >
42230557Sjimharrisstruct auto_array_ref {
43230557Sjimharris    T* m_ptr;
44230557Sjimharris
45230557Sjimharris    explicit auto_array_ref(T*);
46230557Sjimharris};
47230557Sjimharris
48230557Sjimharristemplate< class T >
49230557Sjimharrisauto_array_ref< T >::auto_array_ref(T* ptr) :
50230557Sjimharris    m_ptr(ptr)
51230557Sjimharris{
52230557Sjimharris}
53230557Sjimharris
54230557Sjimharristemplate< class T >
55230557Sjimharrisclass auto_array {
56230557Sjimharris    T* m_ptr;
57230557Sjimharris
58230557Sjimharrispublic:
59230557Sjimharris    auto_array(T* = NULL) throw();
60230557Sjimharris    auto_array(auto_array< T >&) throw();
61230557Sjimharris    auto_array(auto_array_ref< T >) throw();
62230557Sjimharris    ~auto_array(void) throw();
63230557Sjimharris
64230557Sjimharris    T* get(void) throw();
65230557Sjimharris    const T* get(void) const throw();
66230557Sjimharris    T* release(void) throw();
67230557Sjimharris    void reset(T* = NULL) throw();
68230557Sjimharris
69230557Sjimharris    auto_array< T >& operator=(auto_array< T >&) throw();
70230557Sjimharris    auto_array< T >& operator=(auto_array_ref< T >) throw();
71230557Sjimharris
72230557Sjimharris    T& operator[](int) throw();
73230557Sjimharris    operator auto_array_ref< T >(void) throw();
74230557Sjimharris};
75230557Sjimharris
76230557Sjimharristemplate< class T >
77230557Sjimharrisauto_array< T >::auto_array(T* ptr)
78230557Sjimharris    throw() :
79230557Sjimharris    m_ptr(ptr)
80230557Sjimharris{
81230557Sjimharris}
82230557Sjimharris
83230557Sjimharristemplate< class T >
84230557Sjimharrisauto_array< T >::auto_array(auto_array< T >& ptr)
85230557Sjimharris    throw() :
86230557Sjimharris    m_ptr(ptr.release())
87230557Sjimharris{
88230557Sjimharris}
89230557Sjimharris
90230557Sjimharristemplate< class T >
91230557Sjimharrisauto_array< T >::auto_array(auto_array_ref< T > ref)
92230557Sjimharris    throw() :
93230557Sjimharris    m_ptr(ref.m_ptr)
94230557Sjimharris{
95230557Sjimharris}
96230557Sjimharris
97230557Sjimharristemplate< class T >
98230557Sjimharrisauto_array< T >::~auto_array(void)
99230557Sjimharris    throw()
100230557Sjimharris{
101230557Sjimharris    if (m_ptr != NULL)
102230557Sjimharris        delete [] m_ptr;
103230557Sjimharris}
104230557Sjimharris
105230557Sjimharristemplate< class T >
106230557SjimharrisT*
107230557Sjimharrisauto_array< T >::get(void)
108230557Sjimharris    throw()
109230557Sjimharris{
110230557Sjimharris    return m_ptr;
111230557Sjimharris}
112230557Sjimharris
113230557Sjimharristemplate< class T >
114230557Sjimharrisconst T*
115230557Sjimharrisauto_array< T >::get(void)
116230557Sjimharris    const throw()
117230557Sjimharris{
118230557Sjimharris    return m_ptr;
119230557Sjimharris}
120230557Sjimharris
121230557Sjimharristemplate< class T >
122230557SjimharrisT*
123230557Sjimharrisauto_array< T >::release(void)
124230557Sjimharris    throw()
125230557Sjimharris{
126230557Sjimharris    T* ptr = m_ptr;
127230557Sjimharris    m_ptr = NULL;
128230557Sjimharris    return ptr;
129230557Sjimharris}
130230557Sjimharris
131230557Sjimharristemplate< class T >
132230557Sjimharrisvoid
133230557Sjimharrisauto_array< T >::reset(T* ptr)
134230557Sjimharris    throw()
135230557Sjimharris{
136230557Sjimharris    if (m_ptr != NULL)
137230557Sjimharris        delete [] m_ptr;
138230557Sjimharris    m_ptr = ptr;
139230557Sjimharris}
140230557Sjimharris
141230557Sjimharristemplate< class T >
142230557Sjimharrisauto_array< T >&
143230557Sjimharrisauto_array< T >::operator=(auto_array< T >& ptr)
144230557Sjimharris    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