xref: /freebsd/contrib/kyua/utils/auto_array.ipp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1// Copyright 2010 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#if !defined(UTILS_AUTO_ARRAY_IPP)
30#define UTILS_AUTO_ARRAY_IPP
31
32#include "utils/auto_array.hpp"
33
34namespace utils {
35
36
37namespace detail {
38
39
40/// Constructs a new auto_array_ref from a pointer.
41///
42/// \param ptr The pointer to wrap.
43template< class T > inline
44auto_array_ref< T >::auto_array_ref(T* ptr) :
45    _ptr(ptr)
46{
47}
48
49
50}  // namespace detail
51
52
53/// Constructs a new auto_array from a given pointer.
54///
55/// This grabs ownership of the pointer unless it is NULL.
56///
57/// \param ptr The pointer to wrap.  If not NULL, the memory pointed to must
58/// have been allocated with operator new[].
59template< class T > inline
60auto_array< T >::auto_array(T* ptr) throw() :
61    _ptr(ptr)
62{
63}
64
65
66/// Constructs a copy of an auto_array.
67///
68/// \param ptr The pointer to copy from.  This pointer is invalidated and the
69/// new copy grabs ownership of the object pointed to.
70template< class T > inline
71auto_array< T >::auto_array(auto_array< T >& ptr) throw() :
72    _ptr(ptr.release())
73{
74}
75
76
77/// Constructs a new auto_array form a reference.
78///
79/// Internal function used to construct a new auto_array from an object
80/// returned, for example, from a function.
81///
82/// \param ref The reference.
83template< class T > inline
84auto_array< T >::auto_array(detail::auto_array_ref< T > ref) throw() :
85    _ptr(ref._ptr)
86{
87}
88
89
90/// Destructor for auto_array objects.
91template< class T > inline
92auto_array< T >::~auto_array(void) throw()
93{
94    if (_ptr != NULL)
95        delete [] _ptr;
96}
97
98
99/// Gets the value of the wrapped pointer without releasing ownership.
100///
101/// \return The raw mutable pointer.
102template< class T > inline
103T*
104auto_array< T >::get(void) throw()
105{
106    return _ptr;
107}
108
109
110/// Gets the value of the wrapped pointer without releasing ownership.
111///
112/// \return The raw immutable pointer.
113template< class T > inline
114const T*
115auto_array< T >::get(void) const throw()
116{
117    return _ptr;
118}
119
120
121/// Gets the value of the wrapped pointer and releases ownership.
122///
123/// \return The raw mutable pointer.
124template< class T > inline
125T*
126auto_array< T >::release(void) throw()
127{
128    T* ptr = _ptr;
129    _ptr = NULL;
130    return ptr;
131}
132
133
134/// Changes the value of the wrapped pointer.
135///
136/// If the auto_array was pointing to an array, such array is released and the
137/// wrapped pointer is replaced with the new pointer provided.
138///
139/// \param ptr The pointer to use as a replacement; may be NULL.
140template< class T > inline
141void
142auto_array< T >::reset(T* ptr) throw()
143{
144    if (_ptr != NULL)
145        delete [] _ptr;
146    _ptr = ptr;
147}
148
149
150/// Assignment operator.
151///
152/// \param ptr The object to copy from.  This is invalidated after the copy.
153/// \return A reference to the auto_array object itself.
154template< class T > inline
155auto_array< T >&
156auto_array< T >::operator=(auto_array< T >& ptr) throw()
157{
158    reset(ptr.release());
159    return *this;
160}
161
162
163/// Internal assignment operator for function returns.
164///
165/// \param ref The reference object to copy from.
166/// \return A reference to the auto_array object itself.
167template< class T > inline
168auto_array< T >&
169auto_array< T >::operator=(detail::auto_array_ref< T > ref) throw()
170{
171    if (_ptr != ref._ptr) {
172        delete [] _ptr;
173        _ptr = ref._ptr;
174    }
175    return *this;
176}
177
178
179/// Subscript operator to access the array by position.
180///
181/// This does not perform any bounds checking, in particular because auto_array
182/// does not know the size of the arrays pointed to by it.
183///
184/// \param pos The position to access, indexed from zero.
185///
186/// \return A mutable reference to the element at the specified position.
187template< class T > inline
188T&
189auto_array< T >::operator[](int pos) throw()
190{
191    return _ptr[pos];
192}
193
194
195/// Subscript operator to access the array by position.
196///
197/// This does not perform any bounds checking, in particular because auto_array
198/// does not know the size of the arrays pointed to by it.
199///
200/// \param pos The position to access, indexed from zero.
201///
202/// \return An immutable reference to the element at the specified position.
203template< class T > inline
204const T&
205auto_array< T >::operator[](int pos) const throw()
206{
207    return _ptr[pos];
208}
209
210
211/// Internal conversion to a reference wrapper.
212///
213/// This is used internally to support returning auto_array objects from
214/// functions.  The auto_array is invalidated when used.
215///
216/// \return A new detail::auto_array_ref object holding the pointer.
217template< class T > inline
218auto_array< T >::operator detail::auto_array_ref< T >(void) throw()
219{
220    return detail::auto_array_ref< T >(release());
221}
222
223
224}  // namespace utils
225
226
227#endif  // !defined(UTILS_AUTO_ARRAY_IPP)
228