QCAD Application Framework
CAD Application Development and Automation.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RSingleton.h
Go to the documentation of this file.
1 #ifndef RSINGLETON_H_
2 #define RSINGLETON_H_
3 
4 #include <QApplication>
5 #include <QObject>
6 #include <QMap>
7 #include <QString>
8 #include <typeinfo>
9 #include "RDebug.h"
10 
14 class RSingleton {
15 public:
16  virtual ~RSingleton() {
17  }
18  template<class Derived>
19  static Derived& getInstance();
20  static void cleanUp();
21 
22 protected:
23  RSingleton();
24 
25 private:
26  static QMap<QString, RSingleton*> map;
27 };
28 
30 
31 template<class Derived>
32 Derived& RSingleton::getInstance() {
33  if (!map.contains(typeid(Derived).name())) {
34  map[typeid(Derived).name()] = new Derived;
35  }
36  Derived* d = dynamic_cast<Derived*> (map[typeid(Derived).name()]);
37  if (d == NULL) {
38  Q_ASSERT("RSingleton::getInstance(): \"Derived\" in not a singleton");
39  }
40  return *d;
41 }
42 
43 #endif /* RSINGLETON_H_ */