diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2010-08-15 08:09:57 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2010-08-15 08:09:57 +0000 |
commit | 323c80a9f87d22b33b781fa4737d6da3eb7268b7 (patch) | |
tree | 6240a6d5a389be76ea7820ed2c46ba2fd7b95ef4 /interface | |
parent | 16b94d08ac1196823d7e3daf787b85813ba33e6e (diff) | |
download | opt-utilities-323c80a9f87d22b33b781fa4737d6da3eb7268b7.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@132 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'interface')
-rw-r--r-- | interface/makefile | 5 | ||||
-rw-r--r-- | interface/opt.cc | 51 | ||||
-rw-r--r-- | interface/type_depository.hpp | 408 |
3 files changed, 460 insertions, 4 deletions
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 <utilities/opt_types.hpp> #include <math/num_diff.hpp> -#include <models/models.hpp> #include <map> #include <core/freeze_param.hpp> #include <data_sets/default_data_set.hpp> +#include "type_depository.hpp" +#include <memory> + +//models: +#include <models/gauss1d.hpp> +#include <models/bl1d.hpp> +#include <models/nfw1d.hpp> +#include <models/bpl1d.hpp> +#include <models/beta1d.hpp> +#include <models/nbeta1d.hpp> +#include <models/dbeta1d.hpp> +#include <models/lin1d.hpp> +#include <models/pl1d.hpp> +#include <models/poly1d.hpp> +#include <models/bremss.hpp> +#include <models/beta2d2.hpp> +#include <models/beta2d.hpp> +#include <models/dbeta2d2.hpp> +#include <models/dbeta2d3.hpp> +#include <models/dbeta2d.hpp> +#include <models/polar_ellipse.hpp> +//end models //#include <iostream> using namespace std; @@ -27,6 +48,31 @@ struct fit_space }; +std::vector<std::string> 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<<m.get_type_name()<<" has been registed"<<endl; + return; + } + cerr<<"registing:"<<m.get_type_name()<<endl; + model_names.push_back(m.get_type_name()); + opt_utilities::register_model(m); +} + + + +class initializer +{ +public: + initializer() + { + regist_model(lin1d<double>(),"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<dopt::model> p(opt_utilities::get_model<double,double,std::vector<double>,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 <string> +#include <utility> +#include "../core/optimizer.hpp" +#include "../core/fitter.hpp" +#include <map> +#include <iostream> +namespace opt_utilities +{ + + enum fetch_direction + { + in,out + }; + + class type_unregistered + :public opt_exception + { + const char* what()const throw() + { + return "type not registred"; + } + }; + + template <typename T> + class holder + { + private: + T* ptr; + public: + holder() + :ptr(0) + {} + + holder(T* p) + :ptr(p) + {} + + holder(const holder& rhs) + :ptr(rhs.ptr) + { + const_cast<holder&>(rhs).ptr=0; + } + + ~holder() + { + delete ptr; + } + + holder& operator=(const holder& rhs) + { + if(this==&rhs) + { + return *this; + } + delete ptr; + ptr=rhs.ptr; + const_cast<holder&>(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 <typename Ty,typename Tx> + void delete_clone(const func_obj<Ty,Tx>* pfo) + { + const_cast<func_obj<Ty,Tx>* >(pfo)->destroy(); + } + + template <typename Ty,typename Tx> + void fetch_func_obj(func_obj<Ty,Tx>* &fo,std::string cname,fetch_direction dir=in) + { + static std::map<std::string,holder<func_obj<Ty,Tx> > >pm; + typename std::map<std::string, + holder<func_obj<Ty,Tx> > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<<cname<<std::endl; + throw type_unregistered();; + } + else + { + func_obj<Ty,Tx>* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(make_pair(cname,holder<func_obj<Ty,Tx> >(fo->clone()))); + + pm[cname]=holder<func_obj<Ty,Tx> >(fo->clone()); + } + } + + template <typename Ty,typename Tx> + void register_func_obj(const func_obj<Ty,Tx>& fo) + { + func_obj<Ty,Tx>* pfo=const_cast<func_obj<Ty,Tx>*>(&fo); + fetch_func_obj(pfo,fo.get_type_name(),in); + } + + template <typename Ty,typename Tx> + func_obj<Ty,Tx>* get_func_obj(std::string cname) + { + func_obj<Ty,Tx>* pom; + fetch_func_obj(pom,cname,out); + return pom; + } + + + template <typename Ty,typename Tx> + void delete_clone(const opt_method<Ty,Tx>* pfo) + { + const_cast<opt_method<Ty,Tx>* >(pfo)->destroy(); + } + + template <typename Ty,typename Tx> + void fetch_opt_method(opt_method<Ty,Tx>* &fo,std::string cname,fetch_direction dir) + { + static std::map<std::string,holder<opt_method<Ty,Tx> > > pm; + typename std::map<std::string, + holder<opt_method<Ty,Tx> > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<<cname<<std::endl; + throw type_unregistered();; + } + else + { + opt_method<Ty,Tx>* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder<opt_method<Ty,Tx> >(fo->clone()); + } + } + + template <typename Ty,typename Tx> + void register_opt_method(const opt_method<Ty,Tx>& fo) + { + opt_method<Ty,Tx>* pfo=const_cast<opt_method<Ty,Tx>*>(&fo); + fetch_opt_method(pfo,fo.get_type_name(),in); + } + + template <typename Ty,typename Tx> + opt_method<Ty,Tx>* get_opt_method(std::string cname) + { + opt_method<Ty,Tx>* pom; + fetch_opt_method(pom,cname,out); + return pom; + } + + template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr> + void delete_clone(const statistic<Ty,Tx,Tp,Ts,Tstr>* pfo) + { + const_cast<statistic<Ty,Tx,Tp,Ts,Tstr>* >(pfo)->destroy(); + } + + + template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr> + void fetch_statistic(statistic<Ty,Tx,Tp,Ts,Tstr>* &fo,std::string cname,fetch_direction dir) + { + static std::map<std::string,holder<statistic<Ty,Tx,Tp,Ts,Tstr> > > pm; + typename std::map<std::string, + holder<statistic<Ty,Tx,Tp,Ts,Tstr> > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<<cname<<std::endl; + throw type_unregistered();; + } + else + { + statistic<Ty,Tx,Tp,Ts,Tstr>* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder<statistic<Ty,Tx,Tp,Ts,Tstr> >(fo->clone()); + } + } + + template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr> + void register_statistic(const statistic<Ty,Tx,Tp,Ts,Tstr>& fo) + { + statistic<Ty,Tx,Tp,Ts,Tstr>* pfo=const_cast<statistic<Ty,Tx,Tp,Ts,Tstr>*>(&fo); + fetch_statistic(pfo,fo.get_type_name(),in); + } + + template<typename Ty,typename Tx,typename Tp,typename Ts,typename Tstr> + statistic<Ty,Tx,Tp,Ts,Tstr>* get_statistic(std::string cname) + { + statistic<Ty,Tx,Tp,Ts,Tstr>* pst; + fetch_statistic(pst,cname,out); + return pst; + } + + template <typename Ty,typename Tx,typename Tp,typename Tstr> + void delete_clone(const param_modifier<Ty,Tx,Tp,Tstr>* pfo) + { + const_cast<param_modifier<Ty,Tx,Tp,Tstr>* >(pfo)->destroy(); + } + + template <typename Ty,typename Tx,typename Tp,typename Tstr> + void fetch_param_modifier(param_modifier<Ty,Tx,Tp,Tstr>* &fo,std::string cname,fetch_direction dir) + { + static std::map<std::string,holder<param_modifier<Ty,Tx,Tp,Tstr> > > pm; + typename std::map<std::string, + holder<param_modifier<Ty,Tx,Tp,Tstr> > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<<cname<<std::endl; + throw type_unregistered(); + } + else + { + param_modifier<Ty,Tx,Tp,Tstr>* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder<param_modifier<Ty,Tx,Tp,Tstr> >(fo->clone()); + } + } + + template<typename Ty,typename Tx,typename Tp,typename Tstr> + void register_param_modifier(const param_modifier<Ty,Tx,Tp,Tstr>& fo) + { + param_modifier<Ty,Tx,Tp,Tstr>* pfo=const_cast<param_modifier<Ty,Tx,Tp,Tstr>*>(&fo); + fetch_param_modifier(pfo,fo.get_type_name(),in); + } + + template<typename Ty,typename Tx,typename Tp,typename Tstr> + param_modifier<Ty,Tx,Tp,Tstr>* get_param_modifier(std::string cname) + { + param_modifier<Ty,Tx,Tp,Tstr>* ppm; + fetch_param_modifier(ppm,cname,out); + return ppm; + } + + + template <typename Ty,typename Tx> + void delete_clone(const data_set<Ty,Tx>* pfo) + { + const_cast<data_set<Ty,Tx>* >(pfo)->destroy(); + } + + template <typename Ty,typename Tx> + void fetch_data_set(data_set<Ty,Tx>* &fo,std::string cname,fetch_direction dir) + { + static std::map<std::string,holder<data_set<Ty,Tx> > > pm; + typename std::map<std::string, + holder<data_set<Ty,Tx> > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<<cname<<std::endl; + throw type_unregistered();; + } + else + { + data_set<Ty,Tx>* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + //pm.insert(cname,fo->clone()); + pm[cname]=holder<data_set<Ty,Tx> >(fo->clone()); + } + } + + template <typename Ty,typename Tx> + void register_data_set(const data_set<Ty,Tx>& fo) + { + data_set<Ty,Tx>* pfo=const_cast<data_set<Ty,Tx>*>(&fo); + fetch_data_set(pfo,fo.get_type_name(),in); + } + + template<typename Ty,typename Tx> + data_set<Ty,Tx>* get_data_set(std::string cname) + { + data_set<Ty,Tx>* pds; + fetch_data_set(pds,cname,out); + return pds; + } + + + + //////////////////// + template <typename Ty,typename Tx,typename Tp,typename Tstr> + void delete_clone(const model<Ty,Tx,Tp,Tstr>* pfo) + { + const_cast<model<Ty,Tx,Tp,Tstr>* >(pfo)->destroy(); + } + + + template <typename Ty,typename Tx,typename Tp,typename Tstr> + void fetch_model(model<Ty,Tx,Tp,Tstr>* &fo,std::string cname,fetch_direction dir) + { + static std::map<std::string,holder<model<Ty,Tx,Tp,Tstr> > > pm; + typename std::map<std::string, + holder<model<Ty,Tx,Tp,Tstr> > >::iterator it=pm.find(cname); + + if(dir==out) + { + if(it==pm.end()) + { + std::cerr<<cname<<std::endl; + throw type_unregistered(); + } + else + { + model<Ty,Tx,Tp,Tstr>* result=it->second; + fo=result->clone(); + } + } + else if(dir==in) + { + pm[cname]= holder<model<Ty,Tx,Tp,Tstr> >(fo->clone()); + } + } + + template <typename Ty,typename Tx,typename Tp,typename Tstr> + void register_model(const model<Ty,Tx,Tp,Tstr>& fo) + { + model<Ty,Tx,Tp,Tstr>* pfo=const_cast<model<Ty,Tx,Tp,Tstr>*>(&fo); + fetch_model(pfo,fo.get_type_name(),in); + } + + template <typename Ty,typename Tx,typename Tp,typename Tstr> + model<Ty,Tx,Tp,Tstr>* get_model(std::string cname) + { + model<Ty,Tx,Tp,Tstr>* pds; + fetch_model(pds,cname,out); + return pds; + } + +} + + +#endif |