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.h>
Public Member Functions | |
ClassLoader (const std::string &library_path, bool ondemand_load_unload=false) | |
Constructor for ClassLoader. | |
template<class Base > | |
boost::shared_ptr< Base > | createInstance (const std::string &derived_class_name) |
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). | |
template<class Base > | |
Base * | createUnmanagedInstance (const std::string &derived_class_name) |
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). | |
template<class Base > | |
std::vector< std::string > | getAvailableClasses () |
Indicates which classes (i.e. class_loader) that can be loaded by this object. | |
std::string | getLibraryPath () |
Gets the full-qualified path and name of the library associated with this class loader. | |
template<class Base > | |
bool | isClassAvailable (const std::string &class_name) |
Indicates if a plugin class is available. | |
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,. | |
bool | isLibraryLoadedByAnyClassloader () |
Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader. | |
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. | |
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. | |
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. | |
virtual | ~ClassLoader () |
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically. | |
Private Member Functions | |
template<class Base > | |
void | onPluginDeletion (Base *obj) |
Callback method when a plugin created by this class loader is destroyed. | |
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. | |
Static Private Member Functions | |
static bool | hasUnmanagedInstanceBeenCreated () |
Getter for if an unmanaged (i.e. unsafe) instance has been created flag. | |
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 bool | has_unmananged_instance_been_created_ = false |
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 56 of file class_loader.h.
class_loader::ClassLoader::ClassLoader | ( | const std::string & | library_path, |
bool | ondemand_load_unload = false |
||
) |
Constructor for ClassLoader.
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 47 of file class_loader.cpp.
class_loader::ClassLoader::~ClassLoader | ( | ) | [virtual] |
Destructor for ClassLoader. All libraries opened by this ClassLoader are unloaded automatically.
Definition at line 58 of file class_loader.cpp.
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). 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).
derived_class_name | The name of the class we want to create ( |
Definition at line 92 of file class_loader.h.
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).
derived_class_name | The name of the class we want to create ( |
Definition at line 116 of file class_loader.h.
std::vector<std::string> class_loader::ClassLoader::getAvailableClasses | ( | ) | [inline] |
Indicates which classes (i.e. class_loader) that can be loaded by this object.
Definition at line 76 of file class_loader.h.
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 84 of file class_loader.h.
bool class_loader::ClassLoader::hasUnmanagedInstanceBeenCreated | ( | ) | [static, private] |
Getter for if an unmanaged (i.e. unsafe) instance has been created flag.
Definition at line 37 of file class_loader.cpp.
bool class_loader::ClassLoader::isClassAvailable | ( | const std::string & | class_name | ) | [inline] |
Indicates if a plugin class is available.
Base | - polymorphic type indicating base class |
class_name | - the name of the plugin class |
Definition at line 135 of file class_loader.h.
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,.
library_path | The path to the library to load |
Definition at line 64 of file class_loader.cpp.
Indicates if a library is loaded by some entity in the plugin system (another ClassLoader), but not necessarily loaded by this ClassLoader.
Definition at line 69 of file class_loader.cpp.
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 157 of file class_loader.h.
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.
library_path | The path to the library to load |
Definition at line 74 of file class_loader.cpp.
void class_loader::ClassLoader::onPluginDeletion | ( | Base * | obj | ) | [inline, private] |
Callback method when a plugin created by this class loader is destroyed.
obj | - A pointer to the deleted object |
Definition at line 177 of file class_loader.h.
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.
Definition at line 81 of file class_loader.cpp.
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.
lock_plugin_ref_count | - Set to true if plugin_ref_count_mutex_ should be locked, else false |
Definition at line 86 of file class_loader.cpp.
bool class_loader::ClassLoader::has_unmananged_instance_been_created_ = false [static, private] |
Definition at line 216 of file class_loader.h.
std::string class_loader::ClassLoader::library_path_ [private] |
Definition at line 211 of file class_loader.h.
int class_loader::ClassLoader::load_ref_count_ [private] |
Definition at line 212 of file class_loader.h.
boost::recursive_mutex class_loader::ClassLoader::load_ref_count_mutex_ [private] |
Definition at line 213 of file class_loader.h.
bool class_loader::ClassLoader::ondemand_load_unload_ [private] |
Definition at line 210 of file class_loader.h.
int class_loader::ClassLoader::plugin_ref_count_ [private] |
Definition at line 214 of file class_loader.h.
boost::recursive_mutex class_loader::ClassLoader::plugin_ref_count_mutex_ [private] |
Definition at line 215 of file class_loader.h.