![]() |
Blaze 3.9
|
Classes | |
class | blaze::Dependency< T > |
Lifetime dependency on a singleton object. More... | |
class | blaze::Singleton< T, D1, D2, D3, D4, D5, D6, D7, D8 > |
Base class for all lifetime managed singletons. More... | |
Macros | |
#define | BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T) static_assert( ( !blaze::HasCyclicDependency<T,blaze::NullType>::value ), "Cyclic dependency detected" ) |
Constraint on the data type. More... | |
#define | BLAZE_BEFRIEND_SINGLETON |
Friendship declaration for the Singleton class template. More... | |
The singleton design pattern is one of the most popular and most important design patterns available. It can be used to ensures that a specific class has only exactly one instance, and provides a global access point to this instance [1,2]. Additionally, via the singleton pattern it is posssible to manage the lifetime of objects, and especially the lifetime dependencies between several objects.
In the Blaze library the singleton pattern is realized by the Singleton class template. Classes that are supposed to be implemented in terms of the singleton pattern only have to derive from this class in order to gain all necessary characteristics of a singleton:
The only precondition on classes deriving from the Singleton class is the availability of a default constructor. In case it is not possible to instantiate the class via a default constructor, i.e., in case the class has only constructors that require at least a single argument, the Blaze Singleton implementation cannot be used!
In order to make a specific class a singleton, two modifications have to be applied to this class:
The class needs to befriend the Singleton via the blaze::BLAZE_BEFRIEND_SINGLETON macro. This macro provides a convenient way to express this friendship relation and works both in case the class derives publicly or non-publicly from the Singleton class. This friendship is necessary since in order to guarantee the uniqueness of the singleton instance the constructor of the deriving class must be declared in a non-public section of the class definition. However, in order for the Singleton class to provide the instance() function, the constructor must be accessible. This is achieved by the blaze::BLAZE_BEFRIEND_SINGLETON macro. The following example demonstrates this by means of the MySingleton class:
[1] E. Gamma, R. Helm, R.E. Johnson, J.M. Vlissides: Design Patterns, Addison-Wesley Professional Computing Series, 2008, ISBN: 978-0-201-63361-0
[2] S. Meyers: Effective C++, Third Edition, Addison-Wesley Professional Computing Series, 2008, ISBN: 978-0-321-33487-9
[3] J. Ringle: Singleton Creation the Thread-safe Way, Dr. Dobb's (www.drdobbs.com), 1999
[4] A. Alexandrescu: Modern C++ Design, Generic Programming and Design Patterns Applied, Addison-Wesley, 2001, ISBN: 978-0201704310
[5] E. Gabrilovich: Controlling the Destruction Order of Singleton Objects, Dr. Dobbs (www.drdobbs.com), 1999
#define BLAZE_BEFRIEND_SINGLETON |
Friendship declaration for the Singleton class template.
This macro has to be used in order to declare the Singleton functionality as friend of the class deriving from Singleton.
#define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY | ( | T | ) | static_assert( ( !blaze::HasCyclicDependency<T,blaze::NullType>::value ), "Cyclic dependency detected" ) |
Constraint on the data type.
In case the given data type T is not an integral data type, a compilation error is created.