![]() |
Vault
4.1
|
VSingleton is a template class that provides a "singleton holder" that implements the "singleton" pattern behavior for a class T you write. More...
#include <vsingleton.h>
Public Types | |
enum | HolderDeletionPolicy { kDeleteHolderAtShutdown, kDontDeleteHolderAtShutdown } |
Specifies whether the VSingleton can be deleted. More... | |
enum | ThreadSafetyPolicy { kThreadSafeAccess, kSimpleAccess } |
Specifies whether a mutex is used to access the instance. More... | |
enum | ShutdownPolicy { kRegisterForShutdown, kDontRegisterForShutdown } |
Specifies whether the instance is shut down via the shutdown registry. More... | |
enum | ResurrectionPolicy { kDontAllowResurrection, kAllowResurrection } |
Specifies whether the instance can be re-created after being previously destroyed. More... | |
Public Member Functions | |
VSingleton (HolderDeletionPolicy holderDeletionPolicy, ThreadSafetyPolicy threadSafetyPolicy, ShutdownPolicy shutdownPolicy, ResurrectionPolicy resurrectionPolicy=kDontAllowResurrection) | |
Constructs the singleton holder. | |
T * | instance () |
Returns a pointer to the instance of T. | |
void | deleteInstance () |
Deletes the instance of T if it exists. | |
Protected Member Functions | |
virtual void | _shutdown () |
Implementation of IVShutdownHandler interface. |
VSingleton is a template class that provides a "singleton holder" that implements the "singleton" pattern behavior for a class T you write.
As of this version, the only key requirement of class T is that it has a default constructor, which will be used when VSingleton instantiates it. The behavior VSingleton gives you is:
You should define T's constructor and destructor private, and the template class a friend, so that only the template class can create and destroy the instance of T. Otherwise, the singleton behavior can be circumvented.
VSingleton can work with the VShutdownRegistry to make sure that the instance of T is cleaned up at shutdown, so you don't leak global resources (even in a simple case, a leak-detecting debugger will infer that there is a leak if the instance of T exists at program exit).
When you construct the VSingleton<T>, you pass it four "policy" flags:
Definition at line 86 of file vsingleton.h.
enum VSingleton::HolderDeletionPolicy |
Specifies whether the VSingleton can be deleted.
Definition at line 90 of file vsingleton.h.
enum VSingleton::ThreadSafetyPolicy |
Specifies whether a mutex is used to access the instance.
Definition at line 92 of file vsingleton.h.
enum VSingleton::ShutdownPolicy |
Specifies whether the instance is shut down via the shutdown registry.
Definition at line 94 of file vsingleton.h.
enum VSingleton::ResurrectionPolicy |
Specifies whether the instance can be re-created after being previously destroyed.
Definition at line 96 of file vsingleton.h.
VSingleton< T >::VSingleton | ( | HolderDeletionPolicy | holderDeletionPolicy, |
ThreadSafetyPolicy | threadSafetyPolicy, | ||
ShutdownPolicy | shutdownPolicy, | ||
ResurrectionPolicy | resurrectionPolicy = kDontAllowResurrection |
||
) | [inline] |
Constructs the singleton holder.
holderDeletionPolicy | specifies whether the holder can be deleted |
threadSafetyPolicy | specifies whether a mutex is used to access the instance |
shutdownPolicy | specifies whether the instance is shut down via the shutdown registry |
resurrectionPolicy | specifies whether the instance can be re-created after being previously destroyed |
Definition at line 105 of file vsingleton.h.
T* VSingleton< T >::instance | ( | ) | [inline] |
Returns a pointer to the instance of T.
If the instance does not yet exist, it is created. If the resurrection policy prohibits resurrection and the instance was previously deleted, a VException is thrown.
Definition at line 124 of file vsingleton.h.
References NULL.
virtual void VSingleton< T >::_shutdown | ( | ) | [inline, protected, virtual] |
Implementation of IVShutdownHandler interface.
To shut down the singleton means to delete the instance.
Implements IVShutdownHandler.
Definition at line 158 of file vsingleton.h.
References VSingleton< T >::deleteInstance().