aboutsummaryrefslogtreecommitdiff
path: root/src/singleton.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/singleton.h')
-rw-r--r--src/singleton.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/singleton.h b/src/singleton.h
new file mode 100644
index 0000000..4976a61
--- /dev/null
+++ b/src/singleton.h
@@ -0,0 +1,62 @@
1#ifndef SINGLETON_H
2#define SINGLETON_H
3
4#include <stdio.h>
5
6namespace Bu
7{
8 /**
9 * Provides singleton functionality in a modular sort of way. Make this the
10 * base class of any other class and you immediately gain singleton
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
30 */
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() {};
41
42 private:
43 /**
44 * Copy constructor, defined so that you could write your own as well.
45 */
46 Singleton( const Singleton& );
47
48 public:
49 /**
50 * Get a handle to the contained instance of the contained class. It is
51 * a reference.
52 *@returns A reference to the contained object.
53 */
54 static T &getInstance()
55 {
56 static T i;
57 return i;
58 }
59 };
60}
61
62#endif