summaryrefslogtreecommitdiff
path: root/noncore/multimedia/opieplayer2/singleton.h
Unidiff
Diffstat (limited to 'noncore/multimedia/opieplayer2/singleton.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/multimedia/opieplayer2/singleton.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/noncore/multimedia/opieplayer2/singleton.h b/noncore/multimedia/opieplayer2/singleton.h
new file mode 100644
index 0000000..35b3dff
--- a/dev/null
+++ b/noncore/multimedia/opieplayer2/singleton.h
@@ -0,0 +1,49 @@
1#ifndef SINGLETON_H
2#define SINGLETON_H
3
4template <class Product>
5struct DefaultSingletonCreator
6{
7 static Product *create() { return new Product; }
8};
9
10template <class Product>
11struct NullSingletonCreator
12{
13 static Product *create() { return 0; }
14};
15
16template
17<
18 class T,
19 template <class> class Creator = DefaultSingletonCreator
20>
21class Singleton
22{
23public:
24 static T &self()
25 {
26 if ( !s_self )
27 s_self = Creator<T>::create();
28 return *s_self;
29 }
30
31protected:
32 Singleton()
33 { s_self = static_cast<T *>( this ); }
34 ~Singleton()
35 { s_self = 0; }
36
37private:
38 Singleton( const Singleton<T, Creator> &rhs );
39 Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs );
40
41 static T *s_self;
42};
43
44template <class T, template <class> class Creator>
45T *Singleton<T, Creator>::s_self = 0;
46
47#endif // SINGLETON_H
48/* vim: et sw=4 ts=4
49 */