]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_OptionManager.hxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_OptionManager.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #ifndef _OPTION_MANAGER_HXX
23 #define _OPTION_MANAGER_HXX
24
25 #include <string>
26 #include <map>
27 #include "MEDMEM_Exception.hxx"
28 namespace MEDMEM
29 {
30   class OptionGeneric
31   {
32     //dummy method to have some polymorphism...
33     virtual void dummy()=0;
34   };
35   
36   template < class T > 
37   class Option : public OptionGeneric
38   {
39   private:
40     T * _var;
41   public:
42     Option( T defaut, T * var):
43       _var(var)
44     {
45       *_var= defaut;
46     }
47     void setValue(T value)
48     {
49       *_var=value;
50     }
51     T getValue()
52     {
53       return  * _var;
54     }
55
56     //dummy method to have some polymorphism...
57     void dummy(){}
58   };
59     
60   class OptionManager
61   {
62   private:
63     std::map< std::string, OptionGeneric* > _optionList;
64
65   public:
66   
67     ~OptionManager()
68     {
69                         std::map< std::string, OptionGeneric*>::iterator mi;
70       for(mi = _optionList.begin() ; mi != _optionList.end() ; mi++) delete (*mi).second;
71     }
72     
73     template <class T> void  registerOption( T * var, const std::string& name, T defaut)
74     {
75       OptionGeneric  * newoption = new Option<T>( defaut, var);
76       _optionList.insert(make_pair(name, newoption));                   
77     }
78
79     template <class T> void getOption(const std::string& name, T& var)
80     {
81       if(_optionList.find(name) != _optionList.end())  
82                                 {
83                                         Option<T>* option_ptr = dynamic_cast<Option<T>*>(_optionList.find(name)->second);
84                                         var= option_ptr->getValue();
85                                 }
86                         else throw MEDEXCEPTION 
87                                 ("Option is not listed, please register the option before using the getOption command");
88     }
89                 
90     template <class T> void setOption(const std::string& name, T value)
91     {
92       if(_optionList.find(name) != _optionList.end())
93                                 {
94                                         Option<T>* option_ptr = dynamic_cast<Option<T>*>(_optionList.find(name)->second);
95                                         if (option_ptr != 0 )
96                                                 option_ptr->setValue(value);
97                                         else throw MEDEXCEPTION ("Error setOption: Option is registered with a different type");
98                                 }
99       else throw MEDEXCEPTION 
100                                 ("Option is not listed, please register the option before using the setOption command");
101     }
102     
103     void setOption(const std::string& name, int value)
104     {
105       if(_optionList.find(name) != _optionList.end())
106                                 {
107                                         Option<double>* option_double_ptr = dynamic_cast<Option<double>*> (_optionList.find(name)->second);
108                                         if (option_double_ptr!=0)
109                                                 setOption(name,(double) value);
110                                         else
111                                                 {
112                                                         Option<int>* option_ptr =dynamic_cast<Option<int>*>(_optionList.find(name)->second);
113                                                         if (option_ptr != 0 )
114                                                                 option_ptr->setValue(value);
115                                                         else throw MEDEXCEPTION ("Error setOption: Option is registered with a different type");
116                                                 }
117                                 }
118       else throw MEDEXCEPTION 
119                                 ("Option is not listed, please register the option before using the setOption command");
120     }
121     
122   };
123 }
124
125 #endif