mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-07 02:35:33 +08:00
refs #601 Fixed thread safety of get() by adding getCopy().
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -292,9 +292,12 @@ namespace BlackMisc
|
||||
{}
|
||||
|
||||
//! Read the current value.
|
||||
//! \threadsafe
|
||||
const T &get() const { static const T empty {}; return *(isValid() ? static_cast<const T *>(getVariant().data()) : &empty); }
|
||||
|
||||
//! Get a copy of the current value.
|
||||
//! \threadsafe
|
||||
T getCopy() const { return isValid() ? getVariantCopy().template value<T>() : 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<Private::CValuePage::NotifySlot>(slot); }
|
||||
|
||||
const QVariant &getVariant() const { return m_page.getValue(m_element).getQVariant(); }
|
||||
bool isValid() const { return getVariant().isValid() && getVariant().userType() == qMetaTypeId<T>(); }
|
||||
QVariant getVariantCopy() const { return m_page.getValueCopy(m_element).getQVariant(); }
|
||||
bool isValid() const { return m_page.isValid(m_element, qMetaTypeId<T>()); }
|
||||
|
||||
Private::CValuePage &m_page;
|
||||
Private::CValuePage::Element &m_element;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user