roviz  0.7
Code Documentation of roviz
singleton.h
1 #ifndef SINGLETON_H
2 #define SINGLETON_H
3 
4 #include "appcore.h"
5 #include <QtGlobal>
6 #include <QDebug>
7 
12 class ITEMFRAMEWORK_EXPORT AbstractSingleton
13 {
14  Q_GADGET //Needed in order to use qobject_cast / interfaces stuff
15 public:
16  AbstractSingleton() {} //In order to stay constructible (needed because of Q_DISABLE_COPY macro)
17  virtual ~AbstractSingleton() {}
18 protected:
19  friend class ObjectState; //Grant ObjectState access to the following members:
24  virtual bool postInit() = 0;
29  virtual bool preDestroy() = 0;
30 
31  Q_DISABLE_COPY(AbstractSingleton)
32 };
33 Q_DECLARE_INTERFACE(AbstractSingleton, "Itemframework.Interface.Singleton/1.0")
34 
35 
38 class ITEMFRAMEWORK_EXPORT SingletonStorage
39 {
40  //Why do we need this class, and can't we just add a static data member of Type T* to the Singleton<T> class?
41  // Well, the static member would need to be defined and initialized in a separate cpp file. This is not possible for template classes.
42  // But you could return a reference to a local static variable from a static function and use that as storage!
43  // Yes, this works on Linux, but not on Windows :( (Reason unknown).
44 protected:
45  static AbstractSingleton* getInstance(const char* name);
46  static void storeInstance(const char* name, AbstractSingleton* inst);
47 };
48 
54 template<typename T> class ITEMFRAMEWORK_EXPORT Singleton : public AbstractSingleton, private SingletonStorage
55 {
56 public:
57  Singleton() //Should only be called from ObjectState Class
58  {
59  storeInstance(T::staticMetaObject.className(), this); //Store this instance in the Hashmap
60  }
61 
62  ~Singleton() //Should only be called from ObjectState Class
63  {
64  storeInstance(T::staticMetaObject.className(), nullptr); //Remove this instance from the Hashmap
65  }
66 
71  static T* instance()
72  {
73  return static_cast<T*>(getInstance(T::staticMetaObject.className())); //Retrieve Instance from Hashmap by classname
74  }
75 };
76 
77 
78 
79 #endif // SINGLETON_H
bool preDestroy()
Tries to call preDestroy() on the represented singleton. Does nothing if the represented class is a c...
Definition: startup_helper.cpp:352
bool postInit()
Tries to call postInit() on the represented singleton. Does nothing if the represented class is a com...
Definition: startup_helper.cpp:339
The ObjectState class represents the State of a Singleton or Component and helps calling create/delet...
Definition: startup_helper_p.h:11
static T * instance()
Returns the current instance of the singleton.
Definition: singleton.h:71
The AbstractSingleton class defines functions that every singleton must implement Every Singleton mus...
Definition: singleton.h:12
The SingletonStorage class is a helper to provide a unique, once-per-application, shared-across-all-p...
Definition: singleton.h:38
The Singleton Template figures as baseclass for all Singletons. It provides a instance() functions wh...
Definition: singleton.h:54