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