From: vbd Date: Thu, 25 Sep 2008 09:19:46 +0000 (+0000) Subject: moving option manager to INTERP_KERNEL X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=836bf7caf8faf2556e18d4f873ef2518a44b67e5;p=tools%2Fmedcoupling.git moving option manager to INTERP_KERNEL --- diff --git a/src/INTERP_KERNEL/OptionManager.hxx b/src/INTERP_KERNEL/OptionManager.hxx new file mode 100644 index 000000000..ed6609b03 --- /dev/null +++ b/src/INTERP_KERNEL/OptionManager.hxx @@ -0,0 +1,151 @@ +#ifndef _OPTION_MANAGER_HXX +#define _OPTION_MANAGER_HXX + +#include +#include + +#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& 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 void registerOption( T * var, const std::string& name, T defaut) + { + OptionGeneric * newoption = new Option( defaut, var); + _optionList.insert(make_pair(name, newoption)); + } + + template void getOption(const std::string& name, T& var) + { + if(_optionList.find(name) != _optionList.end()) + { + Option* option_ptr = dynamic_cast*>(_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 void setOption(const std::string& name, T value) + { + if(_optionList.find(name) != _optionList.end()) + { + Option* option_ptr = dynamic_cast*>(_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* option_double_ptr = dynamic_cast*> (_optionList.find(name)->second); + if (option_double_ptr!=0) + setOption(name,(double) value); + else + { + Option* option_ptr =dynamic_cast*>(_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