-
Notifications
You must be signed in to change notification settings - Fork 2
/
objectcache.h
51 lines (44 loc) · 1.33 KB
/
objectcache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef OBJECTCACHE_H
#define OBJECTCACHE_H
/**
* @brief A simple "map" that autoremoves destroyed objects. Can hold only pointers to QObject
* subclasses.
*
* While not directly tied to promises, this class is useful in the common use case of sharing a
* single promise instance across multiple function calls, in order to do the processing once and
* then notify all clients with same result.
*
* static ObjectCache<QString, Promise<QJsonDocument>> promises;
* auto promise = promises.value(myId);
* if (promise) return promise;
* else promise = promises.create(myId);
*
*/
template <class Key, class T> class ObjectCache {
public:
/**
* @brief A shared instance for cases when having a dedicated cache is overkill
*/
static ObjectCache &instance() {
static ObjectCache i;
return i;
}
T *value(Key key) {
auto i = cache.constFind(key);
if (i != cache.constEnd()) return i.value();
return nullptr;
}
void remove(Key key) { cache.remove(key); }
void insert(Key key, T *obj) {
obj->connect(obj, &QObject::destroyed, [key, this] { remove(key); });
cache.insert(key, obj);
}
T *create(Key key) {
auto obj = new T();
insert(key, obj);
return obj;
}
private:
QMap<Key, T *> cache;
};
#endif // OBJECTCACHE_H