31 #define REGISTER_SO_LIB_(class_name, base_class) \
32 extern "C" base_class* create_instance() \
34 return new class_name(); \
37 extern "C" void destroy_instance( base_class* instance ) \
56 static std::shared_ptr<T> getFactory (
const std::string& path_to_so,
const std::string& lib_name ) {
59 lib_handle = dlopen ( path_to_so.c_str(), RTLD_NOW );
63 Logger::success() << lib_name <<
" so library found! " << Logger::endl();
64 handles[lib_name] = lib_handle;
67 create = ( T* ( * ) () ) dlsym ( lib_handle,
"create_instance" );
68 if ( ( error = dlerror() ) != NULL ) {
73 T* instance = ( T* ) create();
74 if ( instance !=
nullptr ) {
75 return std::shared_ptr<T> ( instance );
85 static void unloadLib (
const std::string& file_name ) {
86 dlclose ( handles[file_name] );
87 Logger::info() << file_name <<
" so library unloaded! " << Logger::endl();
90 template <
class T,
typename... Args>
91 static std::shared_ptr<T> getFactoryWithArgs (
const std::string& path_to_so,
const std::string& lib_name, Args... args ) {
94 lib_handle = dlopen ( path_to_so.c_str(), RTLD_NOW );
98 Logger::success() << lib_name <<
" so library found! " << Logger::endl();
99 handles[lib_name] = lib_handle;
101 T* ( *create ) ( Args... args );
102 create = ( T* ( * ) ( Args... args ) ) dlsym ( lib_handle,
"create_instance" );
103 if ( ( error = dlerror() ) != NULL ) {
108 T* instance = ( T* ) create ( args... );
109 if ( instance !=
nullptr ) {
110 return std::shared_ptr<T> ( instance );
124 static std::map<std::string, void*> handles;
127 const std::string& middle_path,
128 std::string& absolute_path ) {
130 if ( ! ( input_path.at ( 0 ) ==
'/' ) ) {
132 const char* env_p = std::getenv (
"XBOT_ROOT" );
135 std::string current_path ( env_p );
137 current_path += middle_path;
138 current_path += input_path;
139 absolute_path = current_path;
142 std::cerr <<
"ERROR in " << __func__ <<
" : the input path " << input_path <<
" is neither an absolute path nor related with the robotology superbuild. Download it!" << std::endl;
147 absolute_path = input_path;
154 std::map<std::string, void*> SoLib::handles;