Diffstat (limited to 'noncore/multimedia/opieplayer2/singleton.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/opieplayer2/singleton.h | 49 |
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 | |||
4 | template <class Product> | ||
5 | struct DefaultSingletonCreator | ||
6 | { | ||
7 | static Product *create() { return new Product; } | ||
8 | }; | ||
9 | |||
10 | template <class Product> | ||
11 | struct NullSingletonCreator | ||
12 | { | ||
13 | static Product *create() { return 0; } | ||
14 | }; | ||
15 | |||
16 | template | ||
17 | < | ||
18 | class T, | ||
19 | template <class> class Creator = DefaultSingletonCreator | ||
20 | > | ||
21 | class Singleton | ||
22 | { | ||
23 | public: | ||
24 | static T &self() | ||
25 | { | ||
26 | if ( !s_self ) | ||
27 | s_self = Creator<T>::create(); | ||
28 | return *s_self; | ||
29 | } | ||
30 | |||
31 | protected: | ||
32 | Singleton() | ||
33 | { s_self = static_cast<T *>( this ); } | ||
34 | ~Singleton() | ||
35 | { s_self = 0; } | ||
36 | |||
37 | private: | ||
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 | |||
44 | template <class T, template <class> class Creator> | ||
45 | T *Singleton<T, Creator>::s_self = 0; | ||
46 | |||
47 | #endif // SINGLETON_H | ||
48 | /* vim: et sw=4 ts=4 | ||
49 | */ | ||