16 #ifndef RESSOURCEHANDLER_H 17 #define RESSOURCEHANDLER_H 22 #include <QFutureWatcher> 27 #include <QtConcurrent/QtConcurrent> 44 using Key = std::string;
45 using Value = QPixmap;
48 =
typename QMap<Key, QFuture<Value>>::iterator;
49 using future_const_iterator
50 =
typename QMap<Key, QFuture<Value>>::const_iterator;
53 =
typename QMap<Key, Value>::iterator;
55 =
typename QMap<Key, Value>::const_iterator;
66 setBuilder(t_builder);
69 void setBuilder(std::function<Value(Key
const&)>
const &t_builder)
74 Value buildInMainThread(Key
const &key) {
76 if (
auto pos = finalMap.find(key); pos != finalMap.end())
78 finalMap[key] = builder(key);
82 bool isReady()
const {
84 return futureMap.empty();
87 iterator find(Key
const &key) {
88 auto res = finalMap.find(key);
92 const_iterator find(Key
const &key)
const {
93 auto res = finalMap.find(key);
97 future_iterator isFuture(Key
const &key) {
99 auto res = futureMap.find(key);
103 future_const_iterator isFuture(Key
const &key)
const {
105 auto res = futureMap.find(key);
109 void sendKey(Key
const &key) {
110 if (find(key) != finalMap.end())
113 if (isFuture(key) != futureMap.end())
116 QFutureWatcher<Value> *watcher =
new QFutureWatcher<Value>;
117 futureMap[key] = QtConcurrent::run(builder, key);
118 auto endTask = [=]() { emit this->valueReady(key); };
121 &QFutureWatcher<Value>::finished,
124 watcher->setFuture(futureMap[key]);
129 void valueReady(
const std::string&);
133 std::function<Value(Key const&)> builder;
134 QMap<Key, QFuture<Value>> futureMap;
135 QMap<Key, Value> finalMap;
139 #endif // RESSOURCEHANDLER_H Definition: resourcehandler.h:30
Definition: resourcehandler.h:39