From 323c80a9f87d22b33b781fa4737d6da3eb7268b7 Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Sun, 15 Aug 2010 08:09:57 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@132 ed2142bd-67ad-457f-ba7c-d818d4011675 --- interface/makefile | 5 +- interface/opt.cc | 51 +++++- interface/type_depository.hpp | 408 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 460 insertions(+), 4 deletions(-) create mode 100644 interface/type_depository.hpp diff --git a/interface/makefile b/interface/makefile index 62a53a7..c93f937 100644 --- a/interface/makefile +++ b/interface/makefile @@ -1,11 +1,12 @@ +target: libopt.a INC=-I../ libopt.a:opt.o - $(AR) $@ opt.o + $(AR) rv $@ opt.o opt.o:opt.cc opt.h - $(CXX) $< -o $@ $(INC) $(CXXFLAGS) + $(CXX) $< -o $@ $(INC) $(CXXFLAGS) -c clean: diff --git a/interface/opt.cc b/interface/opt.cc index 3930568..f122e2a 100644 --- a/interface/opt.cc +++ b/interface/opt.cc @@ -1,10 +1,31 @@ #include "opt.h" #include #include -#include #include #include #include +#include "type_depository.hpp" +#include + +//models: +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +//end models //#include using namespace std; @@ -27,6 +48,31 @@ struct fit_space }; +std::vector model_names; + +void regist_model(const dopt::model& m,const char* addr) +{ + + if(find(model_names.begin(),model_names.end(),m.get_type_name())!=model_names.end()) + { + cerr<(),"lin1d"); + } +}_initializer; extern "C" { @@ -90,7 +136,8 @@ void set_model_(const int& nfit,const char* model_name) } try { - iter->second.fit.set_model(opt_utilities::get_1dmodel_by_name(model_name)); + std::auto_ptr p(opt_utilities::get_model,std::string>(model_name)); + iter->second.fit.set_model(*p); } catch(opt_exception& e) { diff --git a/interface/type_depository.hpp b/interface/type_depository.hpp new file mode 100644 index 0000000..7405bb5 --- /dev/null +++ b/interface/type_depository.hpp @@ -0,0 +1,408 @@ +#ifndef TYPE_DEPOSITORY_HPP +#define TYPE_DEPOSITORY_HPP +#include +#include +#include "../core/optimizer.hpp" +#include "../core/fitter.hpp" +#include +#include +namespace opt_utilities +{ + + enum fetch_direction + { + in,out + }; + + class type_unregistered + :public opt_exception + { + const char* what()const throw() + { + return "type not registred"; + } + }; + + template + class holder + { + private: + T* ptr; + public: + holder() + :ptr(0) + {} + + holder(T* p) + :ptr(p) + {} + + holder(const holder& rhs) + :ptr(rhs.ptr) + { + const_cast(rhs).ptr=0; + } + + ~holder() + { + delete ptr; + } + + holder& operator=(const holder& rhs) + { + if(this==&rhs) + { + return *this; + } + delete ptr; + ptr=rhs.ptr; + const_cast(rhs).ptr=0; + } + + public: + T* release() + { + T* p=ptr; + ptr=0; + return p; + } + + void destroy() + { + delete ptr; + } + + T* get()const + { + return ptr; + } + + void reset(T* p) + { + destroy(); + ptr=p; + } + + operator T*() + { + return ptr; + } + + operator const T*()const + { + return ptr; + } + + public: + T& operator*() + { + return *ptr; + } + + public: + T* operator->() + { + return ptr; + } + }; + + + template + void delete_clone(const func_obj* pfo) + { + const_cast* >(pfo)->destroy(); + } + + template + void fetch_func_obj(func_obj* &fo,std::string cname,fetch_direction dir=in) + { + static std::map > >pm; + typename std::map > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(make_pair(cname,holder >(fo->clone()))); + + pm[cname]=holder >(fo->clone()); + } + } + + template + void register_func_obj(const func_obj& fo) + { + func_obj* pfo=const_cast*>(&fo); + fetch_func_obj(pfo,fo.get_type_name(),in); + } + + template + func_obj* get_func_obj(std::string cname) + { + func_obj* pom; + fetch_func_obj(pom,cname,out); + return pom; + } + + + template + void delete_clone(const opt_method* pfo) + { + const_cast* >(pfo)->destroy(); + } + + template + void fetch_opt_method(opt_method* &fo,std::string cname,fetch_direction dir) + { + static std::map > > pm; + typename std::map > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder >(fo->clone()); + } + } + + template + void register_opt_method(const opt_method& fo) + { + opt_method* pfo=const_cast*>(&fo); + fetch_opt_method(pfo,fo.get_type_name(),in); + } + + template + opt_method* get_opt_method(std::string cname) + { + opt_method* pom; + fetch_opt_method(pom,cname,out); + return pom; + } + + template + void delete_clone(const statistic* pfo) + { + const_cast* >(pfo)->destroy(); + } + + + template + void fetch_statistic(statistic* &fo,std::string cname,fetch_direction dir) + { + static std::map > > pm; + typename std::map > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder >(fo->clone()); + } + } + + template + void register_statistic(const statistic& fo) + { + statistic* pfo=const_cast*>(&fo); + fetch_statistic(pfo,fo.get_type_name(),in); + } + + template + statistic* get_statistic(std::string cname) + { + statistic* pst; + fetch_statistic(pst,cname,out); + return pst; + } + + template + void delete_clone(const param_modifier* pfo) + { + const_cast* >(pfo)->destroy(); + } + + template + void fetch_param_modifier(param_modifier* &fo,std::string cname,fetch_direction dir) + { + static std::map > > pm; + typename std::map > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder >(fo->clone()); + } + } + + template + void register_param_modifier(const param_modifier& fo) + { + param_modifier* pfo=const_cast*>(&fo); + fetch_param_modifier(pfo,fo.get_type_name(),in); + } + + template + param_modifier* get_param_modifier(std::string cname) + { + param_modifier* ppm; + fetch_param_modifier(ppm,cname,out); + return ppm; + } + + + template + void delete_clone(const data_set* pfo) + { + const_cast* >(pfo)->destroy(); + } + + template + void fetch_data_set(data_set* &fo,std::string cname,fetch_direction dir) + { + static std::map > > pm; + typename std::map > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder >(fo->clone()); + } + } + + template + void register_data_set(const data_set& fo) + { + data_set* pfo=const_cast*>(&fo); + fetch_data_set(pfo,fo.get_type_name(),in); + } + + template + data_set* get_data_set(std::string cname) + { + data_set* pds; + fetch_data_set(pds,cname,out); + return pds; + } + + + + //////////////////// + template + void delete_clone(const model* pfo) + { + const_cast* >(pfo)->destroy(); + } + + + template + void fetch_model(model* &fo,std::string cname,fetch_direction dir) + { + static std::map > > pm; + typename std::map > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + pm[cname]= holder >(fo->clone()); + } + } + + template + void register_model(const model& fo) + { + model* pfo=const_cast*>(&fo); + fetch_model(pfo,fo.get_type_name(),in); + } + + template + model* get_model(std::string cname) + { + model* pds; + fetch_model(pds,cname,out); + return pds; + } + +} + + +#endif -- cgit v1.2.2