Cockatrice/libcockatrice_card/libcockatrice/card/lazy_properties_hash.cpp
RickyRister a0e76607b5
Some checks are pending
Build Desktop / Configure (push) Waiting to run
Build Desktop / Debian 13 (push) Blocked by required conditions
Build Desktop / Debian 12 (push) Blocked by required conditions
Build Desktop / Fedora 44 (push) Blocked by required conditions
Build Desktop / Fedora 43 (push) Blocked by required conditions
Build Desktop / Servatrice_Debian 12 (push) Blocked by required conditions
Build Desktop / Ubuntu 26.04 (push) Blocked by required conditions
Build Desktop / Ubuntu 24.04 (push) Blocked by required conditions
Build Desktop / Arch (push) Blocked by required conditions
Build Desktop / macOS 14 (push) Blocked by required conditions
Build Desktop / macOS 15 (push) Blocked by required conditions
Build Desktop / macOS 13 Intel (push) Blocked by required conditions
Build Desktop / macOS 15 Debug (push) Blocked by required conditions
Build Desktop / Windows 10 (push) Blocked by required conditions
Build Docker Image / amd64 & arm64 (push) Waiting to run
[CardInfo] Encapsulate lazy properties loading into new class (#7088)
* [CardInfo] Encapsulate lazy properties loading into new class

* check if new value was inserted
2026-08-10 22:43:21 -07:00

95 lines
2.1 KiB
C++

#include "lazy_properties_hash.h"
#include <QIODevice>
LazyPropertiesHash::LazyPropertiesHash() : isMaterialized(true)
{
}
LazyPropertiesHash::LazyPropertiesHash(const QByteArray &blob) : blob(blob)
{
}
LazyPropertiesHash::LazyPropertiesHash(const QHash<QString, QString> &properties)
: properties(properties), isMaterialized(true)
{
}
LazyPropertiesHash::LazyPropertiesHash(const LazyPropertiesHash &other)
{
// since we do not allow dematerialization, we only need to lock if not materialized yet
if (other.isMaterialized) {
blob = other.blob;
properties = other.properties;
isMaterialized = true;
} else {
QMutexLocker lock(&other.propertiesMutex);
blob = other.blob;
properties = other.properties;
isMaterialized = false;
}
}
LazyPropertiesHash &LazyPropertiesHash::operator=(const LazyPropertiesHash &other)
{
if (this == &other) {
return *this;
}
// since we do not allow dematerialization, we only need to lock if not materialized yet
if (other.isMaterialized) {
blob = other.blob;
properties = other.properties;
isMaterialized = true;
} else {
QMutexLocker lock(&other.propertiesMutex);
blob = other.blob;
properties = other.properties;
isMaterialized = false;
}
return *this;
}
void LazyPropertiesHash::ensureMaterialized() const
{
QMutexLocker lock(&propertiesMutex);
if (isMaterialized) {
return;
}
if (!blob.isEmpty()) {
QDataStream in(blob);
in.setVersion(QDataStream::Qt_6_4);
in >> properties;
}
blob.clear();
isMaterialized = true;
}
QString LazyPropertiesHash::value(const QString &key) const
{
ensureMaterialized();
return properties.value(key);
}
bool LazyPropertiesHash::insert(const QString &key, const QString &value)
{
ensureMaterialized();
if (value == properties.value(key)) {
return false;
}
properties.insert(key, value);
return true;
}
const QHash<QString, QString> &LazyPropertiesHash::getProperties() const
{
ensureMaterialized();
return properties;
}