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