shared_library_UNIX.cpp
Go to the documentation of this file.
00001 #include <string>
00002 #include <mutex>
00003 #include <dlfcn.h>
00004 #include "behaviortree_cpp/shared_library.h"
00005 
00006 namespace BT
00007 {
00008 SharedLibrary::SharedLibrary()
00009 {
00010     _handle = nullptr;
00011 }
00012 
00013 void SharedLibrary::load(const std::string& path, int)
00014 {
00015     std::unique_lock<std::mutex> lock(_mutex);
00016 
00017     if (_handle)
00018     {
00019         throw std::runtime_error("Library already loaded: " + path);
00020     }
00021 
00022     _handle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
00023     if (!_handle)
00024     {
00025         const char* err = dlerror();
00026         throw std::runtime_error("Could not load library: " + (err ? std::string(err) : path));
00027     }
00028     _path = path;
00029 }
00030 
00031 void SharedLibrary::unload()
00032 {
00033     std::unique_lock<std::mutex> lock(_mutex);
00034 
00035     if (_handle)
00036     {
00037         dlclose(_handle);
00038         _handle = nullptr;
00039     }
00040 }
00041 
00042 bool SharedLibrary::isLoaded() const
00043 {
00044     return _handle != nullptr;
00045 }
00046 
00047 void* SharedLibrary::findSymbol(const std::string& name)
00048 {
00049     std::unique_lock<std::mutex> lock(_mutex);
00050 
00051     void* result = nullptr;
00052     if (_handle)
00053     {
00054         result = dlsym(_handle, name.c_str());
00055     }
00056     return result;
00057 }
00058 
00059 const std::string& SharedLibrary::getPath() const
00060 {
00061     return _path;
00062 }
00063 
00064 std::string SharedLibrary::prefix()
00065 {
00066 #if BT_OS == BT_OS_CYGWIN
00067     return "cyg";
00068 #else
00069     return "lib";
00070 #endif
00071 }
00072 
00073 std::string SharedLibrary::suffix()
00074 {
00075 #if BT_OS == BT_OS_MAC_OS_X
00076 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
00077     return "d.dylib";
00078 #else
00079     return ".dylib";
00080 #endif
00081 #elif BT_OS == BT_OS_HPUX
00082 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
00083     return "d.sl";
00084 #else
00085     return ".sl";
00086 #endif
00087 #elif BT_OS == BT_OS_CYGWIN
00088 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
00089     return "d.dll";
00090 #else
00091     return ".dll";
00092 #endif
00093 #else
00094 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
00095     return "d.so";
00096 #else
00097     return ".so";
00098 #endif
00099 #endif
00100 }
00101 
00102 }   // namespace


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10