diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fdf30bc..c8cb3bd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ - Dev: Set settings directory to temporary one used in tests. (#6584) - Dev: Check Lua unwinding and version in tests. (#6586) - Dev: Added method to get the last N messages of a channel. (#6602, #6604) +- Dev: Unwrapped `LimitedQueueSnapshot` to `std::vector`. (#6606) ## 2.5.4 diff --git a/src/common/Channel.cpp b/src/common/Channel.cpp index eff0e88e..ab8f891f 100644 --- a/src/common/Channel.cpp +++ b/src/common/Channel.cpp @@ -71,13 +71,12 @@ bool Channel::hasMessages() const return !this->messages_.empty(); } -LimitedQueueSnapshot Channel::getMessageSnapshot() const +std::vector Channel::getMessageSnapshot() const { return this->messages_.getSnapshot(); } -LimitedQueueSnapshot Channel::getMessageSnapshot( - size_t nItems) const +std::vector Channel::getMessageSnapshot(size_t nItems) const { return this->messages_.lastN(nItems); } @@ -147,7 +146,7 @@ void Channel::addOrReplaceClearChat(MessagePtr message, const QDateTime &now) void Channel::disableAllMessages() { - LimitedQueueSnapshot snapshot = this->getMessageSnapshot(); + std::vector snapshot = this->getMessageSnapshot(); int snapshotLength = snapshot.size(); for (int i = 0; i < snapshotLength; i++) { diff --git a/src/common/Channel.hpp b/src/common/Channel.hpp index 074cf389..8cad8c59 100644 --- a/src/common/Channel.hpp +++ b/src/common/Channel.hpp @@ -79,8 +79,8 @@ public: virtual const QString &getLocalizedName() const; bool isTwitchChannel() const; virtual bool isEmpty() const; - LimitedQueueSnapshot getMessageSnapshot() const; - LimitedQueueSnapshot getMessageSnapshot(size_t nItems) const; + std::vector getMessageSnapshot() const; + std::vector getMessageSnapshot(size_t nItems) const; // MESSAGES // overridingFlags can be filled in with flags that should be used instead diff --git a/src/controllers/notifications/NotificationController.cpp b/src/controllers/notifications/NotificationController.cpp index 5b2ad094..866be087 100644 --- a/src/controllers/notifications/NotificationController.cpp +++ b/src/controllers/notifications/NotificationController.cpp @@ -154,7 +154,7 @@ void NotificationController::notifyTwitchChannelLive( void NotificationController::notifyTwitchChannelOffline(const QString &id) const { // "delete" old 'CHANNEL is live' message - const LimitedQueueSnapshot snapshot = + const std::vector snapshot = getApp()->getTwitch()->getLiveChannel()->getMessageSnapshot(200); // Guard against empty snapshot to prevent underflow and invalid access diff --git a/src/messages/LimitedQueue.hpp b/src/messages/LimitedQueue.hpp index fb4647be..368abda0 100644 --- a/src/messages/LimitedQueue.hpp +++ b/src/messages/LimitedQueue.hpp @@ -1,7 +1,5 @@ #pragma once -#include "messages/LimitedQueueSnapshot.hpp" - #include #include @@ -318,22 +316,28 @@ public: return false; } - [[nodiscard]] LimitedQueueSnapshot getSnapshot() const + [[nodiscard]] std::vector getSnapshot() const { std::shared_lock lock(this->mutex_); - return LimitedQueueSnapshot(this->buffer_); + return {this->buffer_.begin(), this->buffer_.end()}; } - [[nodiscard]] LimitedQueueSnapshot lastN(size_t nItems) const + [[nodiscard]] std::vector lastN(size_t nItems) const { std::shared_lock lock(this->mutex_); - return LimitedQueueSnapshot::lastN(this->buffer_, nItems); + return { + this->buffer_.end() - std::min(nItems, this->buffer_.size()), + this->buffer_.end(), + }; } - [[nodiscard]] LimitedQueueSnapshot firstN(size_t nItems) const + [[nodiscard]] std::vector firstN(size_t nItems) const { std::shared_lock lock(this->mutex_); - return LimitedQueueSnapshot::firstN(this->buffer_, nItems); + return { + this->buffer_.begin(), + this->buffer_.begin() + std::min(nItems, this->buffer_.size()), + }; } // Actions diff --git a/src/messages/LimitedQueueSnapshot.hpp b/src/messages/LimitedQueueSnapshot.hpp deleted file mode 100644 index ac5f032d..00000000 --- a/src/messages/LimitedQueueSnapshot.hpp +++ /dev/null @@ -1,80 +0,0 @@ -#pragma once - -#include - -#include -#include -#include - -namespace chatterino { - -template -class LimitedQueue; - -template -class LimitedQueueSnapshot -{ -private: - friend class LimitedQueue; - - LimitedQueueSnapshot(const boost::circular_buffer &buf) - : buffer_(buf.begin(), buf.end()) - { - } - - static LimitedQueueSnapshot lastN(const boost::circular_buffer &buf, - size_t n) - { - return {buf.end() - std::min(n, buf.size()), buf.end()}; - } - - static LimitedQueueSnapshot firstN(const boost::circular_buffer &buf, - size_t n) - { - return {buf.begin(), buf.begin() + std::min(n, buf.size())}; - } - - template - LimitedQueueSnapshot(It begin, It end) - : buffer_(std::move(begin), std::move(end)) - { - } - -public: - LimitedQueueSnapshot() = default; - - size_t size() const - { - return this->buffer_.size(); - } - - const T &operator[](size_t index) const - { - return this->buffer_[index]; - } - - auto begin() const - { - return this->buffer_.begin(); - } - - auto end() const - { - return this->buffer_.end(); - } - - auto rbegin() const - { - return this->buffer_.rbegin(); - } - - auto rend() const - { - return this->buffer_.rend(); - } - -private: - std::vector buffer_; -}; - -} // namespace chatterino diff --git a/src/messages/MessageSimilarity.cpp b/src/messages/MessageSimilarity.cpp index 2f8157d6..2cf25cf6 100644 --- a/src/messages/MessageSimilarity.cpp +++ b/src/messages/MessageSimilarity.cpp @@ -2,7 +2,6 @@ #include "Application.hpp" #include "controllers/accounts/AccountController.hpp" -#include "messages/LimitedQueueSnapshot.hpp" // IWYU pragma: keep #include "providers/twitch/TwitchAccount.hpp" #include "singletons/Settings.hpp" @@ -115,7 +114,5 @@ void setSimilarityFlags(const MessagePtr &message, const T &messages) template void setSimilarityFlags>( const MessagePtr &msg, const std::vector &messages); -template void setSimilarityFlags>( - const MessagePtr &msg, const LimitedQueueSnapshot &messages); } // namespace chatterino diff --git a/src/providers/twitch/IrcMessageHandler.hpp b/src/providers/twitch/IrcMessageHandler.hpp index d81f282a..665cfab8 100644 --- a/src/providers/twitch/IrcMessageHandler.hpp +++ b/src/providers/twitch/IrcMessageHandler.hpp @@ -1,7 +1,5 @@ #pragma once -#include "messages/LimitedQueueSnapshot.hpp" - #include #include @@ -66,7 +64,7 @@ public: private: static float similarity(const MessagePtr &msg, - const LimitedQueueSnapshot &messages); + const std::vector &messages); static void setSimilarityFlags(const MessagePtr &message, const ChannelPtr &channel); }; diff --git a/src/providers/twitch/TwitchIrcServer.cpp b/src/providers/twitch/TwitchIrcServer.cpp index 86293b0a..53a73911 100644 --- a/src/providers/twitch/TwitchIrcServer.cpp +++ b/src/providers/twitch/TwitchIrcServer.cpp @@ -7,7 +7,6 @@ #include "common/Literals.hpp" #include "common/QLogging.hpp" #include "controllers/accounts/AccountController.hpp" -#include "messages/LimitedQueueSnapshot.hpp" #include "messages/Message.hpp" #include "messages/MessageBuilder.hpp" #include "providers/bttv/BttvEmotes.hpp" @@ -480,7 +479,7 @@ void TwitchIrcServer::onReadConnected(IrcConnection *connection) for (const auto &chan : activeChannels) { - LimitedQueueSnapshot snapshot = chan->getMessageSnapshot(); + std::vector snapshot = chan->getMessageSnapshot(); bool replaceMessage = snapshot.size() > 0 && snapshot[snapshot.size() - 1]->flags.has( diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index f388ceb8..f3efa86b 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -108,7 +108,7 @@ bool checkMessageUserName(const QString &userName, MessagePtr message) ChannelPtr filterMessages(const QString &userName, ChannelPtr channel) { - LimitedQueueSnapshot snapshot = channel->getMessageSnapshot(); + std::vector snapshot = channel->getMessageSnapshot(); ChannelPtr channelPtr; if (channel->isTwitchChannel()) diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index 85e6b591..4e4879d1 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -13,7 +13,6 @@ #include "messages/layouts/MessageLayout.hpp" #include "messages/layouts/MessageLayoutContext.hpp" #include "messages/layouts/MessageLayoutElement.hpp" -#include "messages/LimitedQueueSnapshot.hpp" #include "messages/Message.hpp" #include "messages/MessageBuilder.hpp" #include "messages/MessageElement.hpp" @@ -693,7 +692,7 @@ void ChannelView::performLayout(bool causedByScrollbar, bool causedByShow) } void ChannelView::layoutVisibleMessages( - const LimitedQueueSnapshot &messages) + const std::vector &messages) { const auto start = size_t(this->scrollBar_->getRelativeCurrentValue()); const auto layoutWidth = this->getLayoutWidth(); @@ -731,9 +730,8 @@ void ChannelView::layoutVisibleMessages( } } -void ChannelView::updateScrollbar( - const LimitedQueueSnapshot &messages, - bool causedByScrollbar, bool causedByShow) +void ChannelView::updateScrollbar(const std::vector &messages, + bool causedByScrollbar, bool causedByShow) { if (messages.size() == 0) { @@ -820,7 +818,7 @@ QString ChannelView::getSelectedText() { QString result = ""; - LimitedQueueSnapshot &messagesSnapshot = + std::vector &messagesSnapshot = this->getMessagesSnapshot(); Selection selection = this->selection_; @@ -897,7 +895,7 @@ const std::optional &ChannelView::getOverrideFlags() const return this->overrideFlags_; } -LimitedQueueSnapshot &ChannelView::getMessagesSnapshot() +std::vector &ChannelView::getMessagesSnapshot() { this->snapshotGuard_.guard(); if (!this->paused() /*|| this->scrollBar_->isVisible()*/) diff --git a/src/widgets/helper/ChannelView.hpp b/src/widgets/helper/ChannelView.hpp index 096e5e1c..3925a3dd 100644 --- a/src/widgets/helper/ChannelView.hpp +++ b/src/widgets/helper/ChannelView.hpp @@ -3,7 +3,6 @@ #include "common/FlagsEnum.hpp" #include "messages/layouts/MessageLayoutContext.hpp" #include "messages/LimitedQueue.hpp" -#include "messages/LimitedQueueSnapshot.hpp" #include "messages/MessageFlag.hpp" #include "messages/Selection.hpp" #include "util/ThreadGuard.hpp" @@ -178,7 +177,7 @@ public: /// Checks if this view has a #sourceChannel bool hasSourceChannel() const; - LimitedQueueSnapshot &getMessagesSnapshot(); + std::vector &getMessagesSnapshot(); void queueLayout(); void invalidateBuffers(); @@ -286,9 +285,8 @@ private: void performLayout(bool causedByScrollbar = false, bool causedByShow = false); - void layoutVisibleMessages( - const LimitedQueueSnapshot &messages); - void updateScrollbar(const LimitedQueueSnapshot &messages, + void layoutVisibleMessages(const std::vector &messages); + void updateScrollbar(const std::vector &messages, bool causedByScrollbar, bool causedByShow); void drawMessages(QPainter &painter, const QRect &area); @@ -354,7 +352,7 @@ private: MessageLayoutPtr lastReadMessage_; ThreadGuard snapshotGuard_; - LimitedQueueSnapshot snapshot_; + std::vector snapshot_; /// @brief The backing (internal) channel /// diff --git a/src/widgets/helper/SearchPopup.cpp b/src/widgets/helper/SearchPopup.cpp index 8289118e..57f0d488 100644 --- a/src/widgets/helper/SearchPopup.cpp +++ b/src/widgets/helper/SearchPopup.cpp @@ -26,7 +26,7 @@ namespace chatterino { ChannelPtr SearchPopup::filter(const QString &text, const QString &channelName, - const LimitedQueueSnapshot &snapshot) + const std::vector &snapshot) { ChannelPtr channel(new Channel(channelName, Channel::Type::None)); @@ -233,7 +233,7 @@ void SearchPopup::search() this->channelName_, this->snapshot_)); } -LimitedQueueSnapshot SearchPopup::buildSnapshot() +std::vector SearchPopup::buildSnapshot() { // no point in filtering/sorting if it's a single channel search if (this->searchChannels_.length() == 1) @@ -248,7 +248,7 @@ LimitedQueueSnapshot SearchPopup::buildSnapshot() ChannelView &sharedView = channel.get(); const FilterSetPtr filterSet = sharedView.getFilterSet(); - const LimitedQueueSnapshot &snapshot = + const std::vector &snapshot = sharedView.channel()->getMessageSnapshot(); for (const auto &message : snapshot) @@ -284,10 +284,7 @@ LimitedQueueSnapshot SearchPopup::buildSnapshot() return a->serverReceivedTime < b->serverReceivedTime; }); - auto queue = LimitedQueue(combinedSnapshot.size()); - queue.pushFront(combinedSnapshot); - - return queue.getSnapshot(); + return combinedSnapshot; } void SearchPopup::initLayout() diff --git a/src/widgets/helper/SearchPopup.hpp b/src/widgets/helper/SearchPopup.hpp index e8f578fa..03bb0e31 100644 --- a/src/widgets/helper/SearchPopup.hpp +++ b/src/widgets/helper/SearchPopup.hpp @@ -1,7 +1,6 @@ #pragma once #include "ForwardDecl.hpp" -#include "messages/LimitedQueueSnapshot.hpp" #include "widgets/BasePopup.hpp" #include @@ -38,7 +37,7 @@ private: void initLayout(); void search(); void addShortcuts() override; - LimitedQueueSnapshot buildSnapshot(); + std::vector buildSnapshot(); /** * @brief Only retains those message from a list of messages that satisfy a @@ -53,7 +52,7 @@ private: * "snapshot" */ static ChannelPtr filter(const QString &text, const QString &channelName, - const LimitedQueueSnapshot &snapshot); + const std::vector &snapshot); /** * @brief Checks the input for tags and registers their corresponding @@ -65,7 +64,7 @@ private: static std::vector> parsePredicates( const QString &input); - LimitedQueueSnapshot snapshot_; + std::vector snapshot_; QLineEdit *searchInput_{}; ChannelView *channelView_{}; QString channelName_{}; diff --git a/tests/src/LimitedQueue.cpp b/tests/src/LimitedQueue.cpp index aa77bfca..d6aa5082 100644 --- a/tests/src/LimitedQueue.cpp +++ b/tests/src/LimitedQueue.cpp @@ -6,55 +6,12 @@ using namespace chatterino; -namespace chatterino { - template -std::ostream &operator<<(std::ostream &os, - const LimitedQueueSnapshot &snapshot) -{ - os << "[ "; - for (size_t i = 0; i < snapshot.size(); ++i) - { - os << snapshot[i] << ' '; - } - os << "]"; - - return os; -} - -} // namespace chatterino - -namespace std { -template -std::ostream &operator<<(std::ostream &os, const vector &vec) -{ - os << "[ "; - for (const auto &item : vec) - { - os << item << ' '; - } - os << "]"; - - return os; -} -} // namespace std - -template -inline void SNAPSHOT_EQUALS(const LimitedQueueSnapshot &snapshot, +inline void SNAPSHOT_EQUALS(const std::vector &snapshot, const std::vector &values, const std::string &msg) { - SCOPED_TRACE(msg); - ASSERT_EQ(snapshot.size(), values.size()) - << "snapshot = " << snapshot << " values = " << values; - - if (snapshot.size() != values.size()) - return; - - for (size_t i = 0; i < snapshot.size(); ++i) - { - EXPECT_EQ(snapshot[i], values[i]) << "i = " << i; - } + EXPECT_EQ(snapshot, values) << msg; } TEST(LimitedQueue, PushBack)