renamed files SignalVector -> SimpleSignalVector and SingalVector2 -> SignalVector

This commit is contained in:
fourtf 2018-06-28 20:25:37 +02:00
parent c3c2b934ba
commit 6013591730
16 changed files with 174 additions and 174 deletions

View file

@ -242,8 +242,6 @@ HEADERS += \
src/common/Property.hpp \ src/common/Property.hpp \
src/common/ProviderId.hpp \ src/common/ProviderId.hpp \
src/common/SerializeCustom.hpp \ src/common/SerializeCustom.hpp \
src/common/SignalVector.hpp \
src/common/SignalVector2.hpp \
src/common/SignalVectorModel.hpp \ src/common/SignalVectorModel.hpp \
src/common/UrlFetch.hpp \ src/common/UrlFetch.hpp \
src/common/Version.hpp \ src/common/Version.hpp \
@ -392,7 +390,9 @@ HEADERS += \
src/singletons/Settings.hpp \ src/singletons/Settings.hpp \
src/singletons/Updates.hpp \ src/singletons/Updates.hpp \
src/singletons/NativeMessaging.hpp \ src/singletons/NativeMessaging.hpp \
src/singletons/Theme.hpp src/singletons/Theme.hpp \
src/common/SimpleSignalVector.hpp \
src/common/SignalVector.hpp
RESOURCES += \ RESOURCES += \
resources/resources.qrc \ resources/resources.qrc \

View file

@ -1,34 +1,125 @@
#pragma once #pragma once
#include <pajlada/signals/signal.hpp> #include <QStandardItemModel>
#include <QTimer>
#include <mutex> #include <boost/noncopyable.hpp>
#include <vector> #include <pajlada/signals/signal.hpp>
#include <vector>
namespace chatterino {
#include "debug/AssertInGuiThread.hpp"
template <typename TValue>
class SignalVector namespace chatterino {
{
public: template <typename TVectorItem>
SignalVector &operator=(std::vector<TValue> &other) struct SignalVectorItemArgs {
{ const TVectorItem &item;
this->data = other; int index;
void *caller;
this->updated.invoke(); };
return *this; template <typename TVectorItem>
} class ReadOnlySignalVector : boost::noncopyable
{
operator std::vector<TValue> &() public:
{ ReadOnlySignalVector()
return this->data; {
} QObject::connect(&this->itemsChangedTimer, &QTimer::timeout,
[this] { this->delayedItemsChanged.invoke(); });
pajlada::Signals::NoArgSignal updated; this->itemsChangedTimer.setInterval(100);
this->itemsChangedTimer.setSingleShot(true);
private: }
std::vector<TValue> data; virtual ~ReadOnlySignalVector() = default;
};
pajlada::Signals::Signal<SignalVectorItemArgs<TVectorItem>> itemInserted;
} // namespace chatterino pajlada::Signals::Signal<SignalVectorItemArgs<TVectorItem>> itemRemoved;
pajlada::Signals::NoArgSignal delayedItemsChanged;
const std::vector<TVectorItem> &getVector() const
{
assertInGuiThread();
return this->vector;
}
void invokeDelayedItemsChanged()
{
assertInGuiThread();
if (!this->itemsChangedTimer.isActive()) {
itemsChangedTimer.start();
}
}
protected:
std::vector<TVectorItem> vector;
QTimer itemsChangedTimer;
};
template <typename TVectorItem>
class BaseSignalVector : public ReadOnlySignalVector<TVectorItem>
{
public:
// returns the actual index of the inserted item
virtual int insertItem(const TVectorItem &item, int proposedIndex = -1, void *caller = 0) = 0;
void removeItem(int index, void *caller = 0)
{
assertInGuiThread();
assert(index >= 0 && index < this->vector.size());
TVectorItem item = this->vector[index];
this->vector.erase(this->vector.begin() + index);
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
this->itemRemoved.invoke(args);
this->invokeDelayedItemsChanged();
}
int appendItem(const TVectorItem &item, void *caller = 0)
{
return this->insertItem(item, -1, caller);
}
};
template <typename TVectorItem>
class UnsortedSignalVector : public BaseSignalVector<TVectorItem>
{
public:
virtual int insertItem(const TVectorItem &item, int index = -1, void *caller = 0) override
{
assertInGuiThread();
if (index == -1) {
index = this->vector.size();
} else {
assert(index >= 0 && index <= this->vector.size());
}
this->vector.insert(this->vector.begin() + index, item);
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
this->itemInserted.invoke(args);
this->invokeDelayedItemsChanged();
return index;
}
};
template <typename TVectorItem, typename Compare>
class SortedSignalVector : public BaseSignalVector<TVectorItem>
{
public:
virtual int insertItem(const TVectorItem &item, int = -1, void *caller = nullptr) override
{
assertInGuiThread();
auto it = std::lower_bound(this->vector.begin(), this->vector.end(), item, Compare{});
int index = it - this->vector.begin();
this->vector.insert(it, item);
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
this->itemInserted.invoke(args);
this->invokeDelayedItemsChanged();
return index;
}
};
} // namespace chatterino

View file

