PrismLauncher/launcher/RWStorage.h

64 lines
1.3 KiB
C
Raw Normal View History

2014-07-06 10:15:15 +01:00
#pragma once
#include <QMap>
#include <QReadLocker>
#include <QSet>
#include <QWriteLocker>
2014-07-06 10:15:15 +01:00
template <typename K, typename V>
class RWStorage {
public:
2018-07-15 13:51:05 +01:00
void add(K key, V value)
{
QWriteLocker l(&lock);
cache[key] = value;
stale_entries.remove(key);
}
V get(K key)
{
QReadLocker l(&lock);
if (cache.contains(key)) {
2018-07-15 13:51:05 +01:00
return cache[key];
} else
return V();
2018-07-15 13:51:05 +01:00
}
bool get(K key, V& value)
{
QReadLocker l(&lock);
if (cache.contains(key)) {
2018-07-15 13:51:05 +01:00
value = cache[key];
return true;
} else
return false;
2018-07-15 13:51:05 +01:00
}
bool has(K key)
{
QReadLocker l(&lock);
return cache.contains(key);
}
bool stale(K key)
{
QReadLocker l(&lock);
if (!cache.contains(key))
2018-07-15 13:51:05 +01:00
return true;
return stale_entries.contains(key);
}
void setStale(K key)
{
QWriteLocker l(&lock);
if (cache.contains(key)) {
2018-07-15 13:51:05 +01:00
stale_entries.insert(key);
}
}
void clear()
{
QWriteLocker l(&lock);
cache.clear();
stale_entries.clear();
}
private:
2018-07-15 13:51:05 +01:00
QReadWriteLock lock;
QMap<K, V> cache;
QSet<K> stale_entries;
2018-04-02 21:58:54 +01:00
};