2017-06-07 10:09:24 +02:00
|
|
|
#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>
|
2017-01-29 19:14:12 +01:00
|
|
|
#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:
|
2018-03-31 13:44:15 +02:00
|
|
|
ConcurrentMap() = default;
|
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
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutexLocker lock(&this->mutex_);
|
2017-01-29 19:14:12 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
auto a = this->data_.find(name);
|
|
|
|
if (a == this->data_.end()) {
|
2017-01-04 15:12:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-29 19:14:12 +01:00
|
|
|
|
2017-01-04 15:12:31 +01:00
|
|
|
value = a.value();
|
2017-01-29 19:14:12 +01:00
|
|
|
|
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
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutexLocker lock(&this->mutex_);
|
2017-01-26 09:26:18 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
auto a = this->data_.find(name);
|
|
|
|
if (a == this->data_.end()) {
|
2017-01-29 16:18:06 +01:00
|
|
|
TValue value = addLambda();
|
2018-07-06 19:23:47 +02:00
|
|
|
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-26 09:26:18 +01:00
|
|
|
|
2017-01-29 16:18:06 +01:00
|
|
|
return a.value();
|
2017-01-04 15:12:31 +01:00
|
|
|
}
|
|
|
|
|
2017-07-09 17:58:59 +02:00
|
|
|
TValue &operator[](const TKey &name)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutexLocker lock(&this->mutex_);
|
2017-07-09 17:58:59 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->data_[name];
|
2017-07-09 17:58:59 +02:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void clear()
|
2017-01-11 18:52:09 +01:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutexLocker lock(&this->mutex_);
|
2017-01-29 19:14:12 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
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
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutexLocker lock(&this->mutex_);
|
2017-01-29 19:14:12 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->data_.insert(name, value);
|
2017-01-04 15:12:31 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
void each(std::function<void(const TKey &name, const TValue &value)> func) const
|
2017-09-15 17:23:49 +02:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutexLocker lock(&this->mutex_);
|
2017-09-15 17:23:49 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
QMapIterator<TKey, TValue> it(this->data_);
|
2017-09-15 17:23:49 +02:00
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
func(it.key(), it.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 01:28:42 +02:00
|
|
|
void each(std::function<void(const TKey &name, TValue &value)> func)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutexLocker lock(&this->mutex_);
|
2018-06-06 01:28:42 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
QMutableMapIterator<TKey, TValue> it(this->data_);
|
2018-06-06 01:28:42 +02:00
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
func(it.key(), it.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 15:12:31 +01:00
|
|
|
private:
|
2018-07-06 19:23:47 +02:00
|
|
|
mutable QMutex mutex_;
|
|
|
|
QMap<TKey, TValue> data_;
|
2017-01-04 15:12:31 +01:00
|
|
|
};
|
2018-03-31 13:44:15 +02:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|