Classes | Macros

Classes

class  blaze::Dependency< T >
 Lifetime dependency on a singleton object.The Dependency template class represents a lifetime dependency on a singleton object based on the Blaze Singleton functionality. By use of the Dependency template, any class can by either public or non-public inheritance or composition define a single or multiple lifetime dependencies on one or several singletons, which guarantees that the singleton instance(s) will be destroyed after the dependent object. The following example demonstrates both the inheritance as well as the composition approach: More...
 
class  blaze::Singleton< T, D1, D2, D3, D4, D5, D6, D7, D8 >
 Base class for all lifetime managed singletons.The Singleton class represents the base class for all lifetime managed singletons of the Blaze library. Classes, which are supposed to be implemented in terms of the singleton pattern, only have to derive from this class in order to gain all basic characteristics of a singleton: More...
 

Macros

#define BLAZE_DETECT_CYCLIC_LIFETIME_DEPENDENCY(T)
 Constraint on the data type.In case the given data type T is not an integral data type, a compilation error is created. More...
 
#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. More...
 

Detailed Description

Motivation

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!

Usage of the Singleton

In order to make a specific class a singleton, two modifications have to be applied to this class:

  1. The class has to derive (publicly or non-publicly) from the Singleton class. In case the class derives publicly the instance() member function, which the class inherits from the Singleton class, is publicly accessible and provides a point of access to the singleton instance. In case the class derives non-publicly, the instance() function is not publicly accessible and therefore the class has to provide another point of access to the singleton instance.
    The first template parameter has to be the class itself. The following template parameters define lifetime dependencies of this class, i.e., specify on which singleton instances the class depends. It is possible to specify up to 8 lifetime dependencies. The example below demonstrates this for the MySingleton class, which is solely depending on the Logger class, which represents the core of the Blaze logging functionality.
  2. 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:
class MySingleton : private Singleton<MySingleton,Logger>
{
private:
MySingleton();
...
...
};

References

[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

Macro Definition Documentation

#define BLAZE_BEFRIEND_SINGLETON
Value:
template< typename, typename, typename, typename, typename, typename, typename, typename, typename > friend class blaze::Singleton; \
template< typename, typename, bool > friend struct blaze::HasCyclicDependency; \
template< typename > friend class blaze::Dependency;
Base class for all lifetime managed singletons.The Singleton class represents the base class for all ...
Definition: Singleton.h:606
Lifetime dependency on a singleton object.The Dependency template class represents a lifetime depende...
Definition: Dependency.h:84
Definition: Singleton.h:61

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)
Value:
typedef \
blaze::CYCLIC_LIFETIME_DEPENDENCY_TEST< \
blaze::CYCLIC_LIFETIME_DEPENDENCY_DETECTED< blaze::HasCyclicDependency<T,blaze::NullType>::value >::value > \
BLAZE_JOIN( DETECT_CYCLIC_LIFETIME_DEPENDENCY_TYPEDEF, __LINE__ )

Constraint on the data type.In case the given data type T is not an integral data type, a compilation error is created.