diff --git a/src/blackmisc/valuecache.cpp b/src/blackmisc/valuecache.cpp index c9adbeec3..bc814c5f9 100644 --- a/src/blackmisc/valuecache.cpp +++ b/src/blackmisc/valuecache.cpp @@ -422,7 +422,20 @@ namespace BlackMisc return element; } + bool CValuePage::isValid(const Element &element, int typeId) const + { + auto reader = element.m_value.read(); + return reader->isValid() && reader->userType() == typeId; + } + const CVariant &CValuePage::getValue(const Element &element) const + { + Q_ASSERT(QThread::currentThread() == thread()); + + return element.m_value.read(); + } + + CVariant CValuePage::getValueCopy(const Element &element) const { return element.m_value.read(); } diff --git a/src/blackmisc/valuecache.h b/src/blackmisc/valuecache.h index 7fe4cf64a..a19a4c2cd 100644 --- a/src/blackmisc/valuecache.h +++ b/src/blackmisc/valuecache.h @@ -292,9 +292,12 @@ namespace BlackMisc {} //! Read the current value. - //! \threadsafe const T &get() const { static const T empty {}; return *(isValid() ? static_cast(getVariant().data()) : &empty); } + //! Get a copy of the current value. + //! \threadsafe + T getCopy() const { return isValid() ? getVariantCopy().template value() : T{}; } + //! Write a new value. Must be called from the thread in which the owner lives. CStatusMessage set(const T &value, qint64 timestamp = 0) { return m_page.setValue(m_element, CVariant::from(value), timestamp); } @@ -328,7 +331,8 @@ namespace BlackMisc static Private::CValuePage::NotifySlot slot_cast(F slot) { return static_cast(slot); } const QVariant &getVariant() const { return m_page.getValue(m_element).getQVariant(); } - bool isValid() const { return getVariant().isValid() && getVariant().userType() == qMetaTypeId(); } + QVariant getVariantCopy() const { return m_page.getValueCopy(m_element).getQVariant(); } + bool isValid() const { return m_page.isValid(m_element, qMetaTypeId()); } Private::CValuePage &m_page; Private::CValuePage::Element &m_element; diff --git a/src/blackmisc/valuecacheprivate.h b/src/blackmisc/valuecacheprivate.h index 3856b7bcb..918896435 100644 --- a/src/blackmisc/valuecacheprivate.h +++ b/src/blackmisc/valuecacheprivate.h @@ -57,9 +57,16 @@ namespace BlackMisc //! \param slot Optional member function of parent, will be called when value changes. Element &createElement(const QString &key, int metaType, Validator validator, const CVariant &defaultValue, NotifySlot slot); + //! True if the currently paged value is a valid instance of the given type. + //! \threadsafe + bool isValid(const Element &element, int typeId) const; + + //! Read the currently paged value corresponding to the element's key. + const CVariant &getValue(const Element &element) const; + //! Read the currently paged value corresponding to the element's key. //! \threadsafe - const CVariant &getValue(const Element &element) const; + CVariant getValueCopy(const Element &element) const; //! Write the value corresponding to the element's key and begin synchronizing it to any other pages. CStatusMessage setValue(Element &element, const CVariant &value, qint64 timestamp, bool save = false);