/** \file type_depository.hpp \brief depository and draw objects \author Junhua Gu */ #ifndef TYPE_DEPOSITORY_HPP #define TYPE_DEPOSITORY_HPP #include #include #include #include #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() { destroy(); } holder& operator=(const holder& rhs) { if(this==&rhs) { return *this; } destroy(); ptr=rhs.ptr; const_cast(rhs).ptr=0; } public: T* release() { T* p=ptr; ptr=0; return p; } void destroy() { if(ptr) { ptr->destroy(); } } 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(const 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; } } 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) { const func_obj* pfo=&fo; fetch_func_obj(pfo,fo.get_type_name(),in); } template const func_obj* get_func_obj(std::string cname) { const 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(const 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; } } else if(dir==in) { //pm.insert(cname,fo->clone()); pm[cname]=holder >(fo->clone()); } } template void register_opt_method(const opt_method& fo) { const opt_method* pfo=&fo; fetch_opt_method(pfo,fo.get_type_name(),in); } template const opt_method* get_opt_method(std::string cname) { const 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(const 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; } } else if(dir==in) { //pm.insert(cname,fo->clone()); pm[cname]=holder >(fo->clone()); } } template void register_statistic(const statistic& fo) { const statistic* pfo=&fo; fetch_statistic(pfo,fo.get_type_name(),in); } template const statistic* get_statistic(std::string cname) { const 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(const 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; } } else if(dir==in) { //pm.insert(cname,fo->clone()); pm[cname]=holder >(fo->clone()); } } template void register_param_modifier(const param_modifier& fo) { const param_modifier* pfo=&fo; fetch_param_modifier(pfo,fo.get_type_name(),in); } template const param_modifier* get_param_modifier(std::string cname) { const 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(const 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; } } else if(dir==in) { //pm.insert(cname,fo->clone()); pm[cname]=holder >(fo->clone()); } } template void register_data_set(const data_set& fo) { const data_set* pfo=&fo; fetch_data_set(pfo,fo.get_type_name(),in); } template const data_set* get_data_set(std::string cname) { const 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(const 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; } } else if(dir==in) { pm[cname]= holder >(fo->clone()); } } template void register_model(const model& fo) { const model* pfo=&fo; fetch_model(pfo,fo.get_type_name(),in); } template const model* get_model(std::string cname) { const model* pds; fetch_model(pds,cname,out); return pds; } } #endif