1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/*************************************************************************
* Copyright (C) 2004 by Olivier Galizzi *
* olivier.galizzi@imag.fr *
* with help from Bronek Kozicki *
* *
* This program is free software; it is licensed under the terms of the *
* GNU General Public License v2 or later. See file LICENSE for details. *
*************************************************************************/
#include "DynLibManager.hpp"
#include "ClassFactory.hpp"
namespace yade { // Cannot have #include directive inside.
CREATE_LOGGER(DynLibManager);
DynLibManager::DynLibManager() { autoUnload = true; }
DynLibManager::~DynLibManager()
{
if (autoUnload) unloadAll();
}
// load plugin with given filename
bool DynLibManager::load(const string& lib)
{
if (lib.empty()) throw std::runtime_error(__FILE__ ": got empty library name to load.");
void* handle = dlopen(lib.c_str(), RTLD_GLOBAL | RTLD_NOW);
if (!handle) return !error();
handles[lib] = handle;
return true;
}
// unload plugin, given full filename
bool DynLibManager::unload(const string& libName)
{
if (isLoaded(libName)) return closeLib(libName);
else
return false;
}
bool DynLibManager::unloadAll()
{
std::map<const string, void*>::iterator ith = handles.begin();
std::map<const string, void*>::iterator ithEnd = handles.end();
for (; ith != ithEnd; ++ith)
if ((*ith).first.length() != 0) unload((*ith).first);
return false;
}
bool DynLibManager::isLoaded(const string& libName)
{
std::map<const string, void*>::iterator ith = handles.find(libName);
return (ith != handles.end() && (*ith).second != NULL);
}
void DynLibManager::setAutoUnload(bool enabled) { autoUnload = enabled; }
bool DynLibManager::closeLib(const string libName)
{
dlclose(handles[libName]);
return !error();
}
std::string DynLibManager::lastError() { return lastError_; }
bool DynLibManager::error()
{
char* error = dlerror();
if (error != NULL) { lastError_ = error; }
return (error != NULL);
}
} // namespace yade
|