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