Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _OPTION_MANAGER_HXX
00021 #define _OPTION_MANAGER_HXX
00022
00023 #include <string>
00024 #include <map>
00025 #include "MEDMEM_Exception.hxx"
00026 namespace MEDMEM
00027 {
00028 class OptionGeneric
00029 {
00030
00031 virtual void dummy()=0;
00032 };
00033
00034 template < class T >
00035 class Option : public OptionGeneric
00036 {
00037 private:
00038 T * _var;
00039 public:
00040 Option( T defaut, T * var):
00041 _var(var)
00042 {
00043 *_var= defaut;
00044 }
00045 void setValue(T value)
00046 {
00047 *_var=value;
00048 }
00049 T getValue()
00050 {
00051 return * _var;
00052 }
00053
00054
00055 void dummy(){}
00056 };
00057
00058 class OptionManager
00059 {
00060 private:
00061 std::map< std::string, OptionGeneric* > _optionList;
00062
00063 public:
00064
00065 ~OptionManager()
00066 {
00067 std::map< std::string, OptionGeneric*>::iterator mi;
00068 for(mi = _optionList.begin() ; mi != _optionList.end() ; mi++) delete (*mi).second;
00069 }
00070
00071 template <class T> void registerOption( T * var, const std::string& name, T defaut)
00072 {
00073 OptionGeneric * newoption = new Option<T>( defaut, var);
00074 _optionList.insert(make_pair(name, newoption));
00075 }
00076
00077 template <class T> void getOption(const std::string& name, T& var)
00078 {
00079 if(_optionList.find(name) != _optionList.end())
00080 {
00081 Option<T>* option_ptr = dynamic_cast<Option<T>*>(_optionList.find(name)->second);
00082 var= option_ptr->getValue();
00083 }
00084 else throw MEDEXCEPTION
00085 ("Option is not listed, please register the option before using the getOption command");
00086 }
00087
00088 template <class T> void setOption(const std::string& name, T value)
00089 {
00090 if(_optionList.find(name) != _optionList.end())
00091 {
00092 Option<T>* option_ptr = dynamic_cast<Option<T>*>(_optionList.find(name)->second);
00093 if (option_ptr != 0 )
00094 option_ptr->setValue(value);
00095 else throw MEDEXCEPTION ("Error setOption: Option is registered with a different type");
00096 }
00097 else throw MEDEXCEPTION
00098 ("Option is not listed, please register the option before using the setOption command");
00099 }
00100
00101 void setOption(const std::string& name, int value)
00102 {
00103 if(_optionList.find(name) != _optionList.end())
00104 {
00105 Option<double>* option_double_ptr = dynamic_cast<Option<double>*> (_optionList.find(name)->second);
00106 if (option_double_ptr!=0)
00107 setOption(name,(double) value);
00108 else
00109 {
00110 Option<int>* option_ptr =dynamic_cast<Option<int>*>(_optionList.find(name)->second);
00111 if (option_ptr != 0 )
00112 option_ptr->setValue(value);
00113 else throw MEDEXCEPTION ("Error setOption: Option is registered with a different type");
00114 }
00115 }
00116 else throw MEDEXCEPTION
00117 ("Option is not listed, please register the option before using the setOption command");
00118 }
00119
00120 };
00121 }
00122
00123 #endif