32 #ifndef CLASS_LOADER__MULTI_LIBRARY_CLASS_LOADER_HPP_ 33 #define CLASS_LOADER__MULTI_LIBRARY_CLASS_LOADER_HPP_ 35 #include <boost/thread.hpp> 41 #include "console_bridge/console.h" 80 CONSOLE_BRIDGE_logDebug(
81 "class_loader::MultiLibraryClassLoader: " 82 "Attempting to create instance of class type %s.",
84 ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
85 if (
nullptr == loader) {
87 "MultiLibraryClassLoader: Could not create object of class type " +
89 " as no factory exists for it. Make sure that the library exists and " 90 "was explicitly loaded through MultiLibraryClassLoader::loadLibrary()");
93 return loader->createSharedInstance<
Base>(class_name);
105 std::shared_ptr<Base>
108 ClassLoader * loader = getClassLoaderForLibrary(library_path);
109 if (
nullptr == loader) {
111 "Could not create instance as there is no ClassLoader in " 112 "MultiLibraryClassLoader bound to library " + library_path +
113 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
125 CONSOLE_BRIDGE_logDebug(
126 "class_loader::MultiLibraryClassLoader: " 127 "Attempting to create instance of class type %s.",
129 ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
130 if (
nullptr == loader) {
132 "MultiLibraryClassLoader: Could not create object of class type " +
134 " as no factory exists for it. Make sure that the library exists and " 135 "was explicitly loaded through MultiLibraryClassLoader::loadLibrary()");
138 return loader->createInstance<
Base>(class_name);
146 boost::shared_ptr<Base>
149 ClassLoader * loader = getClassLoaderForLibrary(library_path);
150 if (
nullptr == loader) {
152 "Could not create instance as there is no ClassLoader in " 153 "MultiLibraryClassLoader bound to library " + library_path +
154 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
166 CONSOLE_BRIDGE_logDebug(
167 "class_loader::MultiLibraryClassLoader: Attempting to create instance of class type %s.",
169 ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
170 if (
nullptr == loader) {
172 "MultiLibraryClassLoader: Could not create object of class type " + class_name +
173 " as no factory exists for it. " 174 "Make sure that the library exists and was explicitly loaded through " 175 "MultiLibraryClassLoader::loadLibrary()");
177 return loader->createUniqueInstance<
Base>(class_name);
188 ClassLoader * loader = getClassLoaderForLibrary(library_path);
189 if (
nullptr == loader) {
191 "Could not create instance as there is no ClassLoader in " 192 "MultiLibraryClassLoader bound to library " + library_path +
193 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
209 ClassLoader * loader = getClassLoaderForClass<Base>(class_name);
210 if (
nullptr == loader) {
212 "MultiLibraryClassLoader: Could not create class of type " + class_name);
228 ClassLoader * loader = getClassLoaderForLibrary(library_path);
229 if (
nullptr == loader) {
231 "Could not create instance as there is no ClassLoader in MultiLibraryClassLoader " 232 "bound to library " + library_path +
233 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
247 std::vector<std::string> available_classes = getAvailableClasses<Base>();
248 return available_classes.end() != std::find(
249 available_classes.begin(), available_classes.end(), class_name);
257 bool isLibraryAvailable(
const std::string & library_path);
267 std::vector<std::string> available_classes;
268 for (
auto & loader : getAllAvailableClassLoaders()) {
269 std::vector<std::string> loader_classes = loader->getAvailableClasses<
Base>();
270 available_classes.insert(
271 available_classes.end(), loader_classes.begin(), loader_classes.end());
273 return available_classes;
284 ClassLoader * loader = getClassLoaderForLibrary(library_path);
285 if (
nullptr == loader) {
287 "There is no ClassLoader in MultiLibraryClassLoader bound to library " +
289 " Ensure you called MultiLibraryClassLoader::loadLibrary()");
298 std::vector<std::string> getRegisteredLibraries();
304 void loadLibrary(
const std::string & library_path);
323 ClassLoader * getClassLoaderForLibrary(
const std::string & library_path);
330 template<
typename Base>
333 ClassLoaderVector loaders = getAllAvailableClassLoaders();
334 for (ClassLoaderVector::iterator i = loaders.begin(); i != loaders.end(); ++i) {
335 if (!(*i)->isLibraryLoaded()) {
338 if ((*i)->isClassAvailable<
Base>(class_name)) {
348 ClassLoaderVector getAllAvailableClassLoaders();
353 void shutdownAllClassLoaders();
363 #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::UniquePtr< Base > createUniqueInstance(const std::string &class_name)
Creates an instance of an object of given class name with ancestor class Base Same as createSharedIns...
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.
CLASS_LOADER_PUBLIC void loadLibrary(const std::string &library_path, ClassLoader *loader)
Loads a library into memory if it has not already been done so. Attempting to load an already loaded ...
std::vector< std::string > getAvailableClassesForLibrary(const std::string &library_path)
Gets a list of all classes loaded for a particular library.
UniquePtr< Base > createUniqueInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
Base * createUnmanagedInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
std::shared_ptr< Base > createSharedInstance(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...
std::vector< std::string > getAvailableClasses()
Gets a list of all classes that are loaded by the class loader.
ClassLoader::UniquePtr< Base > createUniqueInstance(const std::string &class_name, const std::string &library_path)
Creates an instance of an object of given class name with ancestor class Base Same as createSharedIns...
CLASS_LOADER_PUBLIC void unloadLibrary(const std::string &library_path, ClassLoader *loader)
Unloads a library if it loaded in memory and cleans up its corresponding class factories. If it is not loaded, the function has no effect.
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
std::shared_ptr< Base > createSharedInstance(const std::string &derived_class_name)
Generates an instance of loadable classes (i.e. class_loader).
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_
std::shared_ptr< Base > createSharedInstance(const std::string &class_name)
Creates an instance of an object of given class name with ancestor class Base This version does not l...
An exception class thrown when a multilibrary class loader does not have a ClassLoader bound to it...
std::map< LibraryPath, class_loader::ClassLoader * > LibraryToClassLoaderMap
std::unique_ptr< Base, DeleterType< Base > > UniquePtr
#define CLASS_LOADER_PUBLIC
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_
boost::mutex loader_mutex_
bool isOnDemandLoadUnloadEnabled()
Indicates if on-demand (lazy) load/unload is enabled so libraries are loaded/unloaded automatically a...
boost::shared_ptr< Base > createInstance(const std::string &class_name)
Creates an instance of an object of given class name with ancestor class Base Same as createSharedIns...
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 Same as createSharedIns...