From 02292e75a25dce7a7291d6ae51919d3fce0ac5da Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sat, 8 Dec 2018 15:35:32 +0100 Subject: [PATCH] Added initialized() function. --- include/MUtils/Lazy.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/MUtils/Lazy.h b/include/MUtils/Lazy.h index ed21e2d..8f2f631 100644 --- a/include/MUtils/Lazy.h +++ b/include/MUtils/Lazy.h @@ -62,6 +62,11 @@ namespace MUtils return getValue(); } + bool initialized() + { + return (m_state > 1); + } + ~Lazy(void) { if(T *const value = m_value) @@ -76,14 +81,15 @@ namespace MUtils T *value; while (!(value = m_value)) { - if (m_status.testAndSetOrdered(0, 1)) + if (m_state.testAndSetOrdered(0, 1)) { if (value = m_initializer()) { m_value.fetchAndStoreOrdered(value); + m_state.fetchAndStoreOrdered(2); break; /*success*/ } - m_status.fetchAndStoreOrdered(0); + m_state.fetchAndStoreOrdered(0); MUTILS_THROW("Initializer returned NULL pointer!"); } QThread::yieldCurrentThread(); @@ -93,7 +99,7 @@ namespace MUtils private: QAtomicPointer m_value; - QAtomicInt m_status; + QAtomicInt m_state; const std::function m_initializer; }; }