diff options
Diffstat (limited to '')
-rw-r--r-- | src/leeds/base/singletonManager.h | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/leeds/base/singletonManager.h b/src/leeds/base/singletonManager.h new file mode 100644 index 00000000..9c980bb2 --- /dev/null +++ b/src/leeds/base/singletonManager.h @@ -0,0 +1,62 @@ +#pragma once + +#include "common.h" + +namespace base +{ + +class cSingletonBase; + +class cSingletonManager +{ + cSingletonBase* head; + cSingletonBase* tail; + +public: + cSingletonManager() : + head(nil), + tail(nil) + {} + + void Add(cSingletonBase*); + void Purge(); + ~cSingletonManager(); +}; + +cSingletonManager& SingletonManager(); + +class cSingletonBase +{ + friend class cSingletonManager; + + cSingletonBase* next; + +public: + virtual ~cSingletonBase() {} +}; + +template<typename T> +class cSingleton : public cSingletonBase +{ + static T* mspInstance; + static void CreateInstance() + { + mspInstance = new T(); + SingletonManager().Add(mspInstance); + } + +public: + static T* Instance() + { + if (!mspInstance) + CreateInstance(); + return mspInstance; + } + + ~cSingleton<T>() + { + mspInstance = nil; + } +}; + +}
\ No newline at end of file |