Added initialized() function.

This commit is contained in:
LoRd_MuldeR 2018-12-08 15:35:32 +01:00
parent 280815ff49
commit 02292e75a2

View File

@ -62,6 +62,11 @@ namespace MUtils
return getValue(); return getValue();
} }
bool initialized()
{
return (m_state > 1);
}
~Lazy(void) ~Lazy(void)
{ {
if(T *const value = m_value) if(T *const value = m_value)
@ -76,14 +81,15 @@ namespace MUtils
T *value; T *value;
while (!(value = m_value)) while (!(value = m_value))
{ {
if (m_status.testAndSetOrdered(0, 1)) if (m_state.testAndSetOrdered(0, 1))
{ {
if (value = m_initializer()) if (value = m_initializer())
{ {
m_value.fetchAndStoreOrdered(value); m_value.fetchAndStoreOrdered(value);
m_state.fetchAndStoreOrdered(2);
break; /*success*/ break; /*success*/
} }
m_status.fetchAndStoreOrdered(0); m_state.fetchAndStoreOrdered(0);
MUTILS_THROW("Initializer returned NULL pointer!"); MUTILS_THROW("Initializer returned NULL pointer!");
} }
QThread::yieldCurrentThread(); QThread::yieldCurrentThread();
@ -93,7 +99,7 @@ namespace MUtils
private: private:
QAtomicPointer<T> m_value; QAtomicPointer<T> m_value;
QAtomicInt m_status; QAtomicInt m_state;
const std::function<T*(void)> m_initializer; const std::function<T*(void)> m_initializer;
}; };
} }