00001 // This file is part of ff3d - http://www.freefem.org/ff3d 00002 // Copyright (C) 2001, 2002, 2003 Stéphane Del Pino 00003 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2, or (at your option) 00007 // any later version. 00008 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software Foundation, 00016 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 // $Id: VariableRepository.cpp,v 1.2 2007/05/20 23:02:48 delpinux Exp $ 00019 00020 #include <VariableRepository.hpp> 00021 00022 #include <ReferenceCounting.hpp> 00023 #include <Variable.hpp> 00024 00025 #include <Stringify.hpp> 00026 #include <ErrorHandler.hpp> 00027 00028 ReferenceCounting<Variable> VariableRepository:: 00029 find(const std::string& name) const 00030 { 00031 Container::const_iterator i; 00032 size_t level=0; 00033 for (; level<=__blockLevel;++level) { 00034 i = __variableList[level].find(name); 00035 if (i != __variableList[level].end()) break; 00036 } 00037 if (level>__blockLevel) return 0; 00038 if (i == __variableList[level].end()) return 0; 00039 else { 00040 return i->second; 00041 } 00042 } 00043 00044 void VariableRepository:: 00045 add(ReferenceCounting<Variable> v) 00046 { 00047 if (find(v->name())) { 00048 throw ErrorHandler (__FILE__,__LINE__, 00049 "variable "+v->name()+" already declared", 00050 ErrorHandler::unexpected); 00051 } 00052 __variableList[__blockLevel][v->name()]=v; 00053 } 00054 00055 void VariableRepository:: 00056 remove(const std::string& name) 00057 { 00058 Container::const_iterator i; 00059 size_t level=0; 00060 for (; level<=__blockLevel;++level) { 00061 i = __variableList[level].find(name); 00062 if (i != __variableList[level].end()) break; 00063 } 00064 if ((i != __variableList[level].end()) and (level>__blockLevel)) { 00065 __variableList[level].erase(name); 00066 } else { 00067 const std::string errorMsg 00068 = "cannot erase variable "+name 00069 +", since it is not declared"; 00070 00071 throw ErrorHandler(__FILE__,__LINE__, 00072 errorMsg, 00073 ErrorHandler::unexpected); 00074 } 00075 } 00076 00077 void VariableRepository:: 00078 beginBlock() 00079 { 00080 __blockLevel++; 00081 __variableList.push_back(Container()); 00082 } 00083 00084 void VariableRepository:: 00085 endBlock() 00086 { 00087 ASSERT(__blockLevel>0); 00088 __variableList[__blockLevel].clear(); 00089 __blockLevel--; 00090 __variableList.pop_back(); 00091 } 00092 00093 VariableRepository:: 00094 VariableRepository() 00095 : __blockLevel(0) 00096 { 00097 __variableList.push_back(Container()); 00098 } 00099 00100 VariableRepository:: 00101 ~VariableRepository() 00102 { 00103 ; 00104 }
1.5.6