#ifndef _CSINGLETON_H_ #define _CSINGLETON_H_ #include template class CSingleton { private: static Derived* ms_pSingleton; protected: CSingleton(); ~CSingleton(); public: inline static Derived& GetInstance(); inline static Derived* GetInstancePtr(); }; template CSingleton::CSingleton() { assert(!ms_pSingleton && "Singleton Ŭ·¡½º°¡ ÀÌ¹Ì »ý¼ºµÇ¾î ÀÖ½À´Ï´Ù."); size_t nOffset = (size_t)(Derived*) 1 - (size_t)(CSingleton*)(Derived*) 1; ms_pSingleton = (Derived*)((size_t)this + nOffset); } template CSingleton::~CSingleton() { assert(ms_pSingleton && "Singleton Ŭ·¡½º°¡ »ý¼ºµÇÁö ¾Ê¾Ò½À´Ï´Ù"); ms_pSingleton = 0; } template inline Derived& CSingleton::GetInstance() { assert(ms_pSingleton && "Singleton Ŭ·¡½º°¡ »ý¼ºµÇÁö ¾Ê¾Ò½À´Ï´Ù"); return (*ms_pSingleton); } template inline Derived* CSingleton::GetInstancePtr() { assert(ms_pSingleton && "Singleton Ŭ·¡½º°¡ »ý¼ºµÇÁö ¾Ê¾Ò½À´Ï´Ù"); return (ms_pSingleton); } template Derived* CSingleton::ms_pSingleton = 0; #endif