32 #ifndef CLASS_LOADER__MULTI_LIBRARY_CLASS_LOADER_HPP_ 33 #define CLASS_LOADER__MULTI_LIBRARY_CLASS_LOADER_HPP_ 35 #include <boost/thread.hpp> 40 #include "console_bridge/console.h" 83 "class_loader::MultiLibraryClassLoader: " 84 "Attempting to create instance of class type %s.",
86 ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
89 "MultiLibraryClassLoader: Could not create object of class type " +
91 " as no factory exists for it. Make sure that the library exists and " 92 "was explicitly loaded through MultiLibraryClassLoader::loadLibrary()");
95 return loader->createInstance<
Base>(class_name);
107 boost::shared_ptr<Base>
111 if (NULL == loader) {
113 "Could not create instance as there is no ClassLoader in " 114 "MultiLibraryClassLoader bound to library " + library_path +
115 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
120 #if __cplusplus >= 201103L 129 ClassLoader::UniquePtr<Base> createUniqueInstance(
const std::string & class_name)
132 "class_loader::MultiLibraryClassLoader: Attempting to create instance of class type %s.",
134 ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
135 if (
nullptr == loader) {
137 "MultiLibraryClassLoader: Could not create object of class type " + class_name +
138 " as no factory exists for it. " 139 "Make sure that the library exists and was explicitly loaded through " 140 "MultiLibraryClassLoader::loadLibrary()");
142 return loader->createUniqueInstance<
Base>(class_name);
154 ClassLoader::UniquePtr<Base>
155 createUniqueInstance(
const std::string & class_name,
const std::string & library_path)
158 if (
nullptr == loader) {
160 "Could not create instance as there is no ClassLoader in " 161 "MultiLibraryClassLoader bound to library " + library_path +
162 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
164 return loader->createUniqueInstance<
Base>(class_name);
179 ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
180 if (NULL == loader) {
182 "MultiLibraryClassLoader: Could not create class of type " + class_name);
199 if (NULL == loader) {
201 "Could not create instance as there is no ClassLoader in MultiLibraryClassLoader " 202 "bound to library " + library_path +
203 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
217 std::vector<std::string> available_classes = getAvailableClasses<Base>();
218 return available_classes.end() != std::find(
219 available_classes.begin(), available_classes.end(), class_name);
237 std::vector<std::string> available_classes;
239 for (
unsigned int c = 0; c < loaders.size(); c++) {
242 available_classes.insert(
243 available_classes.end(), loader_classes.begin(), loader_classes.end());
245 return available_classes;
257 std::vector<std::string> available_classes;
260 return available_classes;
263 "There is no ClassLoader in MultiLibraryClassLoader bound to library " +
265 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
279 void loadLibrary(
const std::string & library_path);
305 template<
typename Base>
309 for (ClassLoaderVector::iterator i = loaders.begin(); i != loaders.end(); ++i) {
310 if (!(*i)->isLibraryLoaded()) {
313 if ((*i)->isClassAvailable<
Base>(class_name)) {
338 #endif // CLASS_LOADER__MULTI_LIBRARY_CLASS_LOADER_HPP_
std::vector< std::string > getAvailableClasses()
Indicates which classes (i.e. class_loader) that can be loaded by this object.
ClassLoader * getClassLoaderForClass(const std::string &class_name)
Gets a handle to the class loader corresponding to a specific class.
A ClassLoader that can bind more than one runtime library.
std::vector< std::string > getAvailableClassesForLibrary(const std::string &library_path)
Gets a list of all classes loaded for a particular library.
bool isLibraryAvailable(const std::string &library_path)
Indicates if a library has been loaded into memory.
Base * createUnmanagedInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
std::vector< std::string > getAvailableClasses()
Gets a list of all classes that are loaded by the class loader.
int unloadLibrary(const std::string &library_path)
Unloads a library for this class loader.
#define CONSOLE_BRIDGE_logDebug(fmt,...)
An exception class thrown when class_loader is unable to create a plugin.
This class allows loading and unloading of dynamically linked libraries which contain class definitio...
std::vector< ClassLoader * > ClassLoaderVector
Base * createUnmanagedInstance(const std::string &class_name, const std::string &library_path)
Creates an instance of an object of given class name with ancestor class Base This version takes a sp...
LibraryToClassLoaderMap active_class_loaders_
void loadLibrary(const std::string &library_path)
Loads a library into memory for this class loader.
An exception class thrown when a multilibrary class loader does not have a ClassLoader bound to it...
void shutdownAllClassLoaders()
Destroys all ClassLoaders.
std::map< LibraryPath, class_loader::ClassLoader * > LibraryToClassLoaderMap
virtual ~MultiLibraryClassLoader()
Virtual destructor for class.
std::vector< std::string > getRegisteredLibraries()
Gets a list of all libraries opened by this class loader .
MultiLibraryClassLoader(bool enable_ondemand_loadunload)
Constructor for the class.
boost::shared_ptr< Base > createInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
bool enable_ondemand_loadunload_
ClassLoaderVector getAllAvailableClassLoaders()
Gets all class loaders loaded within scope.
boost::mutex loader_mutex_
bool isOnDemandLoadUnloadEnabled()
Indicates if on-demand (lazy) load/unload is enabled so libraries are loaded/unloaded automatically a...
ClassLoader * getClassLoaderForLibrary(const std::string &library_path)
Gets a handle to the class loader corresponding to a specific runtime library.
boost::shared_ptr< Base > createInstance(const std::string &class_name)
Creates an instance of an object of given class name with ancestor class Base This version does not l...
bool isClassAvailable(const std::string &class_name)
Indicates if a class has been loaded and can be instantiated.
Base * createUnmanagedInstance(const std::string &class_name)
Creates an instance of an object of given class name with ancestor class Base This version does not l...
boost::shared_ptr< Base > createInstance(const std::string &class_name, const std::string &library_path)
Creates an instance of an object of given class name with ancestor class Base This version takes a sp...