00001 /// @addtogroup struct 00002 /// @{ 00003 00004 ///////////////////////////////////////////////////////////////////////////// 00005 /// 00006 /// @file autoarray.h 00007 /// 00008 /// An auto_array template that mimics selected behaviors 00009 /// of the std::auto_ptr template, but releases the memory 00010 /// with delete[], and thus should be applied to arrays. 00011 /// This template is based on what I found in the g++ header file "memory". 00012 /// 00013 /// @author Pawel Pilarczyk 00014 /// 00015 ///////////////////////////////////////////////////////////////////////////// 00016 00017 // Copyright (C) 1997-2013 by Pawel Pilarczyk. 00018 // 00019 // This file is part of my research software package. This is free software; 00020 // you can redistribute it and/or modify it under the terms of the GNU 00021 // General Public License as published by the Free Software Foundation; 00022 // either version 2 of the License, or (at your option) any later version. 00023 // 00024 // This software is distributed in the hope that it will be useful, 00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00027 // GNU General Public License for more details. 00028 // 00029 // You should have received a copy of the GNU General Public License along 00030 // with this software; see the file "license.txt". If not, write to the 00031 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00032 // MA 02111-1307, USA. 00033 00034 // Started on April 10, 2011. Last revision: April 10, 2011. 00035 00036 00037 #ifndef _CHOMP_STRUCT_AUTOARRAY_H_ 00038 #define _CHOMP_STRUCT_AUTOARRAY_H_ 00039 00040 00041 namespace chomp { 00042 namespace homology { 00043 00044 00045 // -------------------------------------------------- 00046 // ------------------- auto_array ------------------- 00047 // -------------------------------------------------- 00048 00049 /// An auto_array template that mimics selected behaviors 00050 /// of the std::auto_ptr template, but releases the memory 00051 /// with delete[], and thus should be applied to arrays. 00052 /// This template is based on what I found in the g++ header file "memory". 00053 template <typename T> 00054 class auto_array 00055 { 00056 private: 00057 T *ptr; 00058 00059 public: 00060 /// The type of the elements in the array. 00061 typedef T element_type; 00062 00063 /// The constructor. 00064 explicit auto_array (element_type *p = 0) throw (): ptr (p) {} 00065 00066 /// Copy constructor. 00067 auto_array (auto_array &a) throw (): ptr (a. release ()) {} 00068 00069 /// Assignment operator. 00070 auto_array &operator = (auto_array &a) throw () 00071 { 00072 reset (a. release ()); 00073 return *this; 00074 } 00075 00076 /// The destructor. 00077 ~auto_array () 00078 { 00079 if (ptr) 00080 delete [] ptr; 00081 return; 00082 } 00083 00084 /// Returns the internally stored pointer to an array. 00085 element_type *get () const throw () 00086 { 00087 return ptr; 00088 } 00089 00090 /// Releases the pointer and returns it. 00091 element_type *release () throw () 00092 { 00093 element_type *tmp = ptr; 00094 ptr = 0; 00095 return tmp; 00096 } 00097 00098 /// Resets the object to hold another pointer. 00099 void reset (element_type *p = 0) throw () 00100 { 00101 if (p != ptr) 00102 { 00103 if (ptr) 00104 delete [] ptr; 00105 ptr = p; 00106 } 00107 } 00108 00109 }; /* class auto_array */ 00110 00111 00112 } // namespace homology 00113 } // namespace chomp 00114 00115 #endif // _CHOMP_STRUCT_AUTOARRAY_H_ 00116 00117 /// @} 00118