![]() |
A utility class to manage a shared instance of an object. More...
#include <SurgSim/Framework/SharedInstance.h>
Public Types | |
typedef std::function< std::shared_ptr< T >)> | InstanceCreator |
A type that can hold a function object or lambda that takes no arguments and returns std::shared_ptr<T>. More... | |
Public Member Functions | |
SharedInstance () | |
Create the SharedInstance object used to manage the shared instance. More... | |
SharedInstance (const InstanceCreator &instanceCreator) | |
Create the SharedInstance object used to manage the shared instance. More... | |
~SharedInstance () | |
Destroy the container and the data it contains. More... | |
std::shared_ptr< T > | get () |
Gets the shared object instance. More... | |
Private Member Functions | |
SharedInstance (const SharedInstance &) | |
Prevent copying. More... | |
SharedInstance & | operator= (const SharedInstance &) |
Prevent assignment. More... | |
std::shared_ptr< T > | createInstance () |
Creates an object instance. More... | |
Static Private Member Functions | |
static InstanceCreator | defaultInstanceCreator () |
Creates a function that can create an instance using std::make_shared<T>(). More... | |
Private Attributes | |
InstanceCreator | m_instanceCreator |
A creator function used to construct the shared instance. More... | |
std::weak_ptr< T > | m_weakInstance |
A weak reference to the shared instance, if any. More... | |
boost::mutex | m_mutex |
Mutex for synchronization of object creation. More... | |
A utility class to manage a shared instance of an object.
This class behaves in a way that is superficially similar to the Singleton pattern, in that it manages a single shared instance of an object. However, there are some important differences to note:
Code using this class will normally use the get() method to initialize its own shared_ptr referencing the shared instance. Such code should hold onto the shared_ptr for as long as it needs to use the shared instance. As soon as all of the shared pointers are released, the shared object instance will be destroyed.
A Simple Example
To share an instance between many objects in the same class, you can declare a static SharedInstance member:
The downside to this approach is that it requires the SharedInstance object itself to be created as a static member of a class. Since the order of construction (and destruction) of various static data members is not well defined by the C++ language standards, this can lead to objects getting used before they are initialized.
A Better Example
To avoid the initialization order problems, you can instead use the SharedInstance as a static variable inside a function or a (most likely also static) method, so it will not be initialized until that function/method is first called. (Note that this is only safe if your compiler follows the C++11 requirement that the initialization of static variables inside functions must be atomic.)
T | Type of the data held by the SharedInstance. |
typedef std::function<std::shared_ptr<T>)> SurgSim::Framework::SharedInstance< T >::InstanceCreator |
A type that can hold a function object or lambda that takes no arguments and returns std::shared_ptr<T>.
SurgSim::Framework::SharedInstance< T >::SharedInstance | ( | ) |
Create the SharedInstance object used to manage the shared instance.
Note that this does not immediately create the instance itself. If and when the shared instance is created, it will be initialized using the default constructor via std::make_shared.
|
explicit |
Create the SharedInstance object used to manage the shared instance.
Note that this does not immediately create the instance itself. If and when the shared instance is created, it will be initialized using the creator call.
SurgSim::Framework::SharedInstance< T >::~SharedInstance | ( | ) |
Destroy the container and the data it contains.
|
private |
Prevent copying.
|
private |
Creates an object instance.
|
staticprivate |
Creates a function that can create an instance using std::make_shared<T>().
The function must be default-constuctible. It was necessary to split this into a separate function because with VS2010, we can't just put a lambda in the initializer for m_instanceCreator.
std::shared_ptr< T > SurgSim::Framework::SharedInstance< T >::get | ( | ) |
Gets the shared object instance.
If the instance has not been created previously, it will be created during the call.
The calling code should generally copy the shared_ptr and hold onto it for as long as needed. As soon as all of the shared pointers are released, the shared object instance will be destroyed.
|
private |
Prevent assignment.
|
private |
A creator function used to construct the shared instance.
|
private |
Mutex for synchronization of object creation.
|
private |
A weak reference to the shared instance, if any.