summaryrefslogtreecommitdiffstats
path: root/core/singleton.h
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-09-22 14:33:33 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-09-25 13:35:30 -0700
commit05200f9266a4e8e723450abb281c597a48da2e9f (patch)
tree670d7b30f4b77f40e35253243f95fb219d5c7aea /core/singleton.h
parentf20d6187f0135ef3cf7f583785be65012336a99d (diff)
downloadsubsurface-05200f9266a4e8e723450abb281c597a48da2e9f.tar.gz
Cleanup: unify idiosyncratic singletons
The way we handle singletons in QML, QML insists on allocating the objects. This leads to a very idiosyncratic way of handling singletons: The global instance pointer is set in the constructor. Unify all these by implementing a "SillySingleton" template. All of the weird singleton-classes can derive from this template and don't have to bother with reimplementing the instance() function with all the safety-checks, etc. This serves firstly as documentation but also improves debugging as we will now see wanted and unwanted creation and destruction of these weird singletons. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/singleton.h')
-rw-r--r--core/singleton.h74
1 files changed, 74 insertions, 0 deletions
diff --git a/core/singleton.h b/core/singleton.h
new file mode 100644
index 000000000..9573b07e8
--- /dev/null
+++ b/core/singleton.h
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * We use singletons in numerous places. In combination with QML this gives
+ * us a very fundamental problem, because QML likes to allocate objects by
+ * itself. There are known workarounds, but currently we use idiosyncratic
+ * singleton classes, which initialize the global instance pointer in the
+ * constructor. Things get even more complicated if the same singleton is used
+ * on mobile and on desktop. The latter might want to simply use the classical
+ * instance() function without having to initialize the singleton first, as
+ * that would beat the purpose of the instance() method.
+ *
+ * The template defined here, SillySingleton, codifies all this. Simply derive
+ * a class from this template:
+ * class X : public SillySingleton<X> {
+ * ...
+ * };
+ * This will generate an instance() method. This will do the right thing for
+ * both methods: explicit construction of the singleton via new or implicit
+ * by calling the instance() method. It will also generate warnings if a
+ * singleton class is generated more than once (i.e. first instance() is called
+ * and _then_ new).
+ *
+ * In the long run we should get rid of all users of this class.
+ */
+#ifndef SINGLETON_H
+#define SINGLETON_H
+
+#include <typeinfo>
+#include <QtGlobal>
+
+// 1) Declaration
+template<typename T>
+class SillySingleton {
+ static T *self;
+protected:
+ SillySingleton();
+ ~SillySingleton();
+public:
+ static T *instance();
+};
+
+template<typename T>
+T *SillySingleton<T>::self = nullptr;
+
+// 2) Implementation
+
+template<typename T>
+SillySingleton<T>::SillySingleton()
+{
+ if (self)
+ qWarning("Generating second instance of singleton %s", typeid(T).name());
+ self = static_cast<T *>(this);
+ qDebug("Generated singleton %s", typeid(T).name());
+}
+
+template<typename T>
+SillySingleton<T>::~SillySingleton()
+{
+ if (self == this)
+ self = nullptr;
+ else
+ qWarning("Destroying unknown instance of singleton %s", typeid(T).name());
+ qDebug("Destroyed singleton %s", typeid(T).name());
+}
+
+template<typename T>
+T *SillySingleton<T>::instance()
+{
+ if (!self)
+ new T;
+ return self;
+}
+
+#endif