CARMA C++
|
Thread-safe, templatized Singleton class with policy based creation. More...
#include <carma/util/Singleton.h>
Static Public Member Functions | |
static TYPE & | instance () |
Retrieve a reference to underlying instance. More... | |
Protected Types | |
enum | INSTANCE_STATE { NO_INSTANCE, INSTANCE_CREATED, INSTANCE_DESTROYED, NUM_INSTANCE_STATES } |
typedef enum carma::util::Singleton::INSTANCE_STATE | InstanceStateType |
Static Protected Member Functions | |
static void | destroyInstance () |
Should be called when instance is destroyed. More... | |
Static Protected Attributes | |
static TYPE * | instancePtr_ |
static InstanceStateType | state_ |
Friends | |
class | InstantiationPolicy< TYPE > |
Thread-safe, templatized Singleton class with policy based creation.
Singleton attempts to mimic the Loki and ACE libraries policy based approach towards Singleton instance creation. It was designed around two basic use cases. The first, allows a user to subclass from the templatized Singleton class and create a singleton in the classical sense:
This defines a true Singleton in that only one instance of Lonely will ever exist. Please note the friend access to the Creation Policy - it is required.
So the steps to create a Singleton become:
The second major use case centers around using Singleton as a convenient way to access a single instance of a class. This technique is not a Singleton in the classical sense, as multiple instances of TYPE may be created independently of Singleton. However, it does allow one to conveniently access a certain single instance of TYPE if need be (think carma::monitor subsystem access).
The above concluding typedef allows one to easily change the creation policy of the Singleton accessor. Changing it to:
will cause the PseudoLonely instance to be created in static memory instead of on the heap.
Regardless of which use case is used, the instance storage policy can be easily changed by either inheriting from or typdefing to an alternate creation policy. So far the following three exist:
Unfortunately, Singleton falls short in several areas. It is based only on creation policies and does not allow one to change the lifetime of the instance nor the threading model. One of the main drawbacks of this is that Singletons which depend on Singletons cannot guarantee destruction order. Nonetheless, Singleton, will throw exceptions if any invariants are violated.
Please visit the documentation for Singleton.h for more information and references.
Definition at line 126 of file Singleton.h.
|
staticprotected |
Should be called when instance is destroyed.
Responsible for setting state to INSTANCE_DESTROYED and setting instancePtr to NULL.
Definition at line 269 of file Singleton.h.
|
static |
Retrieve a reference to underlying instance.
::std::logic_error | on invalid Singleton state. |
Definition at line 238 of file Singleton.h.