From 255a3f96e151a64412d5d6e83bf80972758602ea Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 24 Jun 2017 15:13:04 +0100 Subject: Split out plugin manager's global default instance into a reusable template class --- libadhocutil/globalStatic.h | 19 +++++++++++++++++++ libadhocutil/globalStatic.impl.h | 36 ++++++++++++++++++++++++++++++++++++ libadhocutil/plugins.cpp | 23 +++-------------------- 3 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 libadhocutil/globalStatic.h create mode 100644 libadhocutil/globalStatic.impl.h diff --git a/libadhocutil/globalStatic.h b/libadhocutil/globalStatic.h new file mode 100644 index 0000000..544cc1e --- /dev/null +++ b/libadhocutil/globalStatic.h @@ -0,0 +1,19 @@ +#ifndef ADHOCUTIL_GLOBALSTATIC_H +#define ADHOCUTIL_GLOBALSTATIC_H + +namespace AdHoc { + template + class GlobalStatic { + public: + static Object * get(); + + private: + static void createObject() __attribute__((constructor(101))); + static void deleteObject() __attribute__((destructor(101))); + + inline static Object * & instance(); + }; +} + +#endif + diff --git a/libadhocutil/globalStatic.impl.h b/libadhocutil/globalStatic.impl.h new file mode 100644 index 0000000..2a5cf05 --- /dev/null +++ b/libadhocutil/globalStatic.impl.h @@ -0,0 +1,36 @@ +#ifndef ADHOCUTIL_GLOBALSTATIC_IMPL_H +#define ADHOCUTIL_GLOBALSTATIC_IMPL_H + +#include "globalStatic.h" + +namespace AdHoc { + template + Object * GlobalStatic::get() + { + return instance(); + } + + template + void GlobalStatic::createObject() + { + instance() = new Object(); + } + + template + void GlobalStatic::deleteObject() + { + auto & i = instance(); + delete i; + i = nullptr; + } + + template + Object * & GlobalStatic::instance() + { + static Object * _instance = nullptr; + return _instance; + } +}; + +#endif + diff --git a/libadhocutil/plugins.cpp b/libadhocutil/plugins.cpp index 0435daa..90d7b02 100644 --- a/libadhocutil/plugins.cpp +++ b/libadhocutil/plugins.cpp @@ -4,6 +4,7 @@ #include #include #include "compileTimeFormatter.h" +#include "globalStatic.impl.h" namespace std { bool @@ -29,25 +30,7 @@ namespace std { } namespace AdHoc { - static void createDefaultManager() __attribute__((constructor(101))); - static void deleteDefaultManager() __attribute__((destructor(101))); - - static AdHoc::PluginManager * defaultPluginManager; - - static - void - createDefaultManager() - { - defaultPluginManager = new PluginManager(); - } - - static - void - deleteDefaultManager() - { - delete defaultPluginManager; - defaultPluginManager = nullptr; - } + template class GlobalStatic; AbstractPluginImplementation::~AbstractPluginImplementation() = default; @@ -100,7 +83,7 @@ namespace AdHoc { PluginManager * PluginManager::getDefault() { - return defaultPluginManager; + return GlobalStatic::get(); } void -- cgit v1.2.3