All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfSingleton< T > Class Template Reference

#include <singleton.h>

Static Public Member Functions

static TGetInstance ()
static bool CurrentlyExists ()
static void SetInstanceConstructed (T &instance)
static void DeleteInstance ()

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

template<class T>
static bool TfSingleton< T >::CurrentlyExists ( )

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.

template<class T>
static void TfSingleton< T >::DeleteInstance ( )

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.

template<class T>
static T& TfSingleton< T >::GetInstance ( )

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.

template<class T>
static void TfSingleton< T >::SetInstanceConstructed ( T instance)

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: