libmoost
|
#include <dynamic_library.h>
Public Member Functions | |
dynamic_library () | |
dynamic_library (const std::string &library_name, bool resolve_symbols=true) | |
void | open (const std::string &library_name, bool resolve_symbols=true) |
void | close () |
bool | is_open () const |
template<typename T > | |
boost::shared_ptr< T > | create (const std::string &class_name) |
template<typename T > | |
boost::shared_ptr< T > | associate (T *instance) |
Private Member Functions | |
dynamic_class * | create_instance (const std::string &class_name) |
Static Private Member Functions | |
static void | dynamic_class_deleter (dynamic_class *p) |
Private Attributes | |
boost::shared_ptr < dynamic_library_if > | m_impl |
Portable shared object loader and class factory
This class manages shared library loading and unloading as well as instantiation of classes provided by the shared libraries.
It is only useful if the classes in these shared libraries have been properly exported and inherit from moost::pdl::dynamic_class.
It is crucial to understand how the lifetime of a shared library is managed. A shared library is not unloaded while it is being referenced from either a dynamic_library instance or any instance of any class exported by that shared library. It will be immediately unloaded once the last reference is gone. As a consequence, you have to ensure that no instances (other than global instances) implemented by the shared library have a lifetime that exceeds the lifetime of the classes instantiated through dynamic_library.
Instantiating a class within a shared library can be extremely easy:
boost::shared_ptr<interface> i; i = moost::pdl::dynamic_library(library).create<interface>(class_name);
This will load the library library
and create an instance of class class_name
implementing interface
. Once i
goes out of scope, the instance will automatically be destroyed and the shared library will be unloaded.
Definition at line 72 of file dynamic_library.h.
Construct a dynamic_library object
Definition at line 45 of file dynamic_library.cpp.
moost::pdl::dynamic_library::dynamic_library | ( | const std::string & | library_name, |
bool | resolve_symbols = true |
||
) |
Construct a dynamic_library object and open library
This will immediately attempt to open the library. It will throw and exception of the library cannot be opened.
library_name | Name of the shared library, potentially including a path. |
resolve_symbols | Whether or not symbols shall be resolved immediately. |
Definition at line 49 of file dynamic_library.cpp.
boost::shared_ptr<T> moost::pdl::dynamic_library::associate | ( | T * | instance | ) | [inline] |
Associate an instance of a class with the shared library
T | Class type. |
instance | The instance to be associated. |
Definition at line 173 of file dynamic_library.h.
void moost::pdl::dynamic_library::close | ( | ) |
Close a shared library
This will merely remove the internal reference to the shared library. It will not necessarily unload the shared object immediately.
Definition at line 59 of file dynamic_library.cpp.
boost::shared_ptr<T> moost::pdl::dynamic_library::create | ( | const std::string & | class_name | ) | [inline] |
Create a new instance of a class exported by the shared library
This will throw an exception if a new instance cannot be created.
T | Interface type implemented by the class. |
class_name | Name of the class that should be instantiated. |
Definition at line 148 of file dynamic_library.h.
dynamic_class * moost::pdl::dynamic_library::create_instance | ( | const std::string & | class_name | ) | [private] |
Definition at line 64 of file dynamic_library.cpp.
void moost::pdl::dynamic_library::dynamic_class_deleter | ( | dynamic_class * | p | ) | [static, private] |
bool moost::pdl::dynamic_library::is_open | ( | ) | const [inline] |
Check if a shared library has been opened
This will check if the dynamic_library instance currenly references a valid shared library that can be used to create instances.
Definition at line 131 of file dynamic_library.h.
void moost::pdl::dynamic_library::open | ( | const std::string & | library_name, |
bool | resolve_symbols = true |
||
) |
Open a shared library
This will attempt to open a shared library. It will throw and exception of the library cannot be opened. If another library was opened before, that library will implicitly be closed. Note that this doesn't imply that the shared object will immediately be unloaded (it will only be unloaded once the last reference to it is gone). In other words, it is perfectly safe to open a new shared library while there are still instances referencing the old one.
library_name | Name of the shared library, potentially including a path. |
resolve_symbols | Whether or not symbols shall be resolved immediately. |
Definition at line 54 of file dynamic_library.cpp.
boost::shared_ptr<dynamic_library_if> moost::pdl::dynamic_library::m_impl [private] |
Definition at line 184 of file dynamic_library.h.