X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=include%2Fthreadwrapper%2Fsingleton.h;fp=include%2Fthreadwrapper%2Fsingleton.h;h=9e2aab5274a11523d6241dfb2c104d8515fb45b8;hb=dec33c63afafabf83c3039e916725cac6faef9b3;hp=0000000000000000000000000000000000000000;hpb=9b22dd53fe62e312c1647310b7ec43aa127090af;p=fms.git diff --git a/include/threadwrapper/singleton.h b/include/threadwrapper/singleton.h new file mode 100644 index 0000000..9e2aab5 --- /dev/null +++ b/include/threadwrapper/singleton.h @@ -0,0 +1,61 @@ +#ifndef _singleton_ +#define _singleton_ + +#include + +#include "noncopyable.h" + +//! SingletonDestroyer +template +class Destroyer +{ + T* doomed; + public: + + Destroyer(T* q) : doomed(q) { assert(doomed); } + ~Destroyer(); +}; + +template +Destroyer::~Destroyer() +{ + try + { + if(doomed) + { + delete doomed; + } + } + catch(...) + { + } + + doomed = 0; +} + +template +class Singleton:public NonCopyable +{ +public: + + static T *Instance(); + +private: + +}; + +template +T *Singleton::Instance() +{ + static T *obj=0; + static Poco::FastMutex mutex; + Poco::ScopedLock g(mutex); + if(obj==0) + { + obj=new T(); + static Destroyer des(obj); + } + return const_cast(obj); +} + +#endif // _singleton_