From 0cc22100e9ce737d4b2e181947582d00b6d65e43 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 13 Feb 2021 16:09:45 +0100 Subject: [PATCH] Add singleton class --- inferno/src/inferno/singleton.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 inferno/src/inferno/singleton.h diff --git a/inferno/src/inferno/singleton.h b/inferno/src/inferno/singleton.h new file mode 100644 index 0000000..568f3dc --- /dev/null +++ b/inferno/src/inferno/singleton.h @@ -0,0 +1,31 @@ +#ifndef SINGLETON_H +#define SINGLETON_H + +#include // std::unique_ptr + +namespace Inferno { + + template + class Singleton { + public: + static inline T& the() { + static const std::unique_ptr instance { new T { s {} } }; + return *instance; + } + + // Remove copy constructor and copy assignment operator + Singleton(const Singleton&) = delete; + Singleton& operator=(const Singleton&) = delete; + Singleton(Singleton&&) = delete; + Singleton& operator=(Singleton&&) = delete; + + protected: + Singleton() {} + + // Constructor token + struct s {}; + }; + +} // namespace Inferno + +#endif // SINGLETON_H