Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | 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

CLASS_LOADER_PUBLIC ClassLoader (const std::string &library_path, bool ondemand_load_unload=false)
 Constructor for ClassLoader. More...
 
template<class Base >
boost::shared_ptr< BasecreateInstance (const std::string &derived_class_name)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
std::shared_ptr< BasecreateSharedInstance (const std::string &derived_class_name)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
UniquePtr< BasecreateUniqueInstance (const std::string &derived_class_name)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
BasecreateUnmanagedInstance (const std::string &derived_class_name)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
std::vector< std::string > getAvailableClasses ()
 Indicates which classes (i.e. class_loader) that can be loaded by this object. More...
 
CLASS_LOADER_PUBLIC std::string getLibraryPath ()
 Gets the full-qualified path and name of the library associated with this class loader. More...
 
template<class Base >
bool isClassAvailable (const std::string &class_name)
 Indicates if a plugin class is available. More...
 
CLASS_LOADER_PUBLIC bool isLibraryLoaded ()
 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,. More...
 
CLASS_LOADER_PUBLIC bool isLibraryLoadedByAnyClassloader ()
 Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader. More...
 
CLASS_LOADER_PUBLIC bool isOnDemandLoadUnloadEnabled ()
 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...
 
CLASS_LOADER_PUBLIC 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. This is. More...
 
CLASS_LOADER_PUBLIC 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...
 
virtual CLASS_LOADER_PUBLIC ~ClassLoader ()
 Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically. More...
 

Private Member Functions

template<class Base >
BasecreateRawInstance (const std::string &derived_class_name, bool managed)
 Generates an instance of loadable classes (i.e. class_loader). More...
 
template<class Base >
void onPluginDeletion (Base *obj)
 Callback method when a plugin created by this class loader is destroyed. More...
 
CLASS_LOADER_PUBLIC int unloadLibraryInternal (bool lock_plugin_ref_count)
 As the library may be unloaded in "on-demand load/unload" mode, unload maybe called from createInstance(). The problem is that createInstance() locks the plugin_ref_count as does unloadLibrary(). This method is the implementation of unloadLibrary but with a parameter to decide if plugin_ref_mutex_ should be locked. More...
 

Static Private Member Functions

static CLASS_LOADER_PUBLIC bool hasUnmanagedInstanceBeenCreated ()
 Getter for if an unmanaged (i.e. unsafe) instance has been created flag. More...
 

Private Attributes

std::string library_path_
 
int load_ref_count_
 
boost::recursive_mutex load_ref_count_mutex_
 
bool ondemand_load_unload_
 
int plugin_ref_count_
 
boost::recursive_mutex plugin_ref_count_mutex_
 

Static Private Attributes

static CLASS_LOADER_PUBLIC bool has_unmananged_instance_been_created_ = false
 

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.

Definition at line 78 of file class_loader.hpp.

Member Typedef Documentation

◆ DeleterType

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

Definition at line 82 of file class_loader.hpp.

◆ UniquePtr

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

Definition at line 85 of file class_loader.hpp.

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

Definition at line 65 of file class_loader.cpp.

◆ ~ClassLoader()

class_loader::ClassLoader::~ClassLoader ( )
virtual

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

Definition at line 80 of file class_loader.cpp.

Member Function Documentation

◆ createInstance()

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

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

Same as createSharedInstance() except it returns a boost::shared_ptr.

Definition at line 140 of file class_loader.hpp.

◆ createRawInstance()

template<class Base >
Base* class_loader::ClassLoader::createRawInstance ( const std::string &  derived_class_name,
bool  managed 
)
inlineprivate

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())
Parameters
managedIf true, the returned pointer is assumed to be wrapped in a smart pointer by the caller.
Returns
A Base* to newly created plugin object

Definition at line 278 of file class_loader.hpp.

◆ createSharedInstance()

