36 #if defined(__linux__) or defined(__APPLE__)
42 #include <boost/format.hpp>
44 #include "../logging/Logger.h"
45 #include "../logging/LoggerFactory.h"
49 using namespace boost;
59 Impl(
const std::string& name,
60 const std::string& library)
61 : logger(logging::LoggerFactory::getInstance()
62 .
getLogger(str((format(
"rsc.plugins.Plugin[%1%]")
64 name(name), library(library),
65 loaded(false), handle(NULL),
66 init(NULL), shutdown(NULL) {
77 void load(
bool wrapExceptions) {
83 "Plugin %1% is already loaded. Cannot load it again,")
87 RSCINFO(this->logger,
"Trying to load library `" << this->library <<
"'");
99 RSCINFO(this->logger,
"Initializing");
100 if (wrapExceptions) {
104 }
catch (
const std::exception& e) {
105 throw runtime_error(str(format(
"Plugin `%1%' failed to initialize: %2%")
109 throw runtime_error(str(format(
"Plugin `%1%' failed to initialize due to unknown error.")
122 str(format(
"Plugin `%1%' failed cannot be unloaded because it has not been loaded correctly.")
127 RSCINFO(this->logger,
"Shutting down");
128 this->loaded =
false;
130 if (wrapExceptions) {
133 }
catch (
const std::exception& e) {
134 throw runtime_error(str(format(
"Plugin `%1%' failed to shutdown: %2%")
138 throw runtime_error(str(format(
"Plugin `%1%' failed to shutdown due to unknown error.")
146 typedef void (*InitFunction)();
147 typedef void (*ShutdownFunction)();
165 #if defined(__linux__) || defined(__APPLE__)
166 if (!(this->handle = dlopen(this->library.c_str(), RTLD_NOW))) {
167 const char* result = dlerror();
168 throw runtime_error(str(format(
"Failed to load plugin `%1%' from shared object `%2%': %3%.")
171 % (result ? result :
"<unknown error>")));
173 #elif defined(_WIN32)
174 if (!(this->handle= LoadLibrary(this->library.c_str()))) {
175 throw runtime_error(str(format(
"Failed to load plugin `%1%' from shared object `%2%': %3%.")
181 throw runtime_error(
"Plugins are not implemented for this platform.");
186 RSCINFO(this->logger,
"Resolving symbol `"
188 <<
"' in library `" << this->library <<
"'");
190 assert(this->handle);
193 #if defined(__linux__) || defined(__APPLE__)
194 if (!(address = dlsym(this->handle, name.c_str()))) {
195 const char* result = dlerror();
196 throw runtime_error(str(format(
"Plugin `%1%' failed to define function `%2%': %3%")
199 % (result ? result :
"<unknown error>")));
201 #elif defined(_WIN32)
202 if (!(address = GetProcAddress(this->handle, name.c_str()))) {
203 throw runtime_error(str(format(
"Plugin `%1%' failed to define function `%2%': %3%")
209 throw runtime_error(
"Plugins are not implemented for this platform.");
223 return this->
impl->getName();
227 this->
impl->load(wrapExceptions);
231 this->
impl->unload(wrapExceptions);
235 return this->
impl->getLibrary();