2014-07-06 10:15:15 +01:00
|
|
|
#pragma once
|
2018-04-01 19:24:28 +01:00
|
|
|
#include <QWriteLocker>
|
|
|
|
#include <QReadLocker>
|
2021-03-26 00:47:19 +00:00
|
|
|
#include <QMap>
|
|
|
|
#include <QSet>
|
|
|
|
|
2014-07-06 10:15:15 +01:00
|
|
|
template <typename K, typename V>
|
|
|
|
class RWStorage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
return cache[key];
|
|
|
|
}
|
|
|
|
else return V();
|
|
|
|
}
|
|
|
|
bool get(K key, V& value)
|
|
|
|
{
|
|
|
|
QReadLocker l(&lock);
|
|
|
|
if(cache.contains(key))
|
|
|
|
{
|
|
|
|
value = cache[key];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
bool has(K key)
|
|
|
|
{
|
|
|
|
QReadLocker l(&lock);
|
|
|
|
return cache.contains(key);
|
|
|
|
}
|
|
|
|
bool stale(K key)
|
|
|
|
{
|
|
|
|
QReadLocker l(&lock);
|
|
|
|
if(!cache.contains(key))
|
|
|
|
return true;
|
|
|
|
return stale_entries.contains(key);
|
|
|
|
}
|
|
|
|
void setStale(K key)
|
|
|
|
{
|
2018-04-02 21:58:54 +01:00
|
|
|
QWriteLocker l(&lock);
|
2014-07-06 10:15:15 +01:00
|
|
|
if(cache.contains(key))
|
|
|
|
{
|
|
|
|
stale_entries.insert(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
QWriteLocker l(&lock);
|
|
|
|
cache.clear();
|
2018-04-02 21:58:54 +01:00
|
|
|
stale_entries.clear();
|
2014-07-06 10:15:15 +01:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
QReadWriteLock lock;
|
|
|
|
QMap<K, V> cache;
|
|
|
|
QSet<K> stale_entries;
|
2018-04-02 21:58:54 +01:00
|
|
|
};
|