template<class Base >
std::shared_ptr<Base> class_loader::ClassLoader::createSharedInstance ( 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

Definition at line 127 of file class_loader.hpp.

◆ 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

Definition at line 160 of file class_loader.hpp.

◆ 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.

Definition at line 181 of file class_loader.hpp.

◆ getAvailableClasses()

template<class Base >
std::vector<std::string> class_loader::ClassLoader::getAvailableClasses ( )
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>

Definition at line 106 of file class_loader.hpp.

◆ getLibraryPath()

CLASS_LOADER_PUBLIC std::string class_loader::ClassLoader::getLibraryPath ( )
inline

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

Definition at line 115 of file class_loader.hpp.

◆ hasUnmanagedInstanceBeenCreated()

bool class_loader::ClassLoader::hasUnmanagedInstanceBeenCreated ( )
staticprivate

Getter for if an unmanaged (i.e. unsafe) instance has been created flag.

Definition at line 41 of file class_loader.cpp.

◆ isClassAvailable()

template<class Base >
bool class_loader::ClassLoader::isClassAvailable ( const std::string &  class_name)
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

Definition at line 193 of file class_loader.hpp.

◆ isLibraryLoaded()

bool class_loader::ClassLoader::isLibraryLoaded ( )

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

Definition at line 88 of file class_loader.cpp.

◆ isLibraryLoadedByAnyClassloader()

bool class_loader::ClassLoader::isLibraryLoadedByAnyClassloader ( )

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

Definition at line 93 of file class_loader.cpp.

◆ isOnDemandLoadUnloadEnabled()

CLASS_LOADER_PUBLIC bool class_loader::ClassLoader::isOnDemandLoadUnloadEnabled ( )
inline

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.

Definition at line 219 of file class_loader.hpp.

◆ 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. This is.

Parameters
library_pathThe path to the library to load

Definition at line 98 of file class_loader.cpp.

◆ onPluginDeletion()

template<class Base >
void class_loader::ClassLoader::onPluginDeletion ( Base obj)
inlineprivate

Callback method when a plugin created by this class loader is destroyed.

Parameters
obj- A pointer to the deleted object

Definition at line 241 of file class_loader.hpp.

◆ 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

Definition at line 105 of file class_loader.cpp.

◆ unloadLibraryInternal()

int class_loader::ClassLoader::unloadLibraryInternal ( bool  lock_plugin_ref_count)
private

As the library may be unloaded in "on-demand load/unload" mode, unload maybe called from createInstance(). The problem is that createInstance() locks the plugin_ref_count as does unloadLibrary(). This method is the implementation of unloadLibrary but with a parameter to decide if plugin_ref_mutex_ should be locked.

Parameters
lock_plugin_ref_count- Set to true if plugin_ref_count_mutex_ should be locked, else false
Returns
The number of times unloadLibraryInternal has to be called again for it to be unbound from this ClassLoader

Definition at line 110 of file class_loader.cpp.

Member Data Documentation

◆ has_unmananged_instance_been_created_

bool class_loader::ClassLoader::has_unmananged_instance_been_created_ = false
staticprivate

Definition at line 335 of file class_loader.hpp.

◆ library_path_

std::string class_loader::ClassLoader::library_path_
private

Definition at line 328 of file class_loader.hpp.

◆ load_ref_count_

int class_loader::ClassLoader::load_ref_count_
private

Definition at line 329 of file class_loader.hpp.

◆ load_ref_count_mutex_

boost::recursive_mutex class_loader::ClassLoader::load_ref_count_mutex_
private

Definition at line 330 of file class_loader.hpp.

◆ ondemand_load_unload_

bool class_loader::ClassLoader::ondemand_load_unload_
private

Definition at line 327 of file class_loader.hpp.

◆ plugin_ref_count_

int class_loader::ClassLoader::plugin_ref_count_
private

Definition at line 331 of file class_loader.hpp.

◆ plugin_ref_count_mutex_

boost::recursive_mutex class_loader::ClassLoader::plugin_ref_count_mutex_
private

Definition at line 332 of file class_loader.hpp.


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


class_loader
Author(s): Mirza Shah, Steven! Ragnarök
autogenerated on Mon Feb 28 2022 22:02:03