vpr::Singleton< singleClass > Class Template Reference

You can use this coolio class to make a singleton, just inherit like so:. More...

#include <vpr/Util/Singleton.h>

Inheritance diagram for vpr::Singleton< singleClass >:

Inheritance graph
[legend]
List of all members.

Static Public Member Functions

static singleClass * instance ()
 
Note:
Currently, this function is thread safe after first call to instance().


Protected Member Functions

 Singleton ()
 Don't create a singleton with new! use instance().
virtual ~Singleton ()
 don't delete a singleton!

Detailed Description

template<class singleClass>
class vpr::Singleton< singleClass >

You can use this coolio class to make a singleton, just inherit like so:.

 class myClass : public vpr::Singleton<myClass>

Parameters:
singleClass The type to be used a singleton.
Note:
This class is currently broken under certain circumstances. Refer to SourceForge bug #'s 729485 and 929003. Until these bugs are fixed, use of the preprocessor macros vprSingletonHeader() et. al. are recommended.

Definition at line 117 of file Singleton.h.


Constructor & Destructor Documentation

template<class singleClass>
vpr::Singleton< singleClass >::Singleton (  )  [inline, protected]

Don't create a singleton with new! use instance().

Definition at line 153 of file Singleton.h.

00154       {
00155       }

template<class singleClass>
virtual vpr::Singleton< singleClass >::~Singleton (  )  [inline, protected, virtual]

don't delete a singleton!

Definition at line 158 of file Singleton.h.

00159       {
00160       }


Member Function Documentation

template<class singleClass>
static singleClass* vpr::Singleton< singleClass >::instance (  )  [inline, static]

Note:
Currently, this function is thread safe after first call to instance().

if first call to instance happens multiple times simultaneously then don't be surprised when something dies because of a mutex. This bug can be caused by spawning two threads immediately after entering main().

Definition at line 127 of file Singleton.h.

Referenced by vpr::InetAddrSIM::setAddress().

00128       {
00129          // WARNING! race condition possibility, creation of static vars
00130          // are not thread safe.  This is only an issue when creating
00131          // your first thread, since it uses a singleton thread manager,
00132          // the two threads might both try to call instance at the same time
00133          // which then the creation of the following mutex would not be certain.
00134          static vpr::Mutex singleton_lock1;
00135          static singleClass* the_instance1 = NULL;
00136 
00137          if (the_instance1 == NULL)
00138          {
00139             vpr::Guard<vpr::Mutex> guard(singleton_lock1);
00140             if (the_instance1 == NULL)
00141             {
00142                the_instance1 = new singleClass;
00143             }
00144          }
00145          return the_instance1;
00146       }


The documentation for this class was generated from the following file:
Generated on Thu Jan 4 10:56:08 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1