QCAD
Open Source 2D CAD
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_ */
#define QCADCORE_EXPORT
Definition: core_global.h:10
Copyright (c) 2011-2015 by Andrew Mustun.
Definition: RSingleton.h:35
virtual ~RSingleton()
Definition: RSingleton.h:37
static QMap< QString, RSingleton * > map
Copyright (c) 2011-2015 by Andrew Mustun.
Definition: RSingleton.h:47
Q_DECLARE_METATYPE(RMath *)