28 #ifndef ICL_CORE_PLUGIN_PLUGIN_MANAGER_H_INCLUDED 29 #define ICL_CORE_PLUGIN_PLUGIN_MANAGER_H_INCLUDED 35 #include <boost/filesystem.hpp> 36 #include <boost/algorithm/string/predicate.hpp> 47 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 59 template <
class T, const
char* const plugin_dir>
91 bool initialize(
bool load_lazy =
true,
bool recursive_search =
false)
143 std::list<std::string> result;
144 typename std::map<std::string, PluginHandle>::const_iterator iter;
147 result.push_back(iter->first);
150 typename std::map<std::string, T*>::const_iterator siter;
153 result.push_back(siter->first);
173 std::list<T*> result;
174 typename std::map<std::string, T*>::const_iterator iter;
177 result.push_back(iter->second);
180 typename std::map<std::string, T*>::const_iterator siter;
183 result.push_back(siter->second);
252 LOGGING_DEBUG(Plugin,
"Static plugin " << identifier <<
" removed." 266 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 279 bool recursive_search =
false)
281 {
return initialize(load_lazy, recursive_search); }
310 {
return plugin(identifier); }
364 for (StringList::const_iterator iter = paths.begin(); iter != paths.end(); ++iter)
366 std::string path = *iter;
367 boost::filesystem::path bpath(path);
373 found = exists(bpath);
375 catch (
const boost::filesystem::filesystem_error &e)
377 LOGGING_DEBUG(Plugin,
"Exception when examining directory " << path
388 if (!recursive_search)
390 boost::filesystem::directory_iterator end_iter;
391 for (boost::filesystem::directory_iterator dir_iter(bpath); dir_iter != end_iter; ++dir_iter)
393 if (boost::filesystem::is_regular_file(dir_iter->status()))
395 #if BOOST_FILESYSTEM_VERSION == 2 405 boost::filesystem::recursive_directory_iterator end_iter;
406 for (boost::filesystem::recursive_directory_iterator dir_iter(bpath);
407 dir_iter != end_iter; ++dir_iter)
409 if (boost::filesystem::is_regular_file(dir_iter->status()))
411 #if BOOST_FILESYSTEM_VERSION == 2 430 if (boost::algorithm::ends_with(plugin,
".lib")) {
436 PluginHandle plugin_handle = dlopen(plugin.c_str(), RTLD_LAZY);
439 typename T::identifier* get_identifier
440 = (
typename T::identifier*) dlsym(plugin_handle,
"identifier");
442 typename T::basetype* get_basetype
443 = (
typename T::basetype*) dlsym(plugin_handle,
"basetype");
445 const char* dlsym_error = dlerror();
450 unsigned error_code = GetLastError();
452 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
453 | FORMAT_MESSAGE_FROM_SYSTEM,
456 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
457 (LPTSTR) &msg_buffer,
460 std::ostringstream error;
461 error <<
"Could not open dll: (" << error_code <<
") " << msg_buffer;
465 printf(
"could not open dll: (%d) %s\n", error_code, msg_buffer);
466 LocalFree(msg_buffer);
468 std::ostringstream error;
469 error <<
"Cannot load plugin " << plugin <<
": " << dlsym_error;
477 if (!get_identifier || !get_basetype)
479 std::ostringstream error;
480 error <<
"Identifier or basetype method missing in plugin " <<
plugin;
484 else if (strcmp(get_basetype(),
typeid(T).name()) != 0)
486 LOGGING_WARNING(Plugin,
"Plugin type mismatch: Exptected " <<
typeid(T).name()
492 LOGGING_DEBUG(Plugin,
"Initialized plugin " << get_identifier() <<
" of basetype " 505 typename std::map<std::string, PluginHandle>::const_iterator iter;
537 delete plugin_instance;
541 dlclose(plugin_handle);
555 typename T::load_plugin* create_instance
556 = (
typename T::load_plugin*) dlsym(plugin_handle,
558 const char* dlsym_error = dlerror();
563 unsigned error_code = GetLastError();
565 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
566 | FORMAT_MESSAGE_FROM_SYSTEM,
569 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
570 (LPTSTR) &msg_buffer,
573 std::ostringstream error;
574 error <<
"Could not open dll: (" << error_code <<
") " << msg_buffer;
577 LocalFree(msg_buffer);
579 std::ostringstream error;
580 error <<
"Cannot load plugin " << identifier <<
": " << dlsym_error;
588 T* plugin_instance = create_instance();
589 if (!plugin_instance)
591 std::ostringstream error;
592 error <<
"Cannot cast plugin " << identifier;
600 return plugin_instance;
608 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
void addStaticPlugin(T *instance)
#define ICL_CORE_VC_DEPRECATE_STYLE
Contains import/export definitions for the Win32 plattform.
void addErrorMessage(const std::string &message)
T * createPluginInstance(const std::string &identifier)
bool isInitialized() const
Contains macros to deprecate classes, types, functions and variables.
T * plugin(const std::string &identifier)
#define LOGGING_DEBUG(streamname, arg)
void removeStaticPlugin(const std::string &identifier)
void loadHandle(std::string plugin)
std::map< std::string, PluginHandle > m_plugin_handles
Loadable plugin handles.
ICL_CORE_VC_DEPRECATE_STYLE bool Initialize(int &argc, char *argv[], bool remove_read_arguments) ICL_CORE_GCC_DEPRECATE_STYLE
#define LOGGING_ERROR(streamname, arg)
static PluginManager< T, plugin_dir > * instance()
Singleton instance accessor.
std::list< T * > plugins()
Contains logging definitions for the icl_core_plugin library.
ThreadStream & endl(ThreadStream &stream)
std::map< std::string, T * > m_loaded_plugins
Plugins loaded from disk via ltdl.
void initializePlugins(bool recursive_search)
bool isStaticPlugin(const std::string &identifier) const
#define LOGGING_WARNING(streamname, arg)
Contains PluginManagerBase.
std::list< std::string > availablePlugins()
void closePluginHandle(const std::string &identifier)
std::map< std::string, T * > m_static_plugins
Plugins available from an already loaded lib, added manually.
std::list< std::string > StringList
PluginManager()
Protected default constructor.
bool initialize(bool load_lazy=true, bool recursive_search=false)
Initializes the PluginManager.
void clearErrorMessages()
T * loadPluginInstance(PluginHandle plugin_handle, const std::string &identifier)
#define LOGGING_TRACE(streamname, arg)
#define ICL_CORE_GCC_DEPRECATE_STYLE
void unloadPlugin(const std::string &identifier)