#pragma once #define _BT_ASSERT( _CONDITION_ ) if (!_CONDITION_) { _asm int 3 } template<typename T> class Singleton { static T* ms_singleton; public: Singleton() { _BT_ASSERT(!ms_singleton); //use a cunning trick to get the singleton pointing to the start of the whole, rather than //the start of the Singleton part of the object int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1; ms_singleton = (T*)((int)this + offset); } ~Singleton() { _BT_ASSERT(ms_singleton); ms_singleton=0; } static bool Initialized() { return NULL != ms_singleton; } static T& GetSingleton() { _BT_ASSERT(ms_singleton); return *ms_singleton; } static T* GetSingletonPtr() { _BT_ASSERT(ms_singleton); return ms_singleton; } }; template <typename T> T* Singleton <T>::ms_singleton = 0;