AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::Singleton< T > Class Template Reference

Meyers' singleton implementation. More...

#include <singleton.hpp>

Inheritance diagram for anch::Singleton< T >:
Collaboration diagram for anch::Singleton< T >:

Public Member Functions

 Singleton (const Singleton &)=delete
 Singleton (Singleton &&)=delete
Singleton & operator= (const Singleton &)=delete
Singleton & operator= (Singleton &&)=delete

Static Public Member Functions

static T & getInstance ()

Protected Member Functions

 Singleton () noexcept=default
 ~Singleton ()=default

Detailed Description

template<typename T>
class anch::Singleton< T >

Meyers' singleton implementation.

The instance is created on first getInstance call. Its creation is thread safe according to the standard.
If T constructor throws an exception, the next getInstance call will try to create the instance again.

Usage:

class MySingleton: public anch::Singleton<MySingleton> {
friend anch::Singleton<MySingleton>;
private:
MySingleton();
~MySingleton();
};
  • T has to inherit from anch::Singleton<T> ;
  • T constructor and destructor should be private and anch::Singleton<T> declared as friend to prevent other instances ;
  • copy and move are prohibited by this class: there is no need to delete them in T ;
  • T destructor does not need to be virtual: the instance is never destroyed through a base class pointer.

Destruction order: singletons are destroyed at program exit, in the reverse order of the end of their construction. When a singleton uses another one in its destructor (or holds objects which depend on it), it has to call the other singleton's getInstance in its constructor: the other singleton will be fully constructed first, so it will be destroyed after.
Singleton has default symbols visibility: the instance is shared between AnCH libraries (built with hidden visibility) and the application.
A singleton must not be used from a static object destructor or a detached thread after its destruction.

Since
0.1
Author
Vincent Lachenal

Constructor & Destructor Documentation

◆ Singleton() [1/3]

template<typename T>
anch::Singleton< T >::Singleton ( )
protecteddefaultnoexcept

Singleton constructor

◆ Singleton() [2/3]

template<typename T>
anch::Singleton< T >::Singleton ( const Singleton< T > & )
delete

Prohibits Singleton copy constructor

Here is the call graph for this function:

◆ Singleton() [3/3]

template<typename T>
anch::Singleton< T >::Singleton ( Singleton< T > && )
delete

Prohibits Singleton move constructor

Here is the call graph for this function:

◆ ~Singleton()

template<typename T>
anch::Singleton< T >::~Singleton ( )
protecteddefault

Singleton destructor

Here is the call graph for this function:

Member Function Documentation

◆ getInstance()

template<typename T>
T & anch::Singleton< T >::getInstance ( )
static

Get unique instance of a class.

Returns
the unique instance
Here is the call graph for this function:

◆ operator=() [1/2]

template<typename T>
Singleton & anch::Singleton< T >::operator= ( const Singleton< T > & )
delete

Prohibits Singleton copy assignment

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename T>
Singleton & anch::Singleton< T >::operator= ( Singleton< T > && )
delete

Prohibits Singleton move assignment

Here is the call graph for this function:

The documentation for this class was generated from the following file: