Debian Codegraph @

Source/Utilities/CSingletonProvider.hpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #ifndef KONCLUDE_UTILITIES_CSINGLETONPROVIDER_H
22 #define KONCLUDE_UTILITIES_CSINGLETONPROVIDER_H
23
24 // Libraries includes
25 #include <QMutex>
26
27 // Namespace includes
28
29
30 // Other includes
31
32
33 // Logger includes
34 #include "Logger/CLogger.h"
35
36 using namespace Konclude::Logger;
37
38
39 namespace Konclude {
40
41 namespace Utilities {
42
43 /*!
44 *
45 * \class CSingletonProvider
46 * \author Andreas Steigmiller
47 * \version 0.1
48 * \brief TODO
49 *
50 */
51 template<class T>
52 class CSingletonProvider {
53 // public Functions
54 public:
55 //! Constructor
56 CSingletonProvider();
57 //! Destructor
58 virtual ~CSingletonProvider();
59
60 static T *getInstance();
61 static bool hasInstance();
62
63 // private Functions
64 private:
65
66 // private Variables
67 private:
68 static QMutex *syncMutex;
69 static T *instance;
70
71 };
72
73
74 template<class T>
75 T *CSingletonProvider<T>::instance = 0;
76
77 template<class T>
78 QMutex *CSingletonProvider<T>::syncMutex = new QMutex();
79
80 template<class T>
81 CSingletonProvider<T>::CSingletonProvider() {
82 }
83
84
85 template<class T>
86 CSingletonProvider<T>::~CSingletonProvider() {
87 }
88
89
90 template<class T>
91 T *CSingletonProvider<T>::getInstance() {
92 if (instance == 0) {
93 syncMutex->lock();
94 if (instance == 0) {
95 instance = new T();
96 }
97 syncMutex->unlock();
98 }
99 return instance;
100 }
101
102
103 template<class T>
104 bool CSingletonProvider<T>::hasInstance() {
105 return instance != 0;
106 }
107
108
109 }; // end namespace Utilities
110
111 }; // end namespace Konclude
112
113 #endif // KONCLUDE_UTILITIES_CSINGLETONPROVIDER_H