1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #if !defined(UTILS_AUTO_ARRAY_IPP)
30 #define UTILS_AUTO_ARRAY_IPP
32 #include "utils/auto_array.hpp"
40 /// Constructs a new auto_array_ref from a pointer.
42 /// \param ptr The pointer to wrap.
43 template< class T > inline
44 auto_array_ref< T >::auto_array_ref(T* ptr) :
53 /// Constructs a new auto_array from a given pointer.
55 /// This grabs ownership of the pointer unless it is NULL.
57 /// \param ptr The pointer to wrap. If not NULL, the memory pointed to must
58 /// have been allocated with operator new[].
59 template< class T > inline
60 auto_array< T >::auto_array(T* ptr) throw() :
66 /// Constructs a copy of an auto_array.
68 /// \param ptr The pointer to copy from. This pointer is invalidated and the
69 /// new copy grabs ownership of the object pointed to.
70 template< class T > inline
71 auto_array< T >::auto_array(auto_array< T >& ptr) throw() :
77 /// Constructs a new auto_array form a reference.
79 /// Internal function used to construct a new auto_array from an object
80 /// returned, for example, from a function.
82 /// \param ref The reference.
83 template< class T > inline
84 auto_array< T >::auto_array(detail::auto_array_ref< T > ref) throw() :
90 /// Destructor for auto_array objects.
91 template< class T > inline
92 auto_array< T >::~auto_array(void) throw()
99 /// Gets the value of the wrapped pointer without releasing ownership.
101 /// \return The raw mutable pointer.
102 template< class T > inline
104 auto_array< T >::get(void) throw()
110 /// Gets the value of the wrapped pointer without releasing ownership.
112 /// \return The raw immutable pointer.
113 template< class T > inline
115 auto_array< T >::get(void) const throw()
121 /// Gets the value of the wrapped pointer and releases ownership.
123 /// \return The raw mutable pointer.
124 template< class T > inline
126 auto_array< T >::release(void) throw()
134 /// Changes the value of the wrapped pointer.
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.
139 /// \param ptr The pointer to use as a replacement; may be NULL.
140 template< class T > inline
142 auto_array< T >::reset(T* ptr) throw()
150 /// Assignment operator.
152 /// \param ptr The object to copy from. This is invalidated after the copy.
153 /// \return A reference to the auto_array object itself.
154 template< class T > inline
156 auto_array< T >::operator=(auto_array< T >& ptr) throw()
158 reset(ptr.release());
163 /// Internal assignment operator for function returns.
165 /// \param ref The reference object to copy from.
166 /// \return A reference to the auto_array object itself.
167 template< class T > inline
169 auto_array< T >::operator=(detail::auto_array_ref< T > ref) throw()
171 if (_ptr != ref._ptr) {
179 /// Subscript operator to access the array by position.
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.
184 /// \param pos The position to access, indexed from zero.
186 /// \return A mutable reference to the element at the specified position.
187 template< class T > inline
189 auto_array< T >::operator[](int pos) throw()
195 /// Subscript operator to access the array by position.
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.
200 /// \param pos The position to access, indexed from zero.
202 /// \return An immutable reference to the element at the specified position.
203 template< class T > inline
205 auto_array< T >::operator[](int pos) const throw()
211 /// Internal conversion to a reference wrapper.
213 /// This is used internally to support returning auto_array objects from
214 /// functions. The auto_array is invalidated when used.
216 /// \return A new detail::auto_array_ref object holding the pointer.
217 template< class T > inline
218 auto_array< T >::operator detail::auto_array_ref< T >(void) throw()
220 return detail::auto_array_ref< T >(release());
227 #endif // !defined(UTILS_AUTO_ARRAY_IPP)