📄 dll_object_manger_def.h
字号:
#ifndef __HBSTOCK2_MODEL_BASE_DEF_H__
#define __HBSTOCK2_MODEL_BASE_DEF_H__
/* must be at the end of include */
#include "gnu/i18n_debug.h"
template <class T>
hbstock2::DllObjectManger<T>::DllObjectManger()
{
m_set_dllobj_ptr.reset(new SetDllObject);
// init data loader
gnu::AppConfig* p_app_config = gnu::AppConfigInst::instance();
strcpy(m_p_ch_default_dir,p_app_config->get_root_dir().c_str());
strcat(m_p_ch_default_dir,"/plugin/selector");
memset(m_p_ch_file_prefix_default,0,sizeof(m_p_ch_file_prefix_default));
strcpy(m_p_ch_file_prefix_default,"slt_");
m_is_inited = false;
}
template <class T>
hbstock2::DllObjectManger<T>::~DllObjectManger() throw()
{
}
template <class T>
hbstock2::DllObjectManger<T>::DllObjectManger(const DllObjectManger& other)
{
*this = other;
}
template <class T>
hbstock2::DllObjectManger<T>& hbstock2::DllObjectManger<T>::operator=(const DllObjectManger& other)
{
DllObjectManger temp(other);
std::swap(temp);
return *this;
}
//set symbolname
template <class T>
void hbstock2::DllObjectManger<T>::set_symbol_name(const char* p_ch_symbolname)
{
strncpy(m_p_ch_symbolname,p_ch_symbolname,LEN_FILENAME);
}
template <class T>
const char* hbstock2::DllObjectManger<T>::get_symbol_name()
{
return m_p_ch_symbolname;
}
template <class T>
void* hbstock2::DllObjectManger<T>::load_dllobject(const char* p_ch_filename,const char* symbolname)
throw (hbstock2::hbstock_exception)
{
gnu::ACE_DLL_Ptr dll_loader_Ptr;
int ret = 0;
std::map<std::string,gnu::ACE_DLL_Ptr>::const_iterator iter;
iter = m_map_dll_loader.find(p_ch_filename);
if (iter!= m_map_dll_loader.end())
{
dll_loader_Ptr = iter->second;
} else
{
dll_loader_Ptr.reset(new ACE_DLL());
ret = dll_loader_Ptr->open(p_ch_filename); if(ret != 0)
{
throw hbstock2::hbstock_exception(std::string("open dso file:") + p_ch_filename + std::string(" error,error is: ") +
dll_loader_Ptr->error()); }
m_map_dll_loader.insert(std::make_pair(p_ch_filename,dll_loader_Ptr));
}
void* mkr = dll_loader_Ptr->symbol(symbolname);
if (mkr == NULL)
{
throw hbstock2::hbstock_exception("can't find " + std::string(symbolname) + std::string(" function , ") +
std::string(dll_loader_Ptr->error()));
}
return mkr;
}
template <class T>
bool hbstock2::DllObjectManger<T>::register_dllobject(const DllObjectPtr& obj_ptr)
{
bool isTrue = true;
obj_ptr->reload_config_parameter();
// set id
unsigned int id = 0;
id = get_dllobject_id(obj_ptr->get_name().c_str());
obj_ptr->set_id(id);
m_set_dllobj_ptr->insert(obj_ptr);
return isTrue;
}
template <class T>
bool hbstock2::DllObjectManger<T>::unregister_dllobject(unsigned int obj_id)
{
bool isTrue = false;
SetDllObject_Iterator iter;
for (iter=m_set_dllobj_ptr->begin();iter != m_set_dllobj_ptr->end();iter++)
{
if ((*iter)->getID() == obj_id)
{
m_set_dllobj_ptr->erase(iter);
isTrue = true;
break;
}
}
return isTrue;
}
template <class T>
unsigned int hbstock2::DllObjectManger<T>::get_dllobject_id(const char* p_ch_obj_name)
{
unsigned int id = 0;
std::string str_key = "w";
std::string str_value = "w";
char p_ch_value[50];
memset(p_ch_value,0,sizeof(p_ch_value));
str_key = gnu::str_trim(p_ch_obj_name) + "_ID";
gnu::AppConfig* p_app_config = gnu::AppConfigInst::instance();
if (p_app_config->get_value(str_key,str_value))
{
id = atol(str_value.c_str());
if (id < 10)
{
id = gnu::get_parameter_uuid(p_ch_obj_name);
sprintf(p_ch_value,"%d",id);
str_value = p_ch_value;
p_app_config->set_value(str_key,str_value);
}
} else
{
id = gnu::get_parameter_uuid(p_ch_obj_name);
sprintf(p_ch_value,"%d",id);
str_value = p_ch_value;
p_app_config->set_value(str_key,str_value);
}
return id;
}
template <class T>
ACE_Refcounted_Auto_Ptr<T,hbstock2::Hbstock_Mutex> hbstock2::DllObjectManger<T>::get_dllobject(unsigned int obj_id) const
{
DllObjectPtr object_ptr;
SetDllObject_Iterator iter;
for (iter=m_set_dllobj_ptr->begin();iter != m_set_dllobj_ptr->end();iter++)
{
if ((*iter)->get_id() == obj_id)
{
object_ptr = *iter;
break;
}
}
return object_ptr;
}
template <class T>
void hbstock2::DllObjectManger<T>::set_default_dllobject(const DllObjectPtr& obj_ptr)
{
m_default_object_ptr = obj_ptr;
std::string str_key = "w";
std::string str_value = "w";
char p_ch_value[50];
memset(p_ch_value,0,sizeof(p_ch_value));
str_key = std::string(typeid(*this).name()) + "_default_ID";
sprintf(p_ch_value,"%d", m_default_object_ptr->get_id());
str_value = p_ch_value;
gnu::AppConfig* p_app_config = gnu::AppConfigInst::instance();
p_app_config->set_value(str_key,str_value);
}
template <class T>
ACE_Refcounted_Auto_Ptr<T,hbstock2::Hbstock_Mutex> hbstock2::DllObjectManger<T>::get_default_dllobject()
{
if (m_default_object_ptr.get() != NULL)
{
return m_default_object_ptr;
}
// get_id
unsigned int id = 0;
std::string str_key = "w";
std::string str_value = "w";
char p_ch_value[50];
memset(p_ch_value,0,sizeof(p_ch_value));
str_key = std::string(typeid(*this).name()) + "_default_ID";
gnu::AppConfig* p_app_config = gnu::AppConfigInst::instance();
if (p_app_config->get_value(str_key,str_value))
{
id = atol(str_value.c_str());
m_default_object_ptr = get_dllobject(id);
return m_default_object_ptr;
}
m_default_object_ptr = (*m_set_dllobj_ptr->begin());
set_default_dllobject(m_default_object_ptr);
return m_default_object_ptr;
}
template <class T>
int hbstock2::DllObjectManger<T>::load_dllobject_from_dir(const char* p_ch_dir,const char* p_file_prefix)
{
int ret = 0;
std::vector<std::string> vect_dso_file;
gnu::get_dso_file(vect_dso_file,p_ch_dir,p_file_prefix);
for (unsigned int i=0;i<vect_dso_file.size();i++)
{
I18N_DEBUG_EX(_("%s load dso file : %s\n"), vect_dso_file[i].c_str());
load_dllobject_from_file(vect_dso_file[i].c_str());
}
return ret;
}
template <class T>
int hbstock2::DllObjectManger<T>::load_dllobject_from_file(const char* p_ch_filename)
{
int ret = 0;
T *(*dll_loader)(void) = 0;
T* p_loader = 0;
void* mkr = 0;
try
{
mkr = load_dllobject(p_ch_filename,m_p_ch_symbolname);
dll_loader = (T* (*)(void))mkr; p_loader = (*dll_loader)();
DllObjectPtr object_ptr(p_loader);
I18N_INFO(_("%s register DllObject : %s\n"),p_loader->get_title().c_str());
register_dllobject(object_ptr);
} catch (std::exception& ex)
{
ret = -1;
I18N_ERROR_EX(_("%s load dso error, error is : %s\n"),ex.what());
}
return ret;
}
template <class T>
int hbstock2::DllObjectManger<T>::init (int argc, char *argv[])
{
int i_ret = 0;
if (m_set_dllobj_ptr->size() == 0)
{
m_is_inited = false;
load_dllobject_from_default_dir();
}
if (m_is_inited)
{
return i_ret;
}
//I18N_DEBUG(_("%s [%s:%l] call init function ...\n"));
//int argc = 0;
//ACE_TCHAR* argv[] = {NULL};
SetDllObject_Iterator iter;
for (iter=m_set_dllobj_ptr->begin();iter != m_set_dllobj_ptr->end(); iter++)
{
(*iter)->init(argc,argv);
(*iter)->reload_config_parameter();
}
m_is_inited = true;
return i_ret;
}
template <class T>
int hbstock2::DllObjectManger<T>::fini (void)
{
int ret = 0;
//I18N_DEBUG(_("%s [%s:%l] call fini function ...\n"));
m_set_dllobj_ptr->clear();
return ret;
}
template <class T>
void hbstock2::DllObjectManger<T>::print_info() const
{
}
#endif // #ifndef __HBSTOCK2_MODEL_BASE_DEF_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -