2017-01-04 15:12:31 +01:00
|
|
|
#ifndef CONCURRENTMAP_H
|
|
|
|
#define CONCURRENTMAP_H
|
|
|
|
|
2017-01-06 23:28:48 +01:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QMap>
|
|
|
|
#include <functional>
|
2017-01-04 15:12:31 +01:00
|
|
|
|
|
|
|
template<typename TKey, typename TValue>
|
|
|
|
class ConcurrentMap
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConcurrentMap() {
|
|
|
|
mutex = new QMutex();
|
|
|
|
map = new QMap<TKey, TValue>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tryGet(const TKey &name, TValue& value) {
|
|
|
|
mutex->lock();
|
|
|
|
auto a = map->find(name);
|
|
|
|
if (a == map->end()) {
|
|
|
|
mutex->unlock();
|
|
|
|
value = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mutex->unlock();
|
|
|
|
value = a.value();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-05 16:07:20 +01:00
|
|
|
TValue getOrAdd(const TKey &name, std::function<TValue ()> addLambda) {
|
2017-01-04 15:12:31 +01:00
|
|
|
mutex->lock();
|
|
|
|
auto a = map->find(name);
|
|
|
|
if (a == map->end()) {
|
|
|
|
TValue value = addLambda();
|
|
|
|
map->insert(name, value);
|
|
|
|
mutex->unlock();
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
mutex->unlock();
|
|
|
|
return a.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
mutex->lock();
|
|
|
|
map->clear();
|
|
|
|
mutex->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(const TKey &name, const TValue &value) {
|
|
|
|
mutex->lock();
|
|
|
|
map->insert(name, value);
|
|
|
|
mutex->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QMutex* mutex;
|
|
|
|
QMap<TKey, TValue>* map;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CONCURRENTMAP_H
|