Zth (libzth)
Loading...
Searching...
No Matches
zth::Singleton< T > Class Template Reference

Singleton pattern. More...

#include <util.h>

Public Types

typedef T singleton_type
 Alias of the T template parameter.
 

Static Public Member Functions

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

Protected Member Functions

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

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
};
Singleton pattern.
Definition util.h:1088

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 1088 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 1091 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 1098 of file util.h.

◆ ~Singleton()

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

Destructor.

After destruction, instance() will return nullptr.

Definition at line 1111 of file util.h.

Member Function Documentation

◆ instance()

template<typename T >
static constexpr 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 1123 of file util.h.


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