libmoost
moost::pdl::dynamic_library Class Reference

#include <dynamic_library.h>

List of all members.

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_classcreate_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

Detailed Description

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.


Constructor & Destructor Documentation

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.

Parameters:
library_nameName of the shared library, potentially including a path.
resolve_symbolsWhether or not symbols shall be resolved immediately.

Definition at line 49 of file dynamic_library.cpp.

Here is the call graph for this function:


Member Function Documentation

template<typename T >
boost::shared_ptr<T> moost::pdl::dynamic_library::associate ( T *  instance) [inline]

Associate an instance of a class with the shared library

Template Parameters:
TClass type.
Parameters:
instanceThe instance to be associated.
Returns:
A shared pointer to the associated instance.

Definition at line 173 of file dynamic_library.h.

Here is the call graph for this function:

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.

Here is the caller graph for this function:

template<typename T >
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.

Template Parameters:
TInterface type implemented by the class.
Parameters:
class_nameName of the class that should be instantiated.
Returns:
A shared pointer to the newly created instance.

Definition at line 148 of file dynamic_library.h.

Here is the call graph for this function:

Here is the caller graph for this function:

dynamic_class * moost::pdl::dynamic_library::create_instance ( const std::string &  class_name) [private]

Definition at line 64 of file dynamic_library.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 96 of file dynamic_library.cpp.

Here is the caller graph for this function:

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.

Returns:
Boolean indicating if it's open.

Definition at line 131 of file dynamic_library.h.

Here is the caller graph for this function:

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.

Parameters:
library_nameName of the shared library, potentially including a path.
resolve_symbolsWhether or not symbols shall be resolved immediately.

Definition at line 54 of file dynamic_library.cpp.

Here is the caller graph for this function:


Member Data Documentation

Definition at line 184 of file dynamic_library.h.


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