Gamedev Framework (gf)  0.10.0
A C++14 framework for 2D games
Public Member Functions | List of all members
gf::Singleton< T > Class Template Reference

A singleton that wraps a pointer provided by a storage. More...

#include <gf/Singleton.h>

Public Member Functions

 Singleton ()
 Default constructor. More...
 
 Singleton (const Singleton &)=delete
 Deleted copy constructor. More...
 
 Singleton (Singleton &&)=delete
 Deleted move constructor. More...
 
Singletonoperator= (const Singleton &)=delete
 Deleted copy assignment. More...
 
Singletonoperator= (Singleton &&)=delete
 Deleted move assignment. More...
 
Toperator() ()
 Access the singleton. More...
 
void reset () noexcept
 Reset the singleton. More...
 
bool isValid () const noexcept
 Check if the singleton has been initialized. More...
 

Detailed Description

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

A singleton that wraps a pointer provided by a storage.

This class should be used with a global variable. It acts as an access to a singleton which storage is provided by a gf::SingletonStorage<T>.

int main() {
gf::SingletonStorage<Foo> fooStorage(gFoo);
gFoo().bar();
}
See also
gf::SingletonStorage

Constructor & Destructor Documentation

◆ Singleton() [1/3]

template<typename T>
gf::Singleton< T >::Singleton ( )
inline

Default constructor.

It initialize the underlying pointer to nullptr. The actual initialization is done when the storage is allocated.

◆ Singleton() [2/3]

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

Deleted copy constructor.

◆ Singleton() [3/3]

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

Deleted move constructor.

Member Function Documentation

◆ isValid()

template<typename T>
bool gf::Singleton< T >::isValid ( ) const
inlinenoexcept

Check if the singleton has been initialized.

Returns
True if the singleton is valid

◆ operator()()

template<typename T>
T& gf::Singleton< T >::operator() ( )
inline

Access the singleton.

The access of the singleton must be done after a storage has been provided. Otherwise, the behaviour is undefined (and may result in a crash).

Returns
a reference to the stored singleton

◆ operator=() [1/2]

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

Deleted copy assignment.

◆ operator=() [2/2]

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

Deleted move assignment.

◆ reset()

template<typename T>
void gf::Singleton< T >::reset ( )
inlinenoexcept

Reset the singleton.

After a call to this function, the singleton must be accessed anymore.