TfSingleton< T > Class Template Reference

Manage a single instance of an object (see. More...

Static Public Member Functions

static T & GetInstance ()
 Return a reference to an object of type T, creating it if necessary. More...
 
static bool CurrentlyExists ()
 Return whether or not the single object of type T is currently in existence. More...
 
static void SetInstanceConstructed (T &instance)
 Indicate that the sole instance object has already been created. More...
 
static void DeleteInstance ()
 Destroy the sole instance object of type T, if it exists. More...
 

Detailed Description

template<class T>
class TfSingleton< T >

Manage a single instance of an object (see.

Typical Use for a canonical example).

Definition at line 122 of file singleton.h.

Member Function Documentation

◆ CurrentlyExists()

static bool CurrentlyExists ( )
inlinestatic

Return whether or not the single object of type T is currently in existence.

This call tests whether or not the singleton currently exists.

Definition at line 150 of file singleton.h.

◆ DeleteInstance()

static void DeleteInstance ( )
inlinestatic

Destroy the sole instance object of type T, if it exists.

A singleton can be destroyed by a call to DeleteInstance. This call is threadsafe in the sense that competing simultaneous calls will not result in double deletion; however, it is up to the user to ensure that the instance is not being used in one thread during an attempt to delete the instance from another thread. After being destroyed, a call to GetInstance() will create a new instance.

Definition at line 187 of file singleton.h.

◆ GetInstance()

static T& GetInstance ( )
inlinestatic

Return a reference to an object of type T, creating it if necessary.

When GetInstance() is called for the first time, it creates an object of type T, and returns a reference to it. The type in question must have a default constructor (i.e. a constructor taking no arguments).

Subsequent calls to GetInstance() return a reference to the same object. This call is threadsafe; simultaneous attempts to create an object result in only one object being created; locking beyond this (for example, letting only one thread at a time call a member function) are the responsibility of the class author.

Definition at line 137 of file singleton.h.

◆ SetInstanceConstructed()

static void SetInstanceConstructed ( T &  instance)
inlinestatic

Indicate that the sole instance object has already been created.

This function is public, but can only be called usefully from within the class T itself. This function is used to allow the constructor of T to indicate that the sole instance of T has been created, and that future calls to GetInstance() can immediately return instance.

The need for this function occurs when the constructor of T generates a call chain that leads to calling TfSingleton<T>::GetInstance(). Until the constructor for T has finished, however, TfSingleton<T>::GetInstance() is unable to return a value. Calling SetInstanceConstructed() allows future calls to TfSingleton<T>::GetInstance() to return before T's constructor has finished.

Be sure that T has been constructed (enough) before calling this function. Calling this function anyplace but within the call chain of T's constructor will generate a fatal coding error.

Definition at line 172 of file singleton.h.


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