diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/SingletonHolder.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/SingletonHolder.h index 19e7c0f728ac..e189b49c31cd 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/SingletonHolder.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/SingletonHolder.h @@ -30,84 +30,84 @@ namespace Mantid { -namespace Kernel -{ - -/// prototype for function passed to atexit() -typedef void (*atexit_func_t)(); - -extern MANTID_KERNEL_DLL void CleanupSingletons(); -extern MANTID_KERNEL_DLL void AddSingleton(atexit_func_t func); - -/// class to manage an instance of an object as a singleton -template -class SingletonHolder -{ -public: - static T& Instance(); - -private: - static void DestroySingleton(); - /// default constructor marked private so only access is via the Instance() method - SingletonHolder(); - - static T* pInstance; - static bool destroyed; -}; - -/// Implementation of the SingletonHolder create policy using the new and delete operators -template -struct CreateUsingNew -{ - /// create an object using the new operator - /// @returns New instance - static T* Create(){return new T;} - /// delete an object instantiated using Create - /// @param p :: pointer to instance to destroy - static void Destroy(T* p){delete p;} -}; - -/// Return a reference to the Singleton instance, creating it if it does not already exist -/// Creation is done using the CreateUsingNew policy at the moment -template -inline T& SingletonHolder::Instance() -{ - if (destroyed) - { - std::string s("Attempt to use destroyed singleton "); - s += typeid(T).name(); - throw std::runtime_error(s.c_str()); - } - if (!pInstance) - { -// std::cerr << "creating singleton " << typeid(T).name() << std::endl; - pInstance = CreateUsingNew::Create(); - AddSingleton(&DestroySingleton); - //atexit(&CleanupSingletons); - } - return *pInstance; -} - -/// Destroy the singleton -template -void SingletonHolder::DestroySingleton() -{ - //std::cerr << "destroying singleton " << typeid(T).name() << std::endl; - assert(!destroyed); - CreateUsingNew::Destroy(pInstance); - pInstance = 0; - destroyed = true; -} - -/// global variable holding pointer to singleton instance -template -T* SingletonHolder::pInstance = 0; - -/// variable to allow trapping of attempts to destroy a singleton more than once -template -bool SingletonHolder::destroyed = false; - -} + namespace Kernel + { + + /// prototype for function passed to atexit() + typedef void (*atexit_func_t)(); + + extern MANTID_KERNEL_DLL void CleanupSingletons(); + extern MANTID_KERNEL_DLL void AddSingleton(atexit_func_t func); + + /// class to manage an instance of an object as a singleton + template + class SingletonHolder + { + public: + static T& Instance(); + + private: + static void DestroySingleton(); + /// default constructor marked private so only access is via the Instance() method + SingletonHolder(); + + static T* pInstance; + static bool destroyed; + }; + + /// Implementation of the SingletonHolder create policy using the new and delete operators + template + struct CreateUsingNew + { + /// create an object using the new operator + /// @returns New instance + static T* Create(){return new T;} + /// delete an object instantiated using Create + /// @param p :: pointer to instance to destroy + static void Destroy(T* p){delete p;} + }; + + /// Return a reference to the Singleton instance, creating it if it does not already exist + /// Creation is done using the CreateUsingNew policy at the moment + template + inline T& SingletonHolder::Instance() + { + if (destroyed) + { + std::string s("Attempt to use destroyed singleton "); + s += typeid(T).name(); + throw std::runtime_error(s.c_str()); + } + if (!pInstance) + { + // std::cerr << "creating singleton " << typeid(T).name() << std::endl; + pInstance = CreateUsingNew::Create(); + AddSingleton(&DestroySingleton); + //atexit(&CleanupSingletons); + } + return *pInstance; + } + + /// Destroy the singleton + template + void SingletonHolder::DestroySingleton() + { + //std::cerr << "destroying singleton " << typeid(T).name() << std::endl; + assert(!destroyed); + CreateUsingNew::Destroy(pInstance); + pInstance = 0; + destroyed = true; + } + + /// global variable holding pointer to singleton instance + template + T* SingletonHolder::pInstance = 0; + + /// variable to allow trapping of attempts to destroy a singleton more than once + template + bool SingletonHolder::destroyed = false; + + } } #endif //SINGLETON_HOLDER_H