diff options
author | Palmer <palmer@lindenlab.com> | 2009-08-06 14:51:11 -0700 |
---|---|---|
committer | Palmer <palmer@lindenlab.com> | 2009-08-06 14:51:11 -0700 |
commit | b2632c50efc12eacdcadace64e6c0f1906b86ff6 (patch) | |
tree | c32d7e9b29e37d5c30ac16dfac55733456d7eee0 /indra/llcommon/llsingleton.h | |
parent | 27cf39cdbf27fe52dcf9c70cfdadcc18ddf2e75c (diff) | |
parent | a8d216e194327c7bee8a42c983f7f2ca01adb385 (diff) |
Merge of my DEV-36732 work and all the main line login api work that went on.
Diffstat (limited to 'indra/llcommon/llsingleton.h')
-rw-r--r-- | indra/llcommon/llsingleton.h | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/indra/llcommon/llsingleton.h b/indra/llcommon/llsingleton.h index dc1457e4f7..00c87821c2 100644 --- a/indra/llcommon/llsingleton.h +++ b/indra/llcommon/llsingleton.h @@ -33,7 +33,30 @@ #include "llerror.h" // *TODO: eliminate this +#include <typeinfo> #include <boost/noncopyable.hpp> +#include <boost/any.hpp> + +/// @brief A global registry of all singletons to prevent duplicate allocations +/// across shared library boundaries +class LL_COMMON_API LLSingletonRegistry { + private: + typedef std::map<std::string, void *> TypeMap; + static TypeMap sSingletonMap; + + public: + template<typename T> static void * & get() + { + std::string name(typeid(T).name()); + + if(0 == sSingletonMap.count(name)) + { + sSingletonMap[name] = NULL; + } + + return sSingletonMap[typeid(T).name()]; + } +}; // LLSingleton implements the getInstance() method part of the Singleton // pattern. It can't make the derived class constructors protected, though, so @@ -107,8 +130,16 @@ public: static SingletonInstanceData& getData() { + void * & registry = LLSingletonRegistry::get<DERIVED_TYPE>(); static SingletonInstanceData data; - return data; + + // *TODO - look into making this threadsafe + if(NULL == registry) + { + registry = &data; + } + + return *static_cast<SingletonInstanceData *>(registry); } static DERIVED_TYPE* getInstance() @@ -143,7 +174,14 @@ public: { return *getInstance(); } - + + // Has this singleton been created uet? + // Use this to avoid accessing singletons before the can safely be constructed + static bool instanceExists() + { + return getData().mInitState == INITIALIZED; + } + // Has this singleton already been deleted? // Use this to avoid accessing singletons from a static object's destructor static bool destroyed() |