2018-06-28 20:25:37 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <pajlada/signals/signal.hpp>
|
2019-07-31 22:29:07 +02:00
|
|
|
#include <shared_mutex>
|
2018-06-28 20:25:37 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "debug/AssertInGuiThread.hpp"
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
|
|
|
struct SignalVectorItemArgs {
|
|
|
|
const TVectorItem &item;
|
|
|
|
int index;
|
|
|
|
void *caller;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
|
|
|
class ReadOnlySignalVector : boost::noncopyable
|
|
|
|
{
|
2019-07-31 22:29:07 +02:00
|
|
|
using VecIt = typename std::vector<TVectorItem>::iterator;
|
|
|
|
|
2018-06-28 20:25:37 +02:00
|
|
|
public:
|
2019-07-31 22:29:07 +02:00
|
|
|
struct Iterator
|
|
|
|
: public std::iterator<std::input_iterator_tag, TVectorItem> {
|
|
|
|
Iterator(VecIt &&it, std::shared_mutex &mutex)
|
|
|
|
: it_(std::move(it))
|
|
|
|
, lock_(mutex)
|
|
|
|
, mutex_(mutex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator(const Iterator &other)
|
|
|
|
: it_(other.it_)
|
|
|
|
, lock_(other.mutex_)
|
|
|
|
, mutex_(other.mutex_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-01 13:30:58 +02:00
|
|
|
Iterator &operator=(const Iterator &other)
|
|
|
|
{
|
|
|
|
this->lock_ = std::shared_lock(other.mutex_.get());
|
|
|
|
this->mutex_ = other.mutex_;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:29:07 +02:00
|
|
|
TVectorItem &operator*()
|
|
|
|
{
|
|
|
|
return it_.operator*();
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator &operator++()
|
|
|
|
{
|
|
|
|
++this->it_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Iterator &other)
|
|
|
|
{
|
|
|
|
return this->it_ == other.it_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Iterator &other)
|
|
|
|
{
|
|
|
|
return this->it_ != other.it_;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator-(const Iterator &other)
|
|
|
|
{
|
|
|
|
return this->it_ - other.it_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
VecIt it_;
|
|
|
|
std::shared_lock<std::shared_mutex> lock_;
|
2019-08-01 13:30:58 +02:00
|
|
|
std::reference_wrapper<std::shared_mutex> mutex_;
|
2019-07-31 22:29:07 +02:00
|
|
|
};
|
|
|
|
|
2018-06-28 20:25:37 +02:00
|
|
|
ReadOnlySignalVector()
|
|
|
|
{
|
2018-07-06 18:10:21 +02:00
|
|
|
QObject::connect(&this->itemsChangedTimer_, &QTimer::timeout,
|
2018-06-28 20:25:37 +02:00
|
|
|
[this] { this->delayedItemsChanged.invoke(); });
|
2018-07-06 18:10:21 +02:00
|
|
|
this->itemsChangedTimer_.setInterval(100);
|
|
|
|
this->itemsChangedTimer_.setSingleShot(true);
|
2018-06-28 20:25:37 +02:00
|
|
|
}
|
|
|
|
virtual ~ReadOnlySignalVector() = default;
|
|
|
|
|
|
|
|
pajlada::Signals::Signal<SignalVectorItemArgs<TVectorItem>> itemInserted;
|
|
|
|
pajlada::Signals::Signal<SignalVectorItemArgs<TVectorItem>> itemRemoved;
|
|
|
|
pajlada::Signals::NoArgSignal delayedItemsChanged;
|
|
|
|
|
2019-07-31 22:29:07 +02:00
|
|
|
Iterator begin() const
|
|
|
|
{
|
|
|
|
return Iterator(
|
|
|
|
const_cast<std::vector<TVectorItem> &>(this->vector_).begin(),
|
|
|
|
this->mutex_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator end() const
|
|
|
|
{
|
|
|
|
return Iterator(
|
|
|
|
const_cast<std::vector<TVectorItem> &>(this->vector_).end(),
|
|
|
|
this->mutex_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
std::shared_lock lock(this->mutex_);
|
|
|
|
|
|
|
|
return this->vector_.empty();
|
|
|
|
}
|
|
|
|
|
2018-06-28 20:25:37 +02:00
|
|
|
const std::vector<TVectorItem> &getVector() const
|
|
|
|
{
|
|
|
|
assertInGuiThread();
|
|
|
|
|
2018-07-06 18:10:21 +02:00
|
|
|
return this->vector_;
|
2018-06-28 20:25:37 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 19:53:29 +02:00
|
|
|
std::vector<TVectorItem> cloneVector() const
|
2019-08-01 13:30:58 +02:00
|
|
|
{
|
|
|
|
std::shared_lock lock(this->mutex_);
|
|
|
|
|
|
|
|
return this->vector_;
|
|
|
|
}
|
|
|
|
|
2018-06-28 20:25:37 +02:00
|
|
|
void invokeDelayedItemsChanged()
|
|
|
|
{
|
|
|
|
assertInGuiThread();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!this->itemsChangedTimer_.isActive())
|
|
|
|
{
|
2018-07-06 18:10:21 +02:00
|
|
|
this->itemsChangedTimer_.start();
|
2018-06-28 20:25:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:06:23 +02:00
|
|
|
virtual bool isSorted() const = 0;
|
|
|
|
|
2018-06-28 20:25:37 +02:00
|
|
|
protected:
|
2018-07-06 18:10:21 +02:00
|
|
|
std::vector<TVectorItem> vector_;
|
|
|
|
QTimer itemsChangedTimer_;
|
2019-07-31 22:29:07 +02:00
|
|
|
mutable std::shared_mutex mutex_;
|
2018-06-28 20:25:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
|
|
|
class BaseSignalVector : public ReadOnlySignalVector<TVectorItem>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// returns the actual index of the inserted item
|
2018-07-03 15:08:55 +02:00
|
|
|
virtual int insertItem(const TVectorItem &item, int proposedIndex = -1,
|
|
|
|
void *caller = nullptr) = 0;
|
2018-06-28 20:25:37 +02:00
|
|
|
|
2018-07-03 15:08:55 +02:00
|
|
|
void removeItem(int index, void *caller = nullptr)
|
2018-06-28 20:25:37 +02:00
|
|
|
{
|
|
|
|
assertInGuiThread();
|
2019-07-31 22:29:07 +02:00
|
|
|
std::unique_lock lock(this->mutex_);
|
|
|
|
|
|
|
|
assert(index >= 0 && index < int(this->vector_.size()));
|
2018-06-28 20:25:37 +02:00
|
|
|
|
2018-07-06 18:10:21 +02:00
|
|
|
TVectorItem item = this->vector_[index];
|
2019-07-31 22:29:07 +02:00
|
|
|
|
2018-07-06 18:10:21 +02:00
|
|
|
this->vector_.erase(this->vector_.begin() + index);
|
2019-07-31 22:29:07 +02:00
|
|
|
lock.unlock(); // manual unlock
|
|
|
|
|
2018-06-28 20:25:37 +02:00
|
|
|
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
|
|
|
|
this->itemRemoved.invoke(args);
|
|
|
|
|
|
|
|
this->invokeDelayedItemsChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:08:55 +02:00
|
|
|
int appendItem(const TVectorItem &item, void *caller = nullptr)
|
2018-06-28 20:25:37 +02:00
|
|
|
{
|
|
|
|
return this->insertItem(item, -1, caller);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TVectorItem>
|
|
|
|
class UnsortedSignalVector : public BaseSignalVector<TVectorItem>
|
|
|
|
{
|
|
|
|
public:
|
2018-08-06 21:17:03 +02:00
|
|
|
virtual int insertItem(const TVectorItem &item, int index = -1,
|
|
|
|
void *caller = nullptr) override
|
2018-06-28 20:25:37 +02:00
|
|
|
{
|
|
|
|
assertInGuiThread();
|
2019-07-31 22:29:07 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2019-07-31 22:29:07 +02:00
|
|
|
std::unique_lock lock(this->mutex_);
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
index = this->vector_.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(index >= 0 && index <= this->vector_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
this->vector_.insert(this->vector_.begin() + index, item);
|
2018-06-28 20:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
|
|
|
|
this->itemInserted.invoke(args);
|
|
|
|
this->invokeDelayedItemsChanged();
|
|
|
|
return index;
|
|
|
|
}
|
2018-07-03 15:06:23 +02:00
|
|
|
|
|
|
|
virtual bool isSorted() const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-28 20:25:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TVectorItem, typename Compare>
|
|
|
|
class SortedSignalVector : public BaseSignalVector<TVectorItem>
|
|
|
|
{
|
|
|
|
public:
|
2018-08-06 21:17:03 +02:00
|
|
|
virtual int insertItem(const TVectorItem &item, int = -1,
|
|
|
|
void *caller = nullptr) override
|
2018-06-28 20:25:37 +02:00
|
|
|
{
|
|
|
|
assertInGuiThread();
|
2019-07-31 22:29:07 +02:00
|
|
|
int index = -1;
|
2018-06-28 20:25:37 +02:00
|
|
|
|
2019-07-31 22:29:07 +02:00
|
|
|
{
|
|
|
|
std::unique_lock lock(this->mutex_);
|
|
|
|
|
|
|
|
auto it = std::lower_bound(this->vector_.begin(),
|
|
|
|
this->vector_.end(), item, Compare{});
|
|
|
|
index = it - this->vector_.begin();
|
|
|
|
this->vector_.insert(it, item);
|
|
|
|
}
|
2018-06-28 20:25:37 +02:00
|
|
|
|
|
|
|
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
|
|
|
|
this->itemInserted.invoke(args);
|
|
|
|
this->invokeDelayedItemsChanged();
|
|
|
|
return index;
|
|
|
|
}
|
2018-07-03 15:06:23 +02:00
|
|
|
|
|
|
|
virtual bool isSorted() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-28 20:25:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace chatterino
|