From: <sv...@ww...> - 2005-07-22 07:11:33
|
Author: mkrose Date: 2005-07-22 00:11:07 -0700 (Fri, 22 Jul 2005) New Revision: 1600 Modified: trunk/CSP/SimData/Include/SimData/Singleton.h Log: Use lazy initialization for singletons rather than relying on the (ill-defined) order of static initialization. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1600 Modified: trunk/CSP/SimData/Include/SimData/Singleton.h =================================================================== --- trunk/CSP/SimData/Include/SimData/Singleton.h 2005-07-21 05:34:51 UTC (rev 1599) +++ trunk/CSP/SimData/Include/SimData/Singleton.h 2005-07-22 07:11:07 UTC (rev 1600) @@ -43,12 +43,17 @@ /** Get the one instance of the template class. */ static C& getInstance() { - static C __instance; - return __instance; + static C *__instance = 0; + if (!__instance) __instance = new C(); + return *__instance; } + + /** Get the one instance of the template class as a const reference. + */ static C const & getConstInstance() { return getInstance(); } + protected: Singleton() {} ~Singleton() {} |