Singleton<> improvements.

Allow Singleton<>::instance to be inlined
and add a new warning for accesses to the
singleton while its being initialized.
This commit is contained in:
Aleric Inglewood
2012-02-01 03:27:04 +01:00
parent a4e05eea9d
commit 77a21f95e5

View File

@@ -176,25 +176,11 @@ public:
{ {
SingletonInstanceData& data = getData(); SingletonInstanceData& data = getData();
if (data.mInitState == CONSTRUCTING) if (data.mInitState != INITIALIZED)
{ {
llerrs << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from singleton constructor!" << llendl; createInstance(data);
} }
if (data.mInitState == DELETED)
{
llwarns << "Trying to access deleted singleton " << typeid(DERIVED_TYPE).name() << " creating new instance" << llendl;
}
if (!data.mSingletonInstance)
{
data.mInitState = CONSTRUCTING;
data.mSingletonInstance = new DERIVED_TYPE();
data.mInitState = INITIALIZING;
data.mSingletonInstance->initSingleton();
data.mInitState = INITIALIZED;
}
return data.mSingletonInstance; return data.mSingletonInstance;
} }
@@ -220,7 +206,35 @@ public:
} }
private: private:
static void createInstance(SingletonInstanceData& data);
virtual void initSingleton() {} virtual void initSingleton() {}
}; };
// Moved this here cause it's too big to be inlined --Aleric.
template<typename DERIVED_TYPE>
void LLSingleton<DERIVED_TYPE>::createInstance(SingletonInstanceData& data)
{
if (data.mInitState == CONSTRUCTING)
{
llerrs << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from singleton constructor!" << llendl;
}
if (data.mInitState == DELETED)
{
llwarns << "Trying to access deleted singleton " << typeid(DERIVED_TYPE).name() << " creating new instance" << llendl;
}
if (data.mInitState == INITIALIZING)
{
llwarns << "Tried to access singleton " << typeid(DERIVED_TYPE).name() << " from initSingleton(), using half-initialized object" << llendl;
return;
}
data.mInitState = CONSTRUCTING;
data.mSingletonInstance = new DERIVED_TYPE();
data.mInitState = INITIALIZING;
data.mSingletonInstance->initSingleton();
data.mInitState = INITIALIZED;
}
#endif #endif