diff options
Diffstat (limited to 'src/singleton.h')
-rw-r--r-- | src/singleton.h | 101 |
1 files changed, 52 insertions, 49 deletions
diff --git a/src/singleton.h b/src/singleton.h index 47adbd5..c43d71b 100644 --- a/src/singleton.h +++ b/src/singleton.h | |||
@@ -1,59 +1,62 @@ | |||
1 | #ifndef SINGLETON_H | 1 | #ifndef BU_SINGLETON_H |
2 | #define SINGLETON_H | 2 | #define BU_SINGLETON_H |
3 | 3 | ||
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | 5 | ||
6 | /** | 6 | namespace Bu |
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 | { | 7 | { |
32 | protected: | ||
33 | /** | 8 | /** |
34 | * Private constructor. This constructor is empty but has a body so that | 9 | * Provides singleton functionality in a modular sort of way. Make this the |
35 | * you can make your own override of it. Be sure that you're override is | 10 | * base class of any other class and you immediately gain singleton |
36 | * also protected. | 11 | * functionality. Be sure to make your constructor and various functions use |
12 | * intellegent scoping. Cleanup and instantiation are performed automatically | ||
13 | * for you at first use and program exit. There are two things that you must | ||
14 | * do when using this template, first is to inherit from it with the name of | ||
15 | * your class filling in for T and then make this class a friend of your class. | ||
16 | *@code | ||
17 | * // Making the Single Singleton: | ||
18 | * class Single : public Singleton<Single> | ||
19 | * { | ||
20 | * friend class Singleton<Single>; | ||
21 | * protected: | ||
22 | * Single(); | ||
23 | * ... | ||
24 | * }; | ||
25 | @endcode | ||
26 | * You can still add public functions and variables to your new Singleton child | ||
27 | * class, but your constructor should be protected (hence the need for the | ||
28 | * friend decleration). | ||
29 | *@author Mike Buland | ||
37 | */ | 30 | */ |
38 | Singleton() {}; | 31 | template <class T> |
32 | class Singleton | ||
33 | { | ||
34 | protected: | ||
35 | /** | ||
36 | * Private constructor. This constructor is empty but has a body so that | ||
37 | * you can make your own override of it. Be sure that you're override is | ||
38 | * also protected. | ||
39 | */ | ||
40 | Singleton() {}; | ||
39 | 41 | ||
40 | private: | 42 | private: |
41 | /** | 43 | /** |
42 | * Copy constructor, defined so that you could write your own as well. | 44 | * Copy constructor, defined so that you could write your own as well. |
43 | */ | 45 | */ |
44 | Singleton( const Singleton& ); | 46 | Singleton( const Singleton& ); |
45 | 47 | ||
46 | public: | 48 | public: |
47 | /** | 49 | /** |
48 | * Get a handle to the contained instance of the contained class. It is | 50 | * Get a handle to the contained instance of the contained class. It is |
49 | * a reference. | 51 | * a reference. |
50 | *@returns A reference to the contained object. | 52 | *@returns A reference to the contained object. |
51 | */ | 53 | */ |
52 | static T &getInstance() | 54 | static T &getInstance() |
53 | { | 55 | { |
54 | static T i; | 56 | static T i; |
55 | return i; | 57 | return i; |
56 | } | 58 | } |
57 | }; | 59 | }; |
60 | } | ||
58 | 61 | ||
59 | #endif | 62 | #endif |