summaryrefslogtreecommitdiffstats
path: root/libraries/logic/RWStorage.h
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2016-04-10 04:29:29 +0200
committerPetr Mrázek <peterix@gmail.com>2016-04-30 23:59:23 +0200
commit47e37635f50c09b4f9a9ee7699e3120bab3e4088 (patch)
tree061c2f675fb7e244ebe4b54ef206bfbd615c91f8 /libraries/logic/RWStorage.h
parentfcd4a482f759cd58ee319a51082d0146b7e426e2 (diff)
downloadMultiMC-47e37635f50c09b4f9a9ee7699e3120bab3e4088.tar
MultiMC-47e37635f50c09b4f9a9ee7699e3120bab3e4088.tar.gz
MultiMC-47e37635f50c09b4f9a9ee7699e3120bab3e4088.tar.lz
MultiMC-47e37635f50c09b4f9a9ee7699e3120bab3e4088.tar.xz
MultiMC-47e37635f50c09b4f9a9ee7699e3120bab3e4088.zip
NOISSUE split GUI stuff from logic library
Diffstat (limited to 'libraries/logic/RWStorage.h')
-rw-r--r--libraries/logic/RWStorage.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/libraries/logic/RWStorage.h b/libraries/logic/RWStorage.h
new file mode 100644
index 00000000..b1598ca4
--- /dev/null
+++ b/libraries/logic/RWStorage.h
@@ -0,0 +1,60 @@
+#pragma once
+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)
+ {
+ QReadLocker l(&lock);
+ if(cache.contains(key))
+ {
+ stale_entries.insert(key);
+ }
+ }
+ void clear()
+ {
+ QWriteLocker l(&lock);
+ cache.clear();
+ }
+private:
+ QReadWriteLock lock;
+ QMap<K, V> cache;
+ QSet<K> stale_entries;
+}; \ No newline at end of file