#pragma once #include #include #include #include "util/assertinguithread.hpp" namespace chatterino { namespace util { template class ReadOnlySignalVector { public: virtual ~ReadOnlySignalVector() = default; struct ItemInsertedArgs { const TVectorItem &item; int index; }; pajlada::Signals::Signal itemInserted; pajlada::Signals::Signal itemRemoved; pajlada::Signals::NoArgSignal delayedItemsChanged; const std::vector &getVector() const { util::assertInGuiThread(); return this->vector; } protected: std::vector vector; }; template class BaseSignalVector : public ReadOnlySignalVector { public: void removeItem(int index) { util::assertInGuiThread(); assert(index >= 0 && index < this->vector.size()); this->vector.erase(this->vector.begin() + index); this->itemRemoved.invoke(index); } }; template class SignalVector2 : public BaseSignalVector { public: void insertItem(const TVectorItem &item, int index) { util::assertInGuiThread(); assert(index >= 0 && index <= this->vector.size()); this->vector.insert(this->vector.begin() + index, item); ItemInsertedArgs args{item, index}; this->itemInserted.invoke(args); } void appendItem(const TVectorItem &item) { this->insertItem(item, this->vector.size()); } }; template class SortedSignalVector : public BaseSignalVector { public: void addItem(const TVectorItem &item) { util::assertInGuiThread(); int index = this->vector.insert( std::lower_bound(this->vector.begin(), this->vector.end(), item), item) - this->vector.begin(); ItemInsertedArgs args{item, index}; this->itemInserted.invoke(args); } }; } // namespace util } // namespace chatterino