summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/multimedia/opieplayer2/singleton.h13
1 files changed, 11 insertions, 2 deletions
diff --git a/noncore/multimedia/opieplayer2/singleton.h b/noncore/multimedia/opieplayer2/singleton.h
index 83e228f..825344e 100644
--- a/noncore/multimedia/opieplayer2/singleton.h
+++ b/noncore/multimedia/opieplayer2/singleton.h
@@ -1,68 +1,77 @@
1/* 1/*
2 Copyright (C) 2002 Simon Hausmann <simon@lst.de> 2 Copyright (C) 2002 Simon Hausmann <simon@lst.de>
3 3
4 This library is free software; you can redistribute it and/or 4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public 5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either 6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version. 7 version 2 of the License, or (at your option) any later version.
8 8
9 This library is distributed in the hope that it will be useful, 9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details. 12 Library General Public License for more details.
13 13
14 You should have received a copy of the GNU Library General Public License 14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to 15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. 17 Boston, MA 02111-1307, USA.
18*/ 18*/
19 19
20#ifndef SINGLETON_H 20#ifndef SINGLETON_H
21#define SINGLETON_H 21#define SINGLETON_H
22 22
23#include "threadutil.h"
24
23template <class Product> 25template <class Product>
24struct DefaultSingletonCreator 26struct DefaultSingletonCreator
25{ 27{
26 static Product *create() { return new Product; } 28 static Product *create() { return new Product; }
27}; 29};
28 30
29template <class Product> 31template <class Product>
30struct NullSingletonCreator 32struct NullSingletonCreator
31{ 33{
32 static Product *create() { return 0; } 34 static Product *create() { return 0; }
33}; 35};
34 36
35template 37template
36< 38<
37 class T, 39 class T,
38 template <class> class Creator = DefaultSingletonCreator 40 template <class> class Creator = DefaultSingletonCreator
39> 41>
40class Singleton 42class Singleton
41{ 43{
42public: 44public:
43 static T &self() 45 static T &self()
44 { 46 {
45 if ( !s_self ) 47 if ( !s_self ) {
46 s_self = Creator<T>::create(); 48 ThreadUtil::AutoLock lock( s_guard );
49 if ( !s_self )
50 s_self = Creator<T>::create();
51 }
47 return *s_self; 52 return *s_self;
48 } 53 }
49 54
50protected: 55protected:
51 Singleton() 56 Singleton()
52 { s_self = static_cast<T *>( this ); } 57 { s_self = static_cast<T *>( this ); }
53 ~Singleton() 58 ~Singleton()
54 { s_self = 0; } 59 { s_self = 0; }
55 60
56private: 61private:
57 Singleton( const Singleton<T, Creator> &rhs ); 62 Singleton( const Singleton<T, Creator> &rhs );
58 Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs ); 63 Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs );
59 64
60 static T *s_self; 65 static T *s_self;
66 static ThreadUtil::Mutex s_guard;
61}; 67};
62 68
63template <class T, template <class> class Creator> 69template <class T, template <class> class Creator>
64T *Singleton<T, Creator>::s_self = 0; 70T *Singleton<T, Creator>::s_self = 0;
65 71
72template <class T, template <class> class Creator>
73ThreadUtil::Mutex Singleton<T, Creator>::s_guard;
74
66#endif // SINGLETON_H 75#endif // SINGLETON_H
67/* vim: et sw=4 ts=4 76/* vim: et sw=4 ts=4
68 */ 77 */