17 CDynamicLoading::TDLHandleMap CDynamicLoading::m_tOpenLibs;
18 const std::string CDynamicLoading::DEFAULT_PLUGIN_PATH = ARGOS_INSTALL_PREFIX
"/lib/argos3/";
33 std::string& str_msg) {
36 str_msg +=
"\n " + str_lib +
": ";
37 if(tHandle ==
nullptr) {
40 std::string strLibWExt = str_lib +
"." + ARGOS_SHARED_LIBRARY_EXTENSION;
41 tHandle = ::dlopen(strLibWExt.c_str(), RTLD_GLOBAL | RTLD_LAZY);
42 str_msg +=
"\n\n " + strLibWExt +
": ";
43 if(tHandle !=
nullptr) {
50 strLibWExt = str_lib +
"." + ARGOS_MODULE_LIBRARY_EXTENSION;
51 tHandle = ::dlopen(strLibWExt.c_str(), RTLD_GLOBAL | RTLD_LAZY);
52 str_msg +=
"\n\n " + strLibWExt +
": ";
53 if(tHandle !=
nullptr) {
63 if(tHandle !=
nullptr) {
75 if(str_lib[0] ==
'/') {
80 auto it = m_tOpenLibs.find(str_lib);
81 if(it != m_tOpenLibs.end()) {
83 return m_tOpenLibs[str_lib];
86 std::string strLoadedLib = str_lib;
88 tHandle = LoadLibraryTryingExtensions(strLoadedLib, strMsg);
89 if(tHandle ==
nullptr) {
92 <<
"\" after trying the following: "
97 m_tOpenLibs[strLoadedLib] = tHandle;
98 LOG <<
"[INFO] Loaded library \"" << strLoadedLib <<
"\"" << std::endl;
107 std::string strLibPath;
109 std::string strPluginPath =
".:" + DEFAULT_PLUGIN_PATH;
111 if(::getenv(
"ARGOS_PLUGIN_PATH") !=
nullptr) {
113 strPluginPath = std::string(::getenv(
"ARGOS_PLUGIN_PATH")) +
":" + strPluginPath;
116 if(strPluginPath[strPluginPath.length()-1] !=
':') {
117 strPluginPath.append(
":");
123 std::istringstream issPluginPath(strPluginPath);
124 std::string strDir, strMsg;
125 while(std::getline(issPluginPath, strDir,
':')) {
127 if(strDir[strDir.length()-1] !=
'/') {
130 strLibPath = strDir + str_lib;
132 auto it = m_tOpenLibs.find(strLibPath);
133 if(it != m_tOpenLibs.end()) {
135 return m_tOpenLibs[strLibPath];
138 tHandle = LoadLibraryTryingExtensions(strLibPath, strMsg);
139 if(tHandle !=
nullptr) {
141 m_tOpenLibs[strLibPath] = tHandle;
142 LOG <<
"[INFO] Loaded library \"" << strLibPath <<
"\"" << std::endl;
150 <<
"\" after trying the following: "
160 auto it = m_tOpenLibs.find(str_lib);
161 if(it != m_tOpenLibs.end()) {
162 if(::dlclose(it->second) != 0) {
163 LOGERR <<
"[WARNING] Can't unload library \""
174 <<
"\": library does not appear to have been loaded.");
183 std::string strLibPath;
185 std::string strPluginPath = DEFAULT_PLUGIN_PATH;
187 if(::getenv(
"ARGOS_PLUGIN_PATH") !=
nullptr) {
189 strPluginPath = std::string(::getenv(
"ARGOS_PLUGIN_PATH")) +
":" + strPluginPath;
192 if(strPluginPath[strPluginPath.length()-1] !=
':') {
193 strPluginPath.append(
":");
200 struct dirent* ptDirData;
202 std::istringstream issPluginPath(strPluginPath);
204 while(std::getline(issPluginPath, strDir,
':')) {
206 if(strDir[strDir.length()-1] !=
'/') {
210 ptDir = ::opendir(strDir.c_str());
211 if(ptDir !=
nullptr) {
213 while((ptDirData = ::readdir(ptDir)) !=
nullptr) {
215 if(strlen(ptDirData->d_name) > strlen(ARGOS_SHARED_LIBRARY_EXTENSION) &&
216 std::string(ptDirData->d_name).rfind(
"." ARGOS_SHARED_LIBRARY_EXTENSION) +
217 strlen(ARGOS_SHARED_LIBRARY_EXTENSION) + 1 == strlen(ptDirData->d_name)) {
221 if(strcmp(ARGOS_SHARED_LIBRARY_EXTENSION, ARGOS_MODULE_LIBRARY_EXTENSION) != 0) {
222 if(strlen(ptDirData->d_name) > strlen(ARGOS_MODULE_LIBRARY_EXTENSION) &&
223 std::string(ptDirData->d_name).rfind(
"." ARGOS_MODULE_LIBRARY_EXTENSION) +
224 strlen(ARGOS_MODULE_LIBRARY_EXTENSION) + 1 == strlen(ptDirData->d_name)) {
235 LOGERR <<
"[WARNING] Error opening directory \""
249 for(
auto it = m_tOpenLibs.begin();
250 it != m_tOpenLibs.end();
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
CARGoSLog LOGERR(std::cerr, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_RED))
CARGoSLog LOG(std::cout, SLogColor(ARGOS_LOG_ATTRIBUTE_BRIGHT, ARGOS_LOG_COLOR_GREEN))
void * TDLHandle
The handle to a loaded library.
static void UnloadAllLibraries()
Unloads all the dynamic libraries.
static TDLHandle LoadLibrary(const std::string &str_lib)
Loads a dynamic library.
static void LoadAllLibraries()
Loads all the dynamic libraries in the current ARGOS_PLUGIN_PATH.
static void UnloadLibrary(const std::string &str_lib)
Unloads a dynamic library.