Gamedev Framework (gf)  0.3.0
A C++11 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

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.

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

Deleted copy constructor.

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

Deleted move constructor.

Member Function Documentation

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

Check if the singleton has been initialized.

Returns
True if the singleton is valid
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
template<typename T>
Singleton& gf::Singleton< T >::operator= ( const Singleton< T > &  )
delete

Deleted copy assignment.

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

Deleted move assignment.

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

Reset the singleton.

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