diff options
| author | Dan Goodliffe <dan@randomdan.homeip.net> | 2017-06-24 15:13:04 +0100 | 
|---|---|---|
| committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2017-06-24 15:13:04 +0100 | 
| commit | 255a3f96e151a64412d5d6e83bf80972758602ea (patch) | |
| tree | 62f57d109a1a2e801ad886909abc62918317e69a | |
| parent | Standalone split (diff) | |
| download | libadhocutil-255a3f96e151a64412d5d6e83bf80972758602ea.tar.bz2 libadhocutil-255a3f96e151a64412d5d6e83bf80972758602ea.tar.xz libadhocutil-255a3f96e151a64412d5d6e83bf80972758602ea.zip  | |
Split out plugin manager's global default instance into a reusable template classlibadhocutil-0.4.2
| -rw-r--r-- | libadhocutil/globalStatic.h | 19 | ||||
| -rw-r--r-- | libadhocutil/globalStatic.impl.h | 36 | ||||
| -rw-r--r-- | libadhocutil/plugins.cpp | 23 | 
3 files changed, 58 insertions, 20 deletions
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<typename Object> +	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<typename Object> +	Object * GlobalStatic<Object>::get() +	{ +		return instance(); +	} + +	template<typename Object> +	void GlobalStatic<Object>::createObject() +	{ +		instance() = new Object(); +	} + +	template<typename Object> +	void GlobalStatic<Object>::deleteObject() +	{ +		auto & i = instance(); +		delete i; +		i = nullptr; +	} + +	template<typename Object> +	Object * & GlobalStatic<Object>::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 <boost/multi_index_container.hpp>  #include <boost/multi_index/ordered_index.hpp>  #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<PluginManager>;  	AbstractPluginImplementation::~AbstractPluginImplementation() = default; @@ -100,7 +83,7 @@ namespace AdHoc {  	PluginManager *  	PluginManager::getDefault()  	{ -		return defaultPluginManager; +		return GlobalStatic<PluginManager>::get();  	}  	void  | 