@ -1,125 +0,0 @@
#pragma once
#include <QStandardItemModel>
#include <QTimer>
#include <boost/noncopyable.hpp>
#include <pajlada/signals/signal.hpp>
#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
{
public:
ReadOnlySignalVector()
{
QObject::connect(&this->itemsChangedTimer, &QTimer::timeout,
[this] { this->delayedItemsChanged.invoke(); });
this->itemsChangedTimer.setInterval(100);
this->itemsChangedTimer.setSingleShot(true);
}
virtual ~ReadOnlySignalVector() = default;
pajlada::Signals::Signal<SignalVectorItemArgs<TVectorItem>> itemInserted;
pajlada::Signals::Signal<SignalVectorItemArgs<TVectorItem>> itemRemoved;
pajlada::Signals::NoArgSignal delayedItemsChanged;
const std::vector<TVectorItem> &getVector() const
{
assertInGuiThread();
return this->vector;
}
void invokeDelayedItemsChanged()
{
assertInGuiThread();
if (!this->itemsChangedTimer.isActive()) {
itemsChangedTimer.start();
}
}
protected:
std::vector<TVectorItem> vector;
QTimer itemsChangedTimer;
};
template <typename TVectorItem>
class BaseSignalVector : public ReadOnlySignalVector<TVectorItem>
{
public:
// returns the actual index of the inserted item
virtual int insertItem(const TVectorItem &item, int proposedIndex = -1, void *caller = 0) = 0;
void removeItem(int index, void *caller = 0)
{
assertInGuiThread();
assert(index >= 0 && index < this->vector.size());
TVectorItem item = this->vector[index];
this->vector.erase(this->vector.begin() + index);
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
this->itemRemoved.invoke(args);
this->invokeDelayedItemsChanged();
}
int appendItem(const TVectorItem &item, void *caller = 0)
{
return this->insertItem(item, -1, caller);
}
};
template <typename TVectorItem>
class UnsortedSignalVector : public BaseSignalVector<TVectorItem>
{
public:
virtual int insertItem(const TVectorItem &item, int index = -1, void *caller = 0) override
{
assertInGuiThread();
if (index == -1) {
index = this->vector.size();
} else {
assert(index >= 0 && index <= this->vector.size());
}
this->vector.insert(this->vector.begin() + index, item);
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
this->itemInserted.invoke(args);
this->invokeDelayedItemsChanged();
return index;
}
};
template <typename TVectorItem, typename Compare>
class SortedSignalVector : public BaseSignalVector<TVectorItem>
{
public:
virtual int insertItem(const TVectorItem &item, int = -1, void *caller = nullptr) override
{
assertInGuiThread();
auto it = std::lower_bound(this->vector.begin(), this->vector.end(), item, Compare{});
int index = it - this->vector.begin();
this->vector.insert(it, item);
SignalVectorItemArgs<TVectorItem> args{item, index, caller};
this->itemInserted.invoke(args);
this->invokeDelayedItemsChanged();
return index;
}
};
} // namespace chatterino

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QStandardItem> #include <QStandardItem>

View file

@ -0,0 +1,34 @@
#pragma once
#include <pajlada/signals/signal.hpp>
#include <mutex>
#include <vector>
namespace chatterino {
template <typename TValue>
class SignalVector
{
public:
SignalVector &operator=(std::vector<TValue> &other)
{
this->data = other;
this->updated.invoke();
return *this;
}
operator std::vector<TValue> &()
{
return this->data;
}
pajlada::Signals::NoArgSignal updated;
private:
std::vector<TValue> data;
};
} // namespace chatterino

View file

@ -2,7 +2,7 @@
#include <QObject> #include <QObject>
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "controllers/accounts/Account.hpp" #include "controllers/accounts/Account.hpp"
#include "providers/twitch/TwitchAccountManager.hpp" #include "providers/twitch/TwitchAccountManager.hpp"
#include "util/SharedPtrElementLess.hpp" #include "util/SharedPtrElementLess.hpp"

View file

@ -1,7 +1,7 @@
#include "CommandController.hpp" #include "CommandController.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "controllers/accounts/AccountController.hpp" #include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/Command.hpp" #include "controllers/commands/Command.hpp"
#include "controllers/commands/CommandModel.hpp" #include "controllers/commands/CommandModel.hpp"

View file

@ -4,7 +4,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "controllers/commands/Command.hpp" #include "controllers/commands/Command.hpp"
namespace chatterino { namespace chatterino {

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "controllers/highlights/HighlightPhrase.hpp" #include "controllers/highlights/HighlightPhrase.hpp"
#include "messages/Message.hpp" #include "messages/Message.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "controllers/ignores/IgnorePhrase.hpp" #include "controllers/ignores/IgnorePhrase.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "controllers/moderationactions/ModerationAction.hpp" #include "controllers/moderationactions/ModerationAction.hpp"
#include "common/ChatterinoSetting.hpp" #include "common/ChatterinoSetting.hpp"

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "controllers/taggedusers/TaggedUser.hpp" #include "controllers/taggedusers/TaggedUser.hpp"
namespace chatterino { namespace chatterino {

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "common/Emotemap.hpp" #include "common/Emotemap.hpp"
#include "common/SignalVector.hpp" #include "common/SimpleSignalVector.hpp"
#include "util/ConcurrentMap.hpp" #include "util/ConcurrentMap.hpp"
#include <map> #include <map>

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "common/Emotemap.hpp" #include "common/Emotemap.hpp"
#include "common/SignalVector.hpp" #include "common/SimpleSignalVector.hpp"
#include "util/ConcurrentMap.hpp" #include "util/ConcurrentMap.hpp"
#include <QMap> #include <QMap>

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "common/Emotemap.hpp" #include "common/Emotemap.hpp"
#include "common/SignalVector.hpp" #include "common/SimpleSignalVector.hpp"
#include "util/ConcurrentMap.hpp" #include "util/ConcurrentMap.hpp"
#include <map> #include <map>

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "common/SignalVector2.hpp" #include "common/SignalVector.hpp"
#include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchAccount.hpp"
#include "util/SharedPtrElementLess.hpp" #include "util/SharedPtrElementLess.hpp"