replaced raw pointers with unique_ptr

This commit is contained in:
fourtf 2020-08-13 20:05:54 +02:00
parent a9080ceb3c
commit c83fc043e0
4 changed files with 17 additions and 33 deletions

View file

@ -390,7 +390,7 @@ void Window::addShortcuts()
splitContainer->appendSplit(split); splitContainer->appendSplit(split);
}); });
createWindowShortcut(this, "CTRL+H", [this] { createWindowShortcut(this, "CTRL+H", [] {
getSettings()->hideSimilar.setValue(!getSettings()->hideSimilar); getSettings()->hideSimilar.setValue(!getSettings()->hideSimilar);
getApp()->windows->forceLayoutChannelViews(); getApp()->windows->forceLayoutChannelViews();
}); });

View file

@ -4,18 +4,9 @@ namespace chatterino {
QuickSwitcherModel::QuickSwitcherModel(QWidget *parent) QuickSwitcherModel::QuickSwitcherModel(QWidget *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
, items_(INITIAL_ITEMS_SIZE)
{ {
} }
QuickSwitcherModel::~QuickSwitcherModel()
{
for (AbstractSwitcherItem *item : this->items_)
{
delete item;
}
}
int QuickSwitcherModel::rowCount(const QModelIndex &parent) const int QuickSwitcherModel::rowCount(const QModelIndex &parent) const
{ {
return this->items_.size(); return this->items_.size();
@ -30,29 +21,29 @@ QVariant QuickSwitcherModel::data(const QModelIndex &index,
if (index.row() >= this->items_.size()) if (index.row() >= this->items_.size())
return QVariant(); return QVariant();
auto item = this->items_.at(index.row()); auto item = this->items_[index.row()].get();
// See https://stackoverflow.com/a/44503822 . // See https://stackoverflow.com/a/44503822 .
return QVariant::fromValue(static_cast<void *>(item)); return QVariant::fromValue(static_cast<void *>(item));
} }
void QuickSwitcherModel::addItem(AbstractSwitcherItem *item) void QuickSwitcherModel::addItem(std::unique_ptr<AbstractSwitcherItem> item)
{ {
// {begin,end}InsertRows needs to be called to notify attached views // {begin,end}InsertRows needs to be called to notify attached views
this->beginInsertRows(QModelIndex(), this->items_.size(), this->beginInsertRows(QModelIndex(), this->items_.size(),
this->items_.size()); this->items_.size());
this->items_.append(item); this->items_.push_back(std::move(item));
this->endInsertRows(); this->endInsertRows();
} }
void QuickSwitcherModel::clear() void QuickSwitcherModel::clear()
{ {
if (this->items_.empty())
return;
// {begin,end}RemoveRows needs to be called to notify attached views // {begin,end}RemoveRows needs to be called to notify attached views
this->beginRemoveRows(QModelIndex(), 0, this->items_.size() - 1); this->beginRemoveRows(QModelIndex(), 0, this->items_.size() - 1);
for (AbstractSwitcherItem *item : this->items_) // clear
{
delete item;
}
this->items_.clear(); this->items_.clear();
this->endRemoveRows(); this->endRemoveRows();

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory>
#include "widgets/dialogs/switcher/AbstractSwitcherItem.hpp" #include "widgets/dialogs/switcher/AbstractSwitcherItem.hpp"
namespace chatterino { namespace chatterino {
@ -8,7 +9,6 @@ class QuickSwitcherModel : public QAbstractListModel
{ {
public: public:
QuickSwitcherModel(QWidget *parent = nullptr); QuickSwitcherModel(QWidget *parent = nullptr);
~QuickSwitcherModel();
/** /**
* @brief Reimplements QAbstractItemModel::rowCount. * @brief Reimplements QAbstractItemModel::rowCount.
@ -37,7 +37,7 @@ public:
* *
* @param item item to add to the model * @param item item to add to the model
*/ */
void addItem(AbstractSwitcherItem *item); void addItem(std::unique_ptr<AbstractSwitcherItem> item);
/** /**
* @brief Clears this QuickSwitcherModel of all items. This will delete all * @brief Clears this QuickSwitcherModel of all items. This will delete all
@ -47,13 +47,6 @@ public:
void clear(); void clear();
private: private:
/* std::vector<std::unique_ptr<AbstractSwitcherItem>> items_;
* 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<AbstractSwitcherItem *> items_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -106,8 +106,8 @@ void QuickSwitcherPopup::updateSuggestions(const QString &text)
if (split->getChannel()->getName().contains(text, if (split->getChannel()->getName().contains(text,
Qt::CaseInsensitive)) Qt::CaseInsensitive))
{ {
SwitchSplitItem *item = new SwitchSplitItem(split); auto item = std::make_unique<SwitchSplitItem>(split);
this->switcherModel_.addItem(item); this->switcherModel_.addItem(std::move(item));
// We want to continue the outer loop so we need a goto // We want to continue the outer loop so we need a goto
goto nextPage; goto nextPage;
@ -117,8 +117,8 @@ void QuickSwitcherPopup::updateSuggestions(const QString &text)
// Then check if tab title matches // Then check if tab title matches
if (tabTitle.contains(text, Qt::CaseInsensitive)) if (tabTitle.contains(text, Qt::CaseInsensitive))
{ {
SwitchSplitItem *item = new SwitchSplitItem(sc); auto item = std::make_unique<SwitchSplitItem>(sc);
this->switcherModel_.addItem(item); this->switcherModel_.addItem(std::move(item));
continue; continue;
} }
@ -128,8 +128,8 @@ void QuickSwitcherPopup::updateSuggestions(const QString &text)
// Add item for opening a channel in a new tab // Add item for opening a channel in a new tab
if (!text.isEmpty()) if (!text.isEmpty())
{ {
NewTabItem *item = new NewTabItem(text); auto item = std::make_unique<NewTabItem>(text);
this->switcherModel_.addItem(item); this->switcherModel_.addItem(std::move(item));
} }
const auto &startIdx = this->switcherModel_.index(0); const auto &startIdx = this->switcherModel_.index(0);