mirror-chatterino2/src/concurrentmap.hpp

148 lines
2.7 KiB
C++
Raw Normal View History

#pragma once
2017-01-04 15:12:31 +01:00
2017-01-06 23:28:48 +01:00
#include <QMap>
2017-01-11 18:52:09 +01:00
#include <QMutex>
#include <QMutexLocker>
2017-01-29 19:00:26 +01:00
2017-01-06 23:28:48 +01:00
#include <functional>
2017-06-15 23:13:01 +02:00
#include <map>
2017-08-13 14:52:31 +02:00
#include <memory>
2017-01-04 15:12:31 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
2017-01-11 18:52:09 +01:00
template <typename TKey, typename TValue>
2017-01-04 15:12:31 +01:00
class ConcurrentMap
{
public:
2017-01-11 18:52:09 +01:00
ConcurrentMap()
{
2017-01-04 15:12:31 +01:00
}
2017-04-12 17:46:44 +02:00
bool tryGet(const TKey &name, TValue &value) const
2017-01-11 18:52:09 +01:00
{
2017-08-13 14:52:31 +02:00
QMutexLocker lock(this->mutex.get());
auto a = this->data.find(name);
if (a == this->data.end()) {
2017-01-04 15:12:31 +01:00
return false;
}
2017-01-04 15:12:31 +01:00
value = a.value();
2017-01-04 15:12:31 +01:00
return true;
}
2017-04-12 17:46:44 +02:00
TValue getOrAdd(const TKey &name, std::function<TValue()> addLambda)
2017-01-11 18:52:09 +01:00
{
2017-08-13 14:52:31 +02:00
QMutexLocker lock(this->mutex.get());
auto a = this->data.find(name);
if (a == this->data.end()) {
2017-01-29 16:18:06 +01:00
TValue value = addLambda();
this->data.insert(name, value);
2017-01-29 16:18:06 +01:00
return value;
2017-01-04 15:12:31 +01:00
}
2017-01-29 16:18:06 +01:00
return a.value();
2017-01-04 15:12:31 +01:00
}
TValue &operator[](const TKey &name)
{
2017-08-13 14:52:31 +02:00
QMutexLocker lock(this->mutex.get());
return this->data[name];
}
ConcurrentMap(const ConcurrentMap &o)
2017-08-13 14:52:31 +02:00
: mutex(std::move(o.mutex))
, data(std::move(o.data))
{
}
ConcurrentMap &operator=(const ConcurrentMap &rhs)
{
2017-08-13 14:52:31 +02:00
this->mutex = std::move(rhs.mutex);
this->data = std::move(rhs.data);
return *this;
}
2017-04-12 17:46:44 +02:00
void clear()
2017-01-11 18:52:09 +01:00
{
2017-08-13 14:52:31 +02:00
QMutexLocker lock(this->mutex.get());
this->data.clear();
2017-01-04 15:12:31 +01:00
}
2017-04-12 17:46:44 +02:00
void insert(const TKey &name, const TValue &value)
2017-01-11 18:52:09 +01:00
{
2017-08-13 14:52:31 +02:00
QMutexLocker lock(this->mutex.get());
this->data.insert(name, value);
2017-01-04 15:12:31 +01:00
}
private:
2017-08-13 14:52:31 +02:00
mutable std::unique_ptr<QMutex> mutex;
QMap<TKey, TValue> data;
2017-01-04 15:12:31 +01:00
};
2017-06-15 23:13:01 +02:00
template <typename TKey, typename TValue>
class ConcurrentStdMap
{
public:
bool tryGet(const TKey &name, TValue &value) const
{
QMutexLocker lock(&_mutex);
auto a = _map.find(name);
if (a == _map.end()) {
return false;
}
value = a.value();
return true;
}
TValue getOrAdd(const TKey &name, std::function<TValue()> addLambda)
{
QMutexLocker lock(&_mutex);
auto a = _map.find(name);
if (a == _map.end()) {
TValue value = addLambda();
_map.insert(name, value);
return value;
}
return a.value();
}
TValue &operator[](const TKey &name)
{
QMutexLocker lock(&_mutex);
return this->_map[name];
}
2017-06-15 23:13:01 +02:00
void clear()
{
QMutexLocker lock(&_mutex);
_map.clear();
}
void insert(const TKey &name, const TValue &value)
{
QMutexLocker lock(&_mutex);
_map.insert(name, value);
}
private:
mutable QMutex _mutex;
std::map<TKey, TValue> _map;
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino