Vault  4.1
Public Types | Public Member Functions | Protected Member Functions
VSingleton< T > Class Template Reference

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>

Inheritance diagram for VSingleton< T >:
IVShutdownHandler

List of all members.

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.

Detailed Description

template<class T>
class VSingleton< T >

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:

See also:
VShutdownRegistry
IVShutdownHandler

Definition at line 86 of file vsingleton.h.


Member Enumeration Documentation

template<class T >
enum VSingleton::HolderDeletionPolicy

Specifies whether the VSingleton can be deleted.

Definition at line 90 of file vsingleton.h.

template<class T >
enum VSingleton::ThreadSafetyPolicy

Specifies whether a mutex is used to access the instance.

Definition at line 92 of file vsingleton.h.

template<class T >
enum VSingleton::ShutdownPolicy

Specifies whether the instance is shut down via the shutdown registry.

Definition at line 94 of file vsingleton.h.

template<class T >
enum VSingleton::ResurrectionPolicy

Specifies whether the instance can be re-created after being previously destroyed.

Definition at line 96 of file vsingleton.h.


Constructor & Destructor Documentation

template<class T >
VSingleton< T >::VSingleton ( HolderDeletionPolicy  holderDeletionPolicy,
ThreadSafetyPolicy  threadSafetyPolicy,
ShutdownPolicy  shutdownPolicy,
ResurrectionPolicy  resurrectionPolicy = kDontAllowResurrection 
) [inline]

Constructs the singleton holder.

Parameters:
holderDeletionPolicyspecifies whether the holder can be deleted
threadSafetyPolicyspecifies whether a mutex is used to access the instance
shutdownPolicyspecifies whether the instance is shut down via the shutdown registry
resurrectionPolicyspecifies whether the instance can be re-created after being previously destroyed

Definition at line 105 of file vsingleton.h.


Member Function Documentation

template<class T >
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.

Returns:
the instance of T

Definition at line 124 of file vsingleton.h.

References NULL.

template<class T >
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().


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

Copyright ©1997-2014 Trygve Isaacson. All rights reserved. This documentation was generated with Doxygen.