proj home

Files   Classes   Functions   Hierarchy  

SingletonPtr< T > Class Template Reference

Singleton smart pointer. More...

#include <singleton.h>

Collaboration diagram for SingletonPtr< T >:

List of all members.

Public Types

enum  memoryRelease { never = 0, nonarray, array }
 Define how the pointer is deleted. More...

Public Member Functions

 SingletonPtr (T *app)
 No memory management by default, callers responsibility to manage memory.
 SingletonPtr (T *app, SingletonPtr< T >::memoryRelease m)
 Specifies how to delete pointer.
 SingletonPtr ()
 Create a temporary to access the pointer.
 ~SingletonPtr ()
 Cleanup.
Toperator-> ()
 Access the object.
Toperator() ()
 Access the object.

Static Public Attributes

static int count = 0
 Count of the ojbects pointed to by ptr.


Detailed Description

template<class T>
class SingletonPtr< T >

Singleton smart pointer.

A singleton is a pointer to a unique (only one) object.

I do not generally use this as C++ provides a low tech solution with static variables that is simpler or more primitive.

However this does a little more by supporting reference counting deletion, if the client so chooses for array and not array objects.

The class can also be used without memory management. References are still counted but they can become zero and still have a valid pointer, because it is the clients responsibility. The following is an example of this technique.

Example
  //Initialize the pointer.
  SingletonPtr< A > init(&app);  // Be aware of app object's lifetime
  // Let init die.

  //... Access the object anywhere in the C++ universe.
  SingletonPtr< A >()-> A's methods/data

Definition at line 49 of file singleton.h.


Member Enumeration Documentation

template<class T>
enum SingletonPtr::memoryRelease

Define how the pointer is deleted.

Enumerator:
never 
nonarray 
array 

Definition at line 58 of file singleton.h.

00058 { never=0, nonarray, array };


Constructor & Destructor Documentation

template<class T >
SingletonPtr< T >::SingletonPtr ( T app  )  [inline, explicit]

No memory management by default, callers responsibility to manage memory.

Definition at line 99 of file singleton.h.

References SingletonPtr< T >::count, and SingletonPtr< T >::never.

00100 {
00101    assert(ptr==0);
00102 
00103   ptr = app;
00104   count = 1;
00105   memorystate = never;
00106 }

template<class T >
SingletonPtr< T >::SingletonPtr ( T app,
SingletonPtr< T >::memoryRelease  m 
) [inline, explicit]

Specifies how to delete pointer.

Definition at line 111 of file singleton.h.

00112 {
00113 
00114   assert(ptr==0);
00115 
00116   memorystate = m;
00117 
00118   ptr = app;
00119   count = 1;
00120 }

template<class T >
SingletonPtr< T >::SingletonPtr (  )  [inline]

Create a temporary to access the pointer.

Definition at line 124 of file singleton.h.

References SingletonPtr< T >::count, and SingletonPtr< T >::never.

00125 {
00126   assert(ptr!=0);
00127   assert( (count>=1) || (memorystate==never));
00128 
00129   ++count;
00130 }

template<class T >
SingletonPtr< T >::~SingletonPtr (  )  [inline]

Cleanup.

Definition at line 149 of file singleton.h.

References SingletonPtr< T >::array, SingletonPtr< T >::count, SingletonPtr< T >::never, and SingletonPtr< T >::nonarray.

00150 {
00151   --count;
00152   if (count<=0)
00153   {
00154 
00155 assert(count==0);
00156 
00157     switch(memorystate)
00158     {
00159       case never: break;
00160       case array: delete[] ptr; break;
00161       case nonarray: delete ptr; break;
00162     }
00163 
00164     if (memorystate!=never)
00165       ptr=0;
00166   }
00167 }


Member Function Documentation

template<class T >
T & SingletonPtr< T >::operator() (  )  [inline]

Access the object.

Definition at line 141 of file singleton.h.

00142 {
00143   assert(ptr!=0);
00144 
00145   return *ptr;
00146 }

template<class T >
T * SingletonPtr< T >::operator-> (  )  [inline]

Access the object.

Definition at line 133 of file singleton.h.

00134 {
00135   assert(ptr!=0);
00136 
00137   return ptr;
00138 }


Member Data Documentation

template<class T>
int SingletonPtr< T >::count = 0 [inline, static]

Count of the ojbects pointed to by ptr.

Definition at line 55 of file singleton.h.

Referenced by SingletonPtr< T >::SingletonPtr(), and SingletonPtr< T >::~SingletonPtr().


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

Generated on Fri Mar 4 00:50:17 2011 for Chelton Evans Source by  doxygen 1.5.8