]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
moving option manager to INTERP_KERNEL
authorvbd <vbd>
Thu, 25 Sep 2008 09:19:46 +0000 (09:19 +0000)
committervbd <vbd>
Thu, 25 Sep 2008 09:19:46 +0000 (09:19 +0000)
src/INTERP_KERNEL/OptionManager.hxx [new file with mode: 0644]

diff --git a/src/INTERP_KERNEL/OptionManager.hxx b/src/INTERP_KERNEL/OptionManager.hxx
new file mode 100644 (file)
index 0000000..ed6609b
--- /dev/null
@@ -0,0 +1,151 @@
+#ifndef _OPTION_MANAGER_HXX
+#define _OPTION_MANAGER_HXX
+
+#include <string>
+#include <map>
+
+#include "InterpolationUtils.hxx"
+
+namespace INTERP_KERNEL
+{
+  class OptionGeneric
+  {
+    //dummy method to have some polymorphism...
+    virtual void dummy()=0;
+  };
+  
+       template < class T> class shared_ptr
+       {
+       private :
+               T*  _ptr;
+               int* _count;
+       public :
+               shared_ptr(T* ptr) 
+               {
+                       _ptr=ptr;
+                       _count==new int(1);
+               }
+               virtual ~shared_ptr()
+               {
+                       *_count --;
+                       if (*_count ==0 )
+                               {
+                                       delete _ptr;
+                                       delete _count;
+                               }
+               }
+               shared_ptr(const shared_ptr<T*>& sp)
+               {
+                       _count = sp._count;
+                       _ptr=sp._ptr;
+                       (*_count)++;
+               }
+
+               T& operator*()
+               {
+                       return *_ptr;
+               }
+       };
+
+  template < class T > 
+  class Option : public OptionGeneric
+  {
+  private:
+    T* _var;
+  public:
+    Option( T defaut, T * var):
+      _var(var)
+    {
+      *_var= defaut;
+    }
+    void setValue(T value)
+    {
+      *_var = value;
+    }
+    T getValue()
+    {
+      return   *_var;
+    }
+        
+    //dummy method to have some polymorphism...
+    void dummy(){}
+  };
+    
+  class OptionManager
+  {
+  private:
+    std::map< std::string, OptionGeneric* > _optionList;
+
+  public:
+  
+               OptionManager()
+               {}
+
+               OptionManager(const OptionManager& om)
+               {
+                       std::map< std::string, OptionGeneric*>::const_iterator mi;
+      for(mi = om._optionList.begin() ; mi != om._optionList.end() ; mi++) 
+                               {
+                                       _optionList.insert(make_pair(mi->first,mi->second));
+                               }   
+               }
+
+    ~OptionManager()
+    {
+                       //                      std::map< std::string, OptionGeneric*>::iterator mi;
+      //for(mi = _optionList.begin() ; mi != _optionList.end() ; mi++) {if ((*mi).second!=0) delete (*mi).second; (*mi).second=0;}
+    }
+    
+    template <class T> void  registerOption( T * var, const std::string& name, T defaut)
+    {
+      OptionGeneric  * newoption = new Option<T>( defaut, var);
+      _optionList.insert(make_pair(name, newoption));                  
+    }
+
+    template <class T> void getOption(const std::string& name, T& var)
+    {
+      if(_optionList.find(name) != _optionList.end())  
+                               {
+                                       Option<T>* option_ptr = dynamic_cast<Option<T>*>(_optionList.find(name)->second);
+                                       var= option_ptr->getValue();
+                               }
+                       else throw Exception 
+                               ("Option is not listed, please register the option before using the getOption command");
+    }
+               
+    template <class T> void setOption(const std::string& name, T value)
+    {
+      if(_optionList.find(name) != _optionList.end())
+                               {
+                                       Option<T>* option_ptr = dynamic_cast<Option<T>*>(_optionList.find(name)->second);
+                                       if (option_ptr != 0 )
+                                               option_ptr->setValue(value);
+                                       else throw Exception ("Error setOption: Option is registered with a different type");
+                               }
+      else throw Exception 
+                               ("Option is not listed, please register the option before using the setOption command");
+    }
+    
+    void setOption(const std::string& name, int value)
+    {
+      if(_optionList.find(name) != _optionList.end())
+                               {
+                                       Option<double>* option_double_ptr = dynamic_cast<Option<double>*> (_optionList.find(name)->second);
+                                       if (option_double_ptr!=0)
+                                               setOption(name,(double) value);
+                                       else
+                                               {
+                                                       Option<int>* option_ptr =dynamic_cast<Option<int>*>(_optionList.find(name)->second);
+                                                       if (option_ptr != 0 )
+                                                               option_ptr->setValue(value);
+                                                       else throw Exception ("Error setOption: Option is registered with a different type");
+                                               }
+                               }
+      else throw Exception 
+                               ("Option is not listed, please register the option before using the setOption command");
+    }
+    
+  };
+}
+
+#endif