/* * Singleton.hpp * * Created on: Mar 10, 2010 * Author: crueger */ #ifndef SINGLETON_HPP_ #define SINGLETON_HPP_ #include #include #include "defs.hpp" /** * This template produces the generic singleton pattern using the CRTP idiom. */ template class Singleton { private: // simple auto_ptr that allows destruction of the object // std::auto_ptr cannot do this because the destructor of T is ussually private class ptr_t { public: ptr_t(); ptr_t(T* _content); ~ptr_t(); T& operator*(); T* get(); void reset(T* _content); void reset(); ptr_t& operator=(ptr_t& rhs); private: T* content; }; /** * this creator checks what it may or may not do */ template struct creator_t { static creator_T* make(); static void set(creator_T*&,creator_T*); }; // specialization to allow fast creations template struct creator_t{ static creator_T* make(){ return new creator_T(); } static void set(creator_T*&,creator_T*){ assert(0 && "Cannot set the Instance for a singleton of this type"); } }; template struct creator_t{ static creator_T* make(){ assert(0 && "Cannot create a singleton of this type directly"); } static void set(ptr_t& dest,creator_T* src){ dest.reset(src); } }; public: // make the state of this singleton accessible static const bool may_create=_may_create; // this is used for creation typedef creator_t creator; static T& getInstance(); static T* getPointer(); static void purgeInstance(); static T& resetInstance(); static void setInstance(T*); protected: private: static boost::recursive_mutex instanceLock; static ptr_t theInstance; }; #endif /* SINGLETON_HPP_ */