Loading...
Searching...
No Matches
TfSingleton< T > Class Template Reference

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

#include <singleton.h>

Public Member Functions

void DeleteInstance ()=delete
 

Static Public Member Functions

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

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 107 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 140 of file singleton.h.

◆ DeleteInstance()

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 142 of file instantiateSingleton.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 122 of file singleton.h.

◆ SetInstanceConstructed()

void SetInstanceConstructed ( T &  instance)
inlinestatic

Indicate that the sole instance object has already been created.

This function is public, but should only be called by T 's constructor. It makes the instance available to the calling thread for initialization purposes during the remainder of T 's construction. In contrast, concurrent threads that call GetInstance() will continue to wait until construction is fully complete before returning the instance.

The need for this function occurs when T's constructor generates a call chain that calls back to TfSingleton<T>::GetInstance(). Normally calls to TfSingleton<T>::GetInstance() during T's construction will wait for construction to complete. Calling SetInstanceConstructed() lets future calls to TfSingleton<T>::GetInstance() by the same thread that called SetInstanceConstructed() to access the instance before T's constructor has finished. This is useful when the singleton's constuctor calls TfRegistryManager::SubscribeTo(), for example. The invoked TF_REGISTRY_FUNCTION()s can successfully call GetInstance() to access the singleton.

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

Definition at line 61 of file instantiateSingleton.h.


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