Embedded Template Library 1.0
|
#include <singleton_base.h>
Static Public Member Functions | |
static T & | instance () |
static bool | is_valid () |
Returns whether an instance has been attached to singleton<T> or not. | |
Protected Member Functions | |
singleton_base (T &theInstance) | |
~singleton_base () | |
Removes the internal reference to the instance passed in the constructor. | |
Base class for singletons.
T | Any type that wants to expose the instance() interface. |
This class is designed to work as a generic base class for any class that wants to provide a singleton interface. It'll also work for classes that do not have a default constructor.
Usage example:
class Origin : singleton<Origin> { public: Origin(int x, int y) : singleton<Origin>(*this) {}
int getX() const; } theOrigin(0, 0);
int x = Origin::instance().getX();
Note:
It is important that a call to instance() will not create the instance of the class. It needs to be created by the user before calling instance(). This way, the user has better control over the instance lifetime instead of e.g. lazy initialization.
|
inlineexplicitprotected |
Constructs the instance of singleton. theInstance Reference to T, which will be returned when instance() is called.