00001 /// @addtogroup struct 00002 /// @{ 00003 00004 ///////////////////////////////////////////////////////////////////////////// 00005 /// 00006 /// @file localvar.h 00007 /// 00008 /// This file contains the definition of a template of a class 00009 /// whose object can define a local value of a given variable, 00010 /// and restores the original value upon destruction. 00011 /// 00012 /// @author Pawel Pilarczyk 00013 /// 00014 ///////////////////////////////////////////////////////////////////////////// 00015 00016 // Copyright (C) 1997-2013 by Pawel Pilarczyk. 00017 // 00018 // This file is part of the Homology Library. This library is free software; 00019 // you can redistribute it and/or modify it under the terms of the GNU 00020 // General Public License as published by the Free Software Foundation; 00021 // either version 2 of the License, or (at your option) any later version. 00022 // 00023 // This library is distributed in the hope that it will be useful, 00024 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00026 // GNU General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License along 00029 // with this software; see the file "license.txt". If not, write to the 00030 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00031 // MA 02111-1307, USA. 00032 00033 // Started on June 12, 2008. Last revision: June 13, 2008. 00034 00035 00036 #ifndef _CHOMP_STRUCT_LOCALVAR_H_ 00037 #define _CHOMP_STRUCT_LOCALVAR_H_ 00038 00039 00040 namespace chomp { 00041 namespace homology { 00042 00043 00044 // -------------------------------------------------- 00045 // -------------------- LOCALVAR -------------------- 00046 // -------------------------------------------------- 00047 00048 /// Local variable guardian. 00049 /// This template contains the definition of a class whose object 00050 /// remembers the previous value of a variable provided upon initialization, 00051 /// sets the new value, and restores the previous value upon destruction. 00052 /// This corresponds to certain extent to a local variable in Perl. 00053 template <class varType> 00054 class local_var 00055 { 00056 public: 00057 /// The constructor of a local variable guardian. 00058 local_var (varType &_variable); 00059 00060 /// The constructor of a local variable guardian 00061 /// which assigns a new value upon initialization. 00062 local_var (varType &_variable, const varType &_newValue); 00063 00064 /// The destructor of a local variable guardian. 00065 ~local_var (); 00066 00067 private: 00068 /// Reference to the global variable. 00069 varType &var; 00070 00071 /// The original value of the variable which is going to be 00072 /// restored upon destruction of the guardian object. 00073 const varType value; 00074 00075 }; /* class local_var */ 00076 00077 // -------------------------------------------------- 00078 00079 template <class varType> 00080 inline local_var<varType>::local_var (varType &_variable): 00081 var (_variable), value (_variable) 00082 { 00083 return; 00084 } /* local_var */ 00085 00086 template <class varType> 00087 inline local_var<varType>::local_var (varType &_variable, 00088 const varType &_newValue): var (_variable), value (_variable) 00089 { 00090 _variable = _newValue; 00091 return; 00092 } /* local_var */ 00093 00094 template <class varType> 00095 inline local_var<varType>::~local_var () 00096 { 00097 var = value; 00098 return; 00099 } /* ~local_var */ 00100 00101 00102 } // namespace homology 00103 } // namespace chomp 00104 00105 #endif // _CHOMP_STRUCT_LOCALVAR_H_ 00106 00107 /// @} 00108