Path: blob/main/contrib/atf/atf-c++/detail/auto_array.hpp
39563 views
// Copyright (c) 2007 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#if !defined(ATF_CXX_DETAIL_AUTO_ARRAY_HPP)26#define ATF_CXX_DETAIL_AUTO_ARRAY_HPP2728#include <cstddef>2930namespace atf {3132// ------------------------------------------------------------------------33// The "auto_array" class.34// ------------------------------------------------------------------------3536template< class T >37struct auto_array_ref {38T* m_ptr;3940explicit auto_array_ref(T*);41};4243template< class T >44auto_array_ref< T >::auto_array_ref(T* ptr) :45m_ptr(ptr)46{47}4849template< class T >50class auto_array {51T* m_ptr;5253public:54auto_array(T* = NULL) throw();55auto_array(auto_array< T >&) throw();56auto_array(auto_array_ref< T >) throw();57~auto_array(void) throw();5859T* get(void) throw();60const T* get(void) const throw();61T* release(void) throw();62void reset(T* = NULL) throw();6364auto_array< T >& operator=(auto_array< T >&) throw();65auto_array< T >& operator=(auto_array_ref< T >) throw();6667T& operator[](int) throw();68operator auto_array_ref< T >(void) throw();69};7071template< class T >72auto_array< T >::auto_array(T* ptr)73throw() :74m_ptr(ptr)75{76}7778template< class T >79auto_array< T >::auto_array(auto_array< T >& ptr)80throw() :81m_ptr(ptr.release())82{83}8485template< class T >86auto_array< T >::auto_array(auto_array_ref< T > ref)87throw() :88m_ptr(ref.m_ptr)89{90}9192template< class T >93auto_array< T >::~auto_array(void)94throw()95{96if (m_ptr != NULL)97delete [] m_ptr;98}99100template< class T >101T*102auto_array< T >::get(void)103throw()104{105return m_ptr;106}107108template< class T >109const T*110auto_array< T >::get(void)111const throw()112{113return m_ptr;114}115116template< class T >117T*118auto_array< T >::release(void)119throw()120{121T* ptr = m_ptr;122m_ptr = NULL;123return ptr;124}125126template< class T >127void128auto_array< T >::reset(T* ptr)129throw()130{131if (m_ptr != NULL)132delete [] m_ptr;133m_ptr = ptr;134}135136template< class T >137auto_array< T >&138auto_array< T >::operator=(auto_array< T >& ptr)139throw()140{141reset(ptr.release());142return *this;143}144145template< class T >146auto_array< T >&147auto_array< T >::operator=(auto_array_ref< T > ref)148throw()149{150if (m_ptr != ref.m_ptr) {151delete [] m_ptr;152m_ptr = ref.m_ptr;153}154return *this;155}156157template< class T >158T&159auto_array< T >::operator[](int pos)160throw()161{162return m_ptr[pos];163}164165template< class T >166auto_array< T >::operator auto_array_ref< T >(void)167throw()168{169return auto_array_ref< T >(release());170}171172} // namespace atf173174#endif // !defined(ATF_CXX_DETAIL_AUTO_ARRAY_HPP)175176177