QCAD
Open Source 2D CAD
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RSingleton.h
Go to the documentation of this file.
1 
20 #ifndef RSINGLETON_H_
21 #define RSINGLETON_H_
22 
23 #include "core_global.h"
24 
25 #include <QApplication>
26 #include <QObject>
27 #include <QMap>
28 #include <QString>
29 #include <typeinfo>
30 #include "RDebug.h"
31 
36 public:
37  virtual ~RSingleton() {
38  }
39  template<class Derived>
40  static Derived& getInstance();
41  static void cleanUp();
42 
43 protected:
44  RSingleton();
45 
46 private:
47  static QMap<QString, RSingleton*> map;
48 };
49 
51 
52 template<class Derived>
53 Derived& RSingleton::getInstance() {
54  if (!map.contains(typeid(Derived).name())) {
55  map[typeid(Derived).name()] = new Derived;
56  }
57  Derived* d = dynamic_cast<Derived*> (map[typeid(Derived).name()]);
58  if (d == NULL) {
59  Q_ASSERT("RSingleton::getInstance(): \"Derived\" in not a singleton");
60  }
61  return *d;
62 }
63 
64 #endif /* RSINGLETON_H_ */