diff options
author | Mike Buland <eichlan@xagasoft.com> | 2006-05-03 02:56:51 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2006-05-03 02:56:51 +0000 |
commit | 33fef4a17290e7872293d8cc173bec826f24001c (patch) | |
tree | fc428c53b45fe17d2f1d35c48e9340006b3fd694 /src/singleton.h | |
parent | 1587314e55ae761983803aa828addc6854bf4ad4 (diff) | |
download | libbu++-33fef4a17290e7872293d8cc173bec826f24001c.tar.gz libbu++-33fef4a17290e7872293d8cc173bec826f24001c.tar.bz2 libbu++-33fef4a17290e7872293d8cc173bec826f24001c.tar.xz libbu++-33fef4a17290e7872293d8cc173bec826f24001c.zip |
Added the new singleton class template. Very cool, now I need to switch
all my singletons to using it.
Diffstat (limited to 'src/singleton.h')
-rw-r--r-- | src/singleton.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/singleton.h b/src/singleton.h new file mode 100644 index 0000000..c69e6f1 --- /dev/null +++ b/src/singleton.h | |||
@@ -0,0 +1,46 @@ | |||
1 | #ifndef SINGLETON_H | ||
2 | #define SINGLETON_H | ||
3 | |||
4 | #include <stdio.h> | ||
5 | |||
6 | /** | ||
7 | * Provides singleton functionality in a modular sort of way. Make this the | ||
8 | * base class of any other class and you immediately gain singleton | ||
9 | * functionality. Be sure to make your constructor and various functions use | ||
10 | * intellegent scoping. Cleanup and instantiation are performed automatically | ||
11 | * for you at first use and program exit. There are two things that you must | ||
12 | * do when using this template, first is to inherit from it with the name of | ||
13 | * your class filling in for T and then make this class a friend of your class. | ||
14 | *@code | ||
15 | * // Making the Single Singleton: | ||
16 | * class Single : public Singleton<Single> | ||
17 | * { | ||
18 | * friend class Singleton<Single>; | ||
19 | * protected: | ||
20 | * Single(); | ||
21 | * ... | ||
22 | * }; | ||
23 | @endcode | ||
24 | * You can still add public functions and variables to your new Singleton child | ||
25 | * class, but your constructor should be protected (hence the need for the | ||
26 | * friend decleration). | ||
27 | *@author Mike Buland | ||
28 | */ | ||
29 | template <class T> | ||
30 | class Singleton | ||
31 | { | ||
32 | protected: | ||
33 | Singleton() {}; | ||
34 | |||
35 | private: | ||
36 | Singleton( const Singleton& ); | ||
37 | |||
38 | public: | ||
39 | static T &getInstance() | ||
40 | { | ||
41 | static T i; | ||
42 | return i; | ||
43 | } | ||
44 | }; | ||
45 | |||
46 | #endif | ||