#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