// TJP COPYRIGHT HEADER /** * legal header * * ============================================================================ * TJP 10/11/01 Got rid of the constructed singleton, it was essentially * a pointer! * * @author Timothy Prepscius * @created 3/../01 */ #pragma once #include "ThreadSafeSingleton.h" #include "../threads/Monitor.h" namespace timprepscius { namespace core { /** * a Singleton container, whose contained instance is constructed on its * first request * * T defines the class pointer type * X defines the allocation class type */ template class ThreadSafeSingletonNoConstruct : public Monitor { public: typedef Monitor Super; typedef typename Super::Writer Accessor; protected: // only called by reader/writer after lock virtual T *access() const { return this->object; } public: ThreadSafeSingletonNoConstruct () : Super(NULL) { } virtual ~ThreadSafeSingletonNoConstruct() { deleteInstance(); } virtual void setInstance (T *instance) { Accessor w(*this); { this->object = instance; } } void deleteInstance() { Accessor w(*this); if (this->object) delete this->object; this->object = NULL; } } ; template class ThreadSafeSingleton : public ThreadSafeSingletonNoConstruct { public: typedef ThreadSafeSingletonNoConstruct Super; typedef typename Super::Writer Accessor; protected: // only called by reader/writer after lock virtual T *access() const { if (!this->object) { const_cast *>(this)->setInstance(new T()); } return this->object; } public: virtual ~ThreadSafeSingleton() { Super::deleteInstance(); } virtual void setInstance (T *instance) { Accessor a(*this); if (this->object) delete this->object; this->object = instance; } } ; } // namespace utilities } // namespace