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