Commit 40116592 authored by Xaver Hugl's avatar Xaver Hugl Committed by Fushan Wen
Browse files

port to C++ smart pointers where possible

parent a8ce876a
Pipeline #185387 passed with stage
in 12 minutes and 1 second
......@@ -79,7 +79,7 @@ private:
//! window that its menu initialization may be delayed
WId m_delayedMenuWindowId = 0;
QScopedPointer<QMenu> m_searchMenu;
std::unique_ptr<QMenu> m_searchMenu;
QPointer<QMenu> m_menu;
QPointer<QAction> m_searchAction;
QList<QAction *> m_currentSearchActions;
......
......@@ -370,7 +370,7 @@ QList<QAction *> IconApplet::contextualActions()
m_openWithMenu.reset(new QMenu());
}
m_openWithMenu->clear();
m_fileItemActions->insertOpenWithActionsTo(nullptr, m_openWithMenu.data(), QStringList());
m_fileItemActions->insertOpenWithActionsTo(nullptr, m_openWithMenu.get(), QStringList());
m_openWithActions = m_openWithMenu->actions();
}
......
......@@ -87,7 +87,7 @@ private:
QAction *m_openContainingFolderAction = nullptr;
KFileItemActions *m_fileItemActions = nullptr;
QScopedPointer<QMenu> m_openWithMenu;
std::unique_ptr<QMenu> m_openWithMenu;
QPointer<KPropertiesDialog> m_configDialog;
......
......@@ -7,6 +7,7 @@
*/
#include "appmenu.h"
#include "../c_ptr.h"
#include "appmenu_dbus.h"
#include "appmenu_debug.h"
#include "appmenuadaptor.h"
......@@ -134,8 +135,8 @@ void AppMenuModule::slotWindowRegistered(WId id, const QString &serviceName, con
auto setWindowProperty = [c](WId id, xcb_atom_t &atom, const QByteArray &name, const QByteArray &value) {
if (atom == XCB_ATOM_NONE) {
const xcb_intern_atom_cookie_t cookie = xcb_intern_atom(c, false, name.length(), name.constData());
QScopedPointer<xcb_intern_atom_reply_t, QScopedPointerPodDeleter> reply(xcb_intern_atom_reply(c, cookie, nullptr));
if (reply.isNull()) {
UniqueCPointer<xcb_intern_atom_reply_t> reply{xcb_intern_atom_reply(c, cookie, nullptr)};
if (!reply) {
return;
}
atom = reply->atom;
......
/*
SPDX-FileCopyrightText: 2022 Xaver Hugl <xaver.hugl@gmail.com>
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once
#include <memory>
struct CDeleter {
template<typename T>
void operator()(T *ptr)
{
if (ptr) {
free(ptr);
}
}
};
template<typename T>
using UniqueCPointer = std::unique_ptr<T, CDeleter>;
......@@ -58,7 +58,7 @@ public:
, filterModel(w)
, activitiesConsumer(new KActivities::Consumer())
{
QObject::connect(activitiesConsumer.data(), &Consumer::currentActivityChanged, q, [this] {
QObject::connect(activitiesConsumer.get(), &Consumer::currentActivityChanged, q, [this] {
initRunningApplets();
});
}
......@@ -98,7 +98,7 @@ public:
DefaultItemFilterProxyModel filterItemModel;
static QPointer<KNS3::QtQuickDialogWrapper> newStuffDialog;
QScopedPointer<KActivities::Consumer> activitiesConsumer;
std::unique_ptr<KActivities::Consumer> activitiesConsumer;
};
QPointer<KNS3::QtQuickDialogWrapper> WidgetExplorerPrivate::newStuffDialog;
......
......@@ -31,6 +31,7 @@
#endif
#include <xcb/xcb.h>
#include "../c_ptr.h"
#include "window.h"
#include <chrono>
......@@ -340,14 +341,14 @@ QByteArray MenuProxy::getWindowPropertyString(WId id, const QByteArray &name)
static const long MAX_PROP_SIZE = 10000;
auto propertyCookie = xcb_get_property(m_xConnection, false, id, atom, utf8StringAtom, 0, MAX_PROP_SIZE);
QScopedPointer<xcb_get_property_reply_t, QScopedPointerPodDeleter> propertyReply(xcb_get_property_reply(m_xConnection, propertyCookie, nullptr));
if (propertyReply.isNull()) {
UniqueCPointer<xcb_get_property_reply_t> propertyReply(xcb_get_property_reply(m_xConnection, propertyCookie, nullptr));
if (!propertyReply) {
qCWarning(DBUSMENUPROXY) << "XCB property reply for atom" << name << "on" << id << "was null";
return value;
}
if (propertyReply->type == utf8StringAtom && propertyReply->format == 8 && propertyReply->value_len > 0) {
const char *data = (const char *)xcb_get_property_value(propertyReply.data());
const char *data = (const char *)xcb_get_property_value(propertyReply.get());
int len = propertyReply->value_len;
if (data) {
value = QByteArray(data, data[len - 1] ? len : len - 1);
......@@ -378,8 +379,8 @@ xcb_atom_t MenuProxy::getAtom(const QByteArray &name)
auto atom = s_atoms.value(name, XCB_ATOM_NONE);
if (atom == XCB_ATOM_NONE) {
const xcb_intern_atom_cookie_t atomCookie = xcb_intern_atom(m_xConnection, false, name.length(), name.constData());
QScopedPointer<xcb_intern_atom_reply_t, QScopedPointerPodDeleter> atomReply(xcb_intern_atom_reply(m_xConnection, atomCookie, nullptr));
if (!atomReply.isNull()) {
UniqueCPointer<xcb_intern_atom_reply_t> atomReply(xcb_intern_atom_reply(m_xConnection, atomCookie, nullptr));
if (atomReply) {
atom = atomReply->atom;
if (atom != XCB_ATOM_NONE) {
s_atoms.insert(name, atom);
......
......@@ -12,7 +12,6 @@
#include <QColor>
#include <QDBusPendingCallWatcher>
#include <QPointer>
#include <QScopedPointer>
#include <KSharedConfig>
......@@ -120,6 +119,6 @@ private:
KSharedConfigPtr m_config;
KConfigWatcher::Ptr m_configWatcher;
QScopedPointer<QTemporaryFile> m_tempInstallFile;
std::unique_ptr<QTemporaryFile> m_tempInstallFile;
QPointer<KIO::FileCopyJob> m_tempCopyJob;
};
......@@ -9,7 +9,6 @@
#include <KNSCore/EntryWrapper>
#include <KQuickAddons/ManagedConfigModule>
#include <QScopedPointer>
#include "cursorthemesettings.h"
......@@ -121,6 +120,6 @@ private:
bool m_canResize;
bool m_canConfigure;
QScopedPointer<QTemporaryFile> m_tempInstallFile;
std::unique_ptr<QTemporaryFile> m_tempInstallFile;
QPointer<KIO::FileCopyJob> m_tempCopyJob;
};
......@@ -84,7 +84,7 @@ private:
QHash<QString, Plasma::Theme *> m_themes;
bool m_haveThemeExplorerInstalled;
QScopedPointer<QTemporaryFile> m_tempInstallFile;
std::unique_ptr<QTemporaryFile> m_tempInstallFile;
QPointer<KIO::FileCopyJob> m_tempCopyJob;
};
......
......@@ -313,7 +313,7 @@ QVariantList IconModule::previewIcons(const QString &themeName, int size, qreal
{QStringLiteral("folder-video"), QStringLiteral("folder-videos")}};
// created on-demand as it is quite expensive to do and we don't want to do it every loop iteration either
QScopedPointer<KIconTheme> theme;
std::unique_ptr<KIconTheme> theme;
QVariantList pixmaps;
......@@ -327,7 +327,7 @@ QVariantList IconModule::previewIcons(const QString &themeName, int size, qreal
theme.reset(new KIconTheme(themeName));
}
pix = getBestIcon(*theme.data(), iconNames, size, dpr);
pix = getBestIcon(*theme.get(), iconNames, size, dpr);
// Inserting a pixmap even if null so we know whether we searched for it already
QPixmapCache::insert(cacheKey, pix);
......
......@@ -16,7 +16,6 @@
#include <KQuickAddons/ManagedConfigModule>
#include <QCache>
#include <QScopedPointer>
class KIconTheme;
class IconsSettings;
......@@ -103,6 +102,6 @@ private:
mutable QCache<QString, KIconTheme> m_kiconThemeCache;
QScopedPointer<QTemporaryFile> m_tempInstallFile;
std::unique_ptr<QTemporaryFile> m_tempInstallFile;
QPointer<KIO::FileCopyJob> m_tempCopyJob;
};
......@@ -358,9 +358,9 @@ void KCMLookandFeel::save()
// Test if style can be installed before updating the config.
KSharedConfigPtr conf = KSharedConfig::openConfig(package.filePath("defaults"));
KConfigGroup cg(conf, "kdeglobals");
QScopedPointer<QStyle> newStyle(QStyleFactory::create(cg.readEntry("widgetStyle", QString())));
applyFlags.setFlag(LookAndFeelManager::WidgetStyle, (!newStyle.isNull() &&
m_model->data(m_model->index(index, 0), HasWidgetStyleRole).toBool())); //Widget Style isn't in
std::unique_ptr<QStyle> newStyle(QStyleFactory::create(cg.readEntry("widgetStyle", QString())));
applyFlags.setFlag(LookAndFeelManager::WidgetStyle,
(newStyle != nullptr && m_model->data(m_model->index(index, 0), HasWidgetStyleRole).toBool())); // Widget Style isn't in
// the loop above since it has all of this extra checking too for it
}
m_lnf->setAppearanceToApply(applyFlags);
......
......@@ -33,7 +33,6 @@
#include <QQuickItem>
#include <QQuickRenderControl>
#include <QQuickWindow>
#include <QScopedPointer>
#include <QStyleFactory>
#include <QWidget>
#include <QWindow>
......@@ -272,7 +271,7 @@ void KCMStyle::save()
// Otherwise apps will use the default style despite something else having been written to the config
bool newStyleLoaded = false;
if (styleSettings()->widgetStyle() != m_previousStyle) {
QScopedPointer<QStyle> newStyle(QStyleFactory::create(styleSettings()->widgetStyle()));
std::unique_ptr<QStyle> newStyle(QStyleFactory::create(styleSettings()->widgetStyle()));
if (newStyle) {
newStyleLoaded = true;
m_previousStyle = styleSettings()->widgetStyle();
......
......@@ -44,7 +44,7 @@ void PreviewItem::componentComplete()
bool PreviewItem::eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_widget.data()) {
if (watched == m_widget.get()) {
switch (event->type()) {
case QEvent::Show:
case QEvent::UpdateRequest:
......@@ -118,7 +118,7 @@ void PreviewItem::reload()
// Do not wait for this widget to close before the app closes
m_widget->setAttribute(Qt::WA_QuitOnClose, false);
m_ui.setupUi(m_widget.data());
m_ui.setupUi(m_widget.get());
// Prevent Qt from wrongly caching radio button images
QPixmapCache::clear();
......@@ -136,7 +136,7 @@ void PreviewItem::reload()
palette.setColor(QPalette::Inactive, role, palette.color(QPalette::Active, role));
}
setStyleRecursively(m_widget.data(), m_style.data(), palette);
setStyleRecursively(m_widget.get(), m_style.get(), palette);
m_widget->ensurePolished();
......@@ -183,13 +183,13 @@ void PreviewItem::sendHoverEvent(QHoverEvent *event)
QPointF pos = event->pos();
QWidget *child = m_widget->childAt(pos.toPoint());
QWidget *receiver = child ? child : m_widget.data();
QWidget *receiver = child ? child : m_widget.get();
dispatchEnterLeave(receiver, m_lastWidgetUnderMouse, mapToGlobal(event->pos()));
m_lastWidgetUnderMouse = receiver;
pos = receiver->mapFrom(m_widget.data(), pos.toPoint());
pos = receiver->mapFrom(m_widget.get(), pos.toPoint());
QMouseEvent mouseEvent(QEvent::MouseMove,
pos,
......
......@@ -9,7 +9,6 @@
#include <QPointer>
#include <QQuickPaintedItem>
#include <QScopedPointer>
#include "ui_stylepreview.h"
......@@ -61,8 +60,8 @@ private:
Ui::StylePreview m_ui;
QScopedPointer<QWidget> m_widget;
std::unique_ptr<QWidget> m_widget;
QPointer<QWidget> m_lastWidgetUnderMouse;
QScopedPointer<QStyle> m_style;
std::unique_ptr<QStyle> m_style;
};
......@@ -125,11 +125,11 @@ private Q_SLOTS:
QFETCH(QUrl, srcUrl);
QFETCH(QUrl, destUrl);
QScopedPointer<KDirLister> lister(nullptr);
std::unique_ptr<KDirLister> lister;
if (withDirListerCache) {
lister.reset(new KDirLister);
lister->openUrl(QUrl(QStringLiteral("desktop:/")));
QSignalSpy spyCompleted(lister.data(), static_cast<void (KDirLister::*)()>(&KDirLister::completed));
QSignalSpy spyCompleted(lister.get(), static_cast<void (KDirLister::*)()>(&KDirLister::completed));
spyCompleted.wait();
}
......
......@@ -26,8 +26,8 @@ private Q_SLOTS:
void HistoryModelTest::testSetMaxSize()
{
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr));
QScopedPointer<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.data()));
std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
QCOMPARE(history->rowCount(), 0);
QCOMPARE(history->maxSize(), 0);
......@@ -64,8 +64,8 @@ void HistoryModelTest::testSetMaxSize()
void HistoryModelTest::testInsertRemove()
{
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr));
QScopedPointer<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.data()));
std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
history->setMaxSize(10);
QCOMPARE(history->rowCount(), 0);
......@@ -165,8 +165,8 @@ void HistoryModelTest::testInsertRemove()
void HistoryModelTest::testClear()
{
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr));
QScopedPointer<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.data()));
std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
history->setMaxSize(10);
QCOMPARE(history->rowCount(), 0);
......@@ -187,8 +187,8 @@ void HistoryModelTest::testClear()
void HistoryModelTest::testIndexOf()
{
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr));
QScopedPointer<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.data()));
std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
history->setMaxSize(10);
QCOMPARE(history->rowCount(), 0);
QVERIFY(!history->indexOf(QByteArrayLiteral("whatever")).isValid());
......@@ -221,8 +221,8 @@ void HistoryModelTest::testType_data()
void HistoryModelTest::testType()
{
QScopedPointer<HistoryModel> history(new HistoryModel(nullptr));
QScopedPointer<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.data()));
std::unique_ptr<HistoryModel> history(new HistoryModel(nullptr));
std::unique_ptr<QAbstractItemModelTester> modelTest(new QAbstractItemModelTester(history.get()));
history->setMaxSize(10);
QCOMPARE(history->rowCount(), 0);
......
......@@ -24,9 +24,9 @@ private Q_SLOTS:
void HistoryTest::testSetMaxSize()
{
QScopedPointer<History> history(new History(nullptr));
QSignalSpy changedSpy(history.data(), &History::changed);
QSignalSpy topSpy(history.data(), &History::topChanged);
std::unique_ptr<History> history(new History(nullptr));
QSignalSpy changedSpy(history.get(), &History::changed);
QSignalSpy topSpy(history.get(), &History::topChanged);
QVERIFY(history->empty());
QCOMPARE(history->maxSize(), 0u);
QVERIFY(changedSpy.isEmpty());
......@@ -73,9 +73,9 @@ void HistoryTest::testSetMaxSize()
void HistoryTest::testInsertRemove()
{
QScopedPointer<History> history(new History(nullptr));
QSignalSpy topSpy(history.data(), &History::topChanged);
QSignalSpy topUserSelectedSpy(history.data(), &History::topIsUserSelectedSet);
std::unique_ptr<History> history(new History(nullptr));
QSignalSpy topSpy(history.get(), &History::topChanged);
QSignalSpy topUserSelectedSpy(history.get(), &History::topIsUserSelectedSet);
history->setMaxSize(10);
QVERIFY(history->empty());
......@@ -198,7 +198,7 @@ void HistoryTest::testInsertRemove()
void HistoryTest::testClear()
{
QScopedPointer<History> history(new History(nullptr));
std::unique_ptr<History> history(new History(nullptr));
history->setMaxSize(10);
QVERIFY(history->empty());
QVERIFY(!history->topIsUserSelected());
......@@ -216,7 +216,7 @@ void HistoryTest::testClear()
QVERIFY(history->first());
// and clear
QSignalSpy topSpy(history.data(), &History::topChanged);
QSignalSpy topSpy(history.get(), &History::topChanged);
QVERIFY(topSpy.isEmpty());
history->slotClear();
QVERIFY(history->empty());
......@@ -227,7 +227,7 @@ void HistoryTest::testClear()
void HistoryTest::testFind()
{
QScopedPointer<History> history(new History(nullptr));
std::unique_ptr<History> history(new History(nullptr));
history->setMaxSize(10);
QVERIFY(history->empty());
QVERIFY(!history->find(QByteArrayLiteral("whatever")));
......@@ -246,8 +246,8 @@ void HistoryTest::testFind()
void HistoryTest::testCycle()
{
QScopedPointer<History> history(new History(nullptr));
QSignalSpy topSpy(history.data(), &History::topChanged);
std::unique_ptr<History> history(new History(nullptr));
QSignalSpy topSpy(history.get(), &History::topChanged);
history->setMaxSize(10);
QVERIFY(!history->nextInCycle());
QVERIFY(!history->prevInCycle());
......
......@@ -38,6 +38,7 @@
#include <KWayland/Client/surface.h>
#include <KWindowSystem>
#include "../c_ptr.h"
#include "configdialog.h"
#include "history.h"
#include "historyitem.h"
......@@ -721,8 +722,8 @@ bool Klipper::blockFetchingNewData()
}
xcb_connection_t *c = QX11Info::connection();
const xcb_query_pointer_cookie_t cookie = xcb_query_pointer_unchecked(c, QX11Info::appRootWindow());
QScopedPointer<xcb_query_pointer_reply_t, QScopedPointerPodDeleter> queryPointer(xcb_query_pointer_reply(c, cookie, nullptr));
if (queryPointer.isNull()) {
UniqueCPointer<xcb_query_pointer_reply_t> queryPointer(xcb_query_pointer_reply(c, cookie, nullptr));
if (!queryPointer) {
return false;
}
if (((queryPointer->mask & (XCB_KEY_BUT_MASK_SHIFT | XCB_KEY_BUT_MASK_BUTTON_1)) == XCB_KEY_BUT_MASK_SHIFT) // BUG: 85198
......@@ -989,7 +990,7 @@ protected:
}
private:
QScopedPointer<Prison::AbstractBarcode> m_barcode;
std::unique_ptr<Prison::AbstractBarcode> m_barcode;
};
void Klipper::showBarcode(const QSharedPointer<const HistoryItem> &item)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment