Listing 8
template <class T> class DeletableSingleton { public: static void ScheduleDestruction(T*, void (*pFun)()) { static bool firstPass = true; isDead = false; deleter = pFun; if (firstPass || needCallback) { std::atexit(atexitCallback); firstPass = false; needCallback = false; } } static void OnDeadReference() { } static void GracefulDelete(); protected: static void (*deleter) (); static bool isDead; static bool needCallback; static void atexitCallback(); }; template <class T> void (*DeletableSingleton<T>::deleter) (); template <class T> bool DeletableSingleton<T>::isDead = true; template <class T> bool DeletableSingleton<T>::needCallback = true; template< class T > void DeletableSingleton<T>::GracefulDelete() { if (isDead) return; isDead = true; deleter(); } template< class T > void DeletableSingleton<T>::atexitCallback() { needCallback = #ifdef ATEXIT_FIXED true; #else false; #endif GracefulDelete(); }