Zth (libzth)
zth::Singleton< T > Class Template Reference

Singleton pattern. More...

#include <util.h>

Public Types

typedef T singleton_type
 Alias of the T template parameter. More...
 

Static Public Member Functions

constexpr static safe_ptr< singleton_type >::type instance () noexcept
 Return the only instance of T. More...
 

Protected Member Functions

constexpr Singleton () noexcept
 Constructor. More...
 
 ~Singleton ()
 Destructor. More...
 

Detailed Description

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

Singleton pattern.

When a class inherits this class, the single instance of that class is available via the static instance() function.

A class should use Singleton as follows:

class Subclass : public Singleton<Subclass>
{
public:
Subclass() {} // no need to invoke Singleton()
// ...normal class implementation
};
constexpr Singleton() noexcept
Constructor.
Definition: util.h:906

Make sure that the template parameter T is the same as the subclass.

Template Parameters
Tthe type of the subclass that inherits this Singleton.

Definition at line 896 of file util.h.

Member Typedef Documentation

◆ singleton_type

template<typename T >
typedef T zth::Singleton< T >::singleton_type

Alias of the T template parameter.

Definition at line 899 of file util.h.

Constructor & Destructor Documentation

◆ Singleton()

template<typename T >
constexpr zth::Singleton< T >::Singleton ( )
inlineconstexprprotectednoexcept

Constructor.

(Only) the first instance of the T is recorded in m_instance.

Definition at line 906 of file util.h.

◆ ~Singleton()

template<typename T >
zth::Singleton< T >::~Singleton ( )
inlineprotected

Destructor.

After destruction, instance() will return nullptr.

Definition at line 919 of file util.h.

Member Function Documentation

◆ instance()

template<typename T >
constexpr static safe_ptr<singleton_type>::type zth::Singleton< T >::instance ( )
inlinestaticconstexprnoexcept

Return the only instance of T.

Returns
the instance, or nullptr when not constructed yet/anymore.

Definition at line 931 of file util.h.


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