From c83fc043e04906e0d3a967b8e3274fb49e47f552 Mon Sep 17 00:00:00 2001 From: fourtf Date: Thu, 13 Aug 2020 20:05:54 +0200 Subject: [PATCH] replaced raw pointers with unique_ptr --- src/widgets/Window.cpp | 2 +- .../dialogs/switcher/QuickSwitcherModel.cpp | 23 ++++++------------- .../dialogs/switcher/QuickSwitcherModel.hpp | 13 +++-------- .../dialogs/switcher/QuickSwitcherPopup.cpp | 12 +++++----- 4 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/widgets/Window.cpp b/src/widgets/Window.cpp index ae9b68a27..c57eb9104 100644 --- a/src/widgets/Window.cpp +++ b/src/widgets/Window.cpp @@ -390,7 +390,7 @@ void Window::addShortcuts() splitContainer->appendSplit(split); }); - createWindowShortcut(this, "CTRL+H", [this] { + createWindowShortcut(this, "CTRL+H", [] { getSettings()->hideSimilar.setValue(!getSettings()->hideSimilar); getApp()->windows->forceLayoutChannelViews(); }); diff --git a/src/widgets/dialogs/switcher/QuickSwitcherModel.cpp b/src/widgets/dialogs/switcher/QuickSwitcherModel.cpp index 4898e15ad..03bf26e3f 100644 --- a/src/widgets/dialogs/switcher/QuickSwitcherModel.cpp +++ b/src/widgets/dialogs/switcher/QuickSwitcherModel.cpp @@ -4,18 +4,9 @@ namespace chatterino { QuickSwitcherModel::QuickSwitcherModel(QWidget *parent) : QAbstractListModel(parent) - , items_(INITIAL_ITEMS_SIZE) { } -QuickSwitcherModel::~QuickSwitcherModel() -{ - for (AbstractSwitcherItem *item : this->items_) - { - delete item; - } -} - int QuickSwitcherModel::rowCount(const QModelIndex &parent) const { return this->items_.size(); @@ -30,29 +21,29 @@ QVariant QuickSwitcherModel::data(const QModelIndex &index, if (index.row() >= this->items_.size()) return QVariant(); - auto item = this->items_.at(index.row()); + auto item = this->items_[index.row()].get(); // See https://stackoverflow.com/a/44503822 . return QVariant::fromValue(static_cast(item)); } -void QuickSwitcherModel::addItem(AbstractSwitcherItem *item) +void QuickSwitcherModel::addItem(std::unique_ptr item) { // {begin,end}InsertRows needs to be called to notify attached views this->beginInsertRows(QModelIndex(), this->items_.size(), this->items_.size()); - this->items_.append(item); + this->items_.push_back(std::move(item)); this->endInsertRows(); } void QuickSwitcherModel::clear() { + if (this->items_.empty()) + return; + // {begin,end}RemoveRows needs to be called to notify attached views this->beginRemoveRows(QModelIndex(), 0, this->items_.size() - 1); - for (AbstractSwitcherItem *item : this->items_) - { - delete item; - } + // clear this->items_.clear(); this->endRemoveRows(); diff --git a/src/widgets/dialogs/switcher/QuickSwitcherModel.hpp b/src/widgets/dialogs/switcher/QuickSwitcherModel.hpp index 92b448925..70e08d7a4 100644 --- a/src/widgets/dialogs/switcher/QuickSwitcherModel.hpp +++ b/src/widgets/dialogs/switcher/QuickSwitcherModel.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include "widgets/dialogs/switcher/AbstractSwitcherItem.hpp" namespace chatterino { @@ -8,7 +9,6 @@ class QuickSwitcherModel : public QAbstractListModel { public: QuickSwitcherModel(QWidget *parent = nullptr); - ~QuickSwitcherModel(); /** * @brief Reimplements QAbstractItemModel::rowCount. @@ -37,7 +37,7 @@ public: * * @param item item to add to the model */ - void addItem(AbstractSwitcherItem *item); + void addItem(std::unique_ptr item); /** * @brief Clears this QuickSwitcherModel of all items. This will delete all @@ -47,13 +47,6 @@ public: void clear(); private: - /* - * On my system, the default QVector capacity is 0. 20 is an attempt at preventing - * frequent reallocations. The number is not backed by any user data but rather a - * guess at how many switcher items are probably going to be added. - */ - static constexpr int INITIAL_ITEMS_SIZE = 20; - - QVector items_; + std::vector> items_; }; } // namespace chatterino diff --git a/src/widgets/dialogs/switcher/QuickSwitcherPopup.cpp b/src/widgets/dialogs/switcher/QuickSwitcherPopup.cpp index 97ca292a1..6cc2039ff 100644 --- a/src/widgets/dialogs/switcher/QuickSwitcherPopup.cpp +++ b/src/widgets/dialogs/switcher/QuickSwitcherPopup.cpp @@ -106,8 +106,8 @@ void QuickSwitcherPopup::updateSuggestions(const QString &text) if (split->getChannel()->getName().contains(text, Qt::CaseInsensitive)) { - SwitchSplitItem *item = new SwitchSplitItem(split); - this->switcherModel_.addItem(item); + auto item = std::make_unique(split); + this->switcherModel_.addItem(std::move(item)); // We want to continue the outer loop so we need a goto goto nextPage; @@ -117,8 +117,8 @@ void QuickSwitcherPopup::updateSuggestions(const QString &text) // Then check if tab title matches if (tabTitle.contains(text, Qt::CaseInsensitive)) { - SwitchSplitItem *item = new SwitchSplitItem(sc); - this->switcherModel_.addItem(item); + auto item = std::make_unique(sc); + this->switcherModel_.addItem(std::move(item)); continue; } @@ -128,8 +128,8 @@ void QuickSwitcherPopup::updateSuggestions(const QString &text) // Add item for opening a channel in a new tab if (!text.isEmpty()) { - NewTabItem *item = new NewTabItem(text); - this->switcherModel_.addItem(item); + auto item = std::make_unique(text); + this->switcherModel_.addItem(std::move(item)); } const auto &startIdx = this->switcherModel_.index(0);