Files Classes Functions Hierarchy
#include <singleton.h>
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. | |
| T * | operator-> () |
| Access the object. | |
| T & | operator() () |
| Access the object. | |
Static Public Attributes | |
| static int | count = 0 |
| Count of the ojbects pointed to by ptr. | |
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.
//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.
| enum SingletonPtr::memoryRelease |
| 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 }
| 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 }
| 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 }
| 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 }
| T & SingletonPtr< T >::operator() | ( | ) | [inline] |
| T * SingletonPtr< T >::operator-> | ( | ) | [inline] |
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().
1.5.8