class_loader  master
The class_loader package is a ROS-independent package for loading plugins during runtime.
Public Types | Public Member Functions | List of all members
class_loader::ClassLoader Class Reference

This class allows loading and unloading of dynamically linked libraries which contain class definitions from which objects can be created/destroyed during runtime (i.e. class_loader). Libraries loaded by a ClassLoader are only accessible within scope of that ClassLoader object. More...

#include <class_loader.hpp>

Public Types

template<typename Base >
using DeleterType = std::function< void(Base *)>
 
template<typename Base >
using UniquePtr = std::unique_ptr< Base, DeleterType< Base > >
 

Public Member Functions

 ClassLoader (const std::string &library_path, bool ondemand_load_unload=false)
 Constructor for ClassLoader. More...
 
virtual ~ClassLoader ()
 Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically. More...
 
template<class Base >
std::vector< std::stringgetAvailableClasses () const
 Indicates which classes (i.e. class_loader) that can be loaded by this object. More...
 
template<class Base >
std::shared_ptr< Base > createInstance (const std::string &derived_class_name)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
UniquePtr< Base > createUniqueInstance (const std::string &derived_class_name)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
Base * createUnmanagedInstance (const std::string &derived_class_name)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
bool isClassAvailable (const std::string &class_name) const
 Indicates if a plugin class is available. More...
 
const std::stringgetLibraryPath () const
 Gets the full-qualified path and name of the library associated with this class loader. More...
 
bool isLibraryLoaded () const
 Indicates if a library is loaded within the scope of this ClassLoader. More...
 
bool isLibraryLoadedByAnyClassloader () const
 Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader. More...
 
bool isOnDemandLoadUnloadEnabled () const
 Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when the first plugin is created and automatically shut it down when last active plugin is destroyed. More...
 
void loadLibrary ()
 Attempts to load a library on behalf of the ClassLoader. If the library is already opened, this method has no effect. If the library has been already opened by some other entity (i.e. another ClassLoader or global interface), this object is given permissions to access any plugin classes loaded by that other entity. More...
 
int unloadLibrary ()
 Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened, this method has no effect. If the library is opened by other another ClassLoader, the library will NOT be unloaded internally – however this ClassLoader will no longer be able to instantiate class_loader bound to that library. If there are plugin objects that exist in memory created by this classloader, a warning message will appear and the library will not be unloaded. If loadLibrary() was called multiple times (e.g. in the case of multiple threads or purposefully in a single thread), the user is responsible for calling unloadLibrary() the same number of times. The library will not be unloaded within the context of this classloader until the number of unload calls matches the number of loads. More...
 

Detailed Description

This class allows loading and unloading of dynamically linked libraries which contain class definitions from which objects can be created/destroyed during runtime (i.e. class_loader). Libraries loaded by a ClassLoader are only accessible within scope of that ClassLoader object.

A base class for all class_loader exceptions that inherits from std::runtime_exception.

Member Typedef Documentation

◆ DeleterType

template<typename Base >
using class_loader::ClassLoader::DeleterType = std::function<void (Base *)>

◆ UniquePtr

template<typename Base >
using class_loader::ClassLoader::UniquePtr = std::unique_ptr<Base, DeleterType<Base> >

Constructor & Destructor Documentation

◆ ClassLoader()

class_loader::ClassLoader::ClassLoader ( const std::string library_path,
bool  ondemand_load_unload = false 
)
explicit

Constructor for ClassLoader.

Parameters
library_path- The path of the runtime library to load
ondemand_load_unload- Indicates if on-demand (lazy) unloading/loading of libraries occurs as plugins are created/destroyed.

◆ ~ClassLoader()

virtual class_loader::ClassLoader::~ClassLoader ( )
virtual

Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically.

Member Function Documentation

◆ getAvailableClasses()

template<class Base >
std::vector<std::string> class_loader::ClassLoader::getAvailableClasses ( ) const
inline

Indicates which classes (i.e. class_loader) that can be loaded by this object.

Returns
vector of strings indicating names of instantiable classes derived from <Base>

◆ createInstance()

template<class Base >
std::shared_ptr<Base> class_loader::ClassLoader::createInstance ( const std::string derived_class_name)
inline

Generates an instance of loadable classes (i.e. class_loader).

It is not necessary for the user to call loadLibrary() as it will be invoked automatically if the library is not yet loaded (which typically happens when in "On Demand Load/Unload" mode).

Parameters
derived_class_nameThe name of the class we want to create (
See also
getAvailableClasses())
Returns
A std::shared_ptr<Base> to newly created plugin object

◆ createUniqueInstance()

template<class Base >
UniquePtr<Base> class_loader::ClassLoader::createUniqueInstance ( const std::string derived_class_name)
inline

Generates an instance of loadable classes (i.e. class_loader).

It is not necessary for the user to call loadLibrary() as it will be invoked automatically if the library is not yet loaded (which typically happens when in "On Demand Load/Unload" mode).

If you release the wrapped pointer you must manually call the original deleter when you want to destroy the released pointer.

Parameters
derived_class_nameThe name of the class we want to create (
See also
getAvailableClasses()).
Returns
A std::unique_ptr<Base> to newly created plugin object.

◆ createUnmanagedInstance()

template<class Base >
Base* class_loader::ClassLoader::createUnmanagedInstance ( const std::string derived_class_name)
inline

Generates an instance of loadable classes (i.e. class_loader).

It is not necessary for the user to call loadLibrary() as it will be invoked automatically if the library is not yet loaded (which typically happens when in "On Demand Load/Unload" mode).

Creating an unmanaged instance disables dynamically unloading libraries when managed pointers go out of scope for all class loaders in this process.

Parameters
derived_class_nameThe name of the class we want to create (
See also
getAvailableClasses()).
Returns
An unmanaged (i.e. not a shared_ptr) Base* to newly created plugin object.

◆ isClassAvailable()

template<class Base >
bool class_loader::ClassLoader::isClassAvailable ( const std::string class_name) const
inline

Indicates if a plugin class is available.

Parameters
Base- polymorphic type indicating base class
class_name- the name of the plugin class
Returns
true if yes it is available, false otherwise

◆ getLibraryPath()

const std::string& class_loader::ClassLoader::getLibraryPath ( ) const

Gets the full-qualified path and name of the library associated with this class loader.

Returns
the full-qualified path and name of the library

◆ isLibraryLoaded()

bool class_loader::ClassLoader::isLibraryLoaded ( ) const

Indicates if a library is loaded within the scope of this ClassLoader.

Note that the library may already be loaded internally through another ClassLoader, but until loadLibrary() method is called, the ClassLoader cannot create objects from said library. If we want to see if the library has been opened by somebody else,

See also
isLibraryLoadedByAnyClassloader()
Parameters
library_pathThe path to the library to load
Returns
true if library is loaded within this ClassLoader object's scope, otherwise false

◆ isLibraryLoadedByAnyClassloader()

bool class_loader::ClassLoader::isLibraryLoadedByAnyClassloader ( ) const

Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader.

Returns
true if library is loaded within the scope of the plugin system, otherwise false

◆ isOnDemandLoadUnloadEnabled()

bool class_loader::ClassLoader::isOnDemandLoadUnloadEnabled ( ) const

Indicates if the library is to be loaded/unloaded on demand...meaning that only to load a lib when the first plugin is created and automatically shut it down when last active plugin is destroyed.

Returns
true if ondemand load and unload is active, otherwise false

◆ loadLibrary()

void class_loader::ClassLoader::loadLibrary ( )

Attempts to load a library on behalf of the ClassLoader. If the library is already opened, this method has no effect. If the library has been already opened by some other entity (i.e. another ClassLoader or global interface), this object is given permissions to access any plugin classes loaded by that other entity.

◆ unloadLibrary()

int class_loader::ClassLoader::unloadLibrary ( )

Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened, this method has no effect. If the library is opened by other another ClassLoader, the library will NOT be unloaded internally – however this ClassLoader will no longer be able to instantiate class_loader bound to that library. If there are plugin objects that exist in memory created by this classloader, a warning message will appear and the library will not be unloaded. If loadLibrary() was called multiple times (e.g. in the case of multiple threads or purposefully in a single thread), the user is responsible for calling unloadLibrary() the same number of times. The library will not be unloaded within the context of this classloader until the number of unload calls matches the number of loads.

Returns
The number of times more unloadLibrary() has to be called for it to be unbound from this ClassLoader

The documentation for this class was generated from the following file: