Skip to content

Commit

Permalink
core: replace QPointer with shared_ptr/weak_ptr (#48110)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkarneges authored Feb 4, 2025
1 parent 93a1ac5 commit 865ade8
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 79 deletions.
23 changes: 7 additions & 16 deletions src/core/qzmq/src/qzmqsocket.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2012-2020 Justin Karneges
* Copyright (C) 2024 Fastly, Inc.
* Copyright (C) 2024-2025 Fastly, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
Expand All @@ -27,7 +27,6 @@
#include <stdio.h>
#include <assert.h>
#include <QStringList>
#include <QPointer>
#include <QMutex>
#include <boost/signals2.hpp>
#include "rust/bindings.h"
Expand Down Expand Up @@ -362,10 +361,8 @@ static void removeGlobalContextRef()
}
}

class Socket::Private : public QObject
class Socket::Private
{
Q_OBJECT

public:
Socket *q;
bool usingGlobalContext;
Expand All @@ -382,7 +379,6 @@ class Socket::Private : public QObject
bool writeQueueEnabled;

Private(Socket *_q, Socket::Type type, Context *_context) :
QObject(_q),
q(_q),
canWrite(false),
canRead(false),
Expand Down Expand Up @@ -599,9 +595,9 @@ class Socket::Private : public QObject

if(canRead)
{
QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;
q->readyRead();
if(!self)
if(self.expired())
return;
}

Expand Down Expand Up @@ -639,19 +635,16 @@ class Socket::Private : public QObject
Socket::Socket(Type type, QObject *parent) :
QObject(parent)
{
d = new Private(this, type, 0);
d = std::make_shared<Private>(this, type, nullptr);
}

Socket::Socket(Type type, Context *context, QObject *parent) :
QObject(parent)
{
d = new Private(this, type, context);
d = std::make_shared<Private>(this, type, context);
}

Socket::~Socket()
{
delete d;
}
Socket::~Socket() = default;

void Socket::setShutdownWaitTime(int msecs)
{
Expand Down Expand Up @@ -773,5 +766,3 @@ void Socket::write(const QList<QByteArray> &message)
}

}

#include "qzmqsocket.moc"
4 changes: 2 additions & 2 deletions src/core/qzmq/src/qzmqsocket.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2012-2015 Justin Karneges
* Copyright (C) 2024 Fastly, Inc.
* Copyright (C) 2024-2025 Fastly, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
Expand Down Expand Up @@ -113,7 +113,7 @@ class Socket : public QObject

class Private;
friend class Private;
Private *d;
std::shared_ptr<Private> d;
};

}
Expand Down
19 changes: 5 additions & 14 deletions src/core/qzmq/src/qzmqvalve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@

#include "qzmqvalve.h"

#include <QPointer>
#include "qzmqsocket.h"
#include "defercall.h"

namespace QZmq {

class Valve::Private : public QObject
class Valve::Private
{
Q_OBJECT

public:
Valve *q;
QZmq::Socket *sock;
Expand All @@ -44,7 +41,6 @@ class Valve::Private : public QObject
DeferCall deferCall;

Private(Valve *_q) :
QObject(_q),
q(_q),
sock(0),
isOpen(false),
Expand All @@ -70,7 +66,7 @@ class Valve::Private : public QObject

void tryRead()
{
QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;

int count = 0;
while(isOpen && sock->canRead())
Expand All @@ -86,7 +82,7 @@ class Valve::Private : public QObject
if(!msg.isEmpty())
{
q->readyRead(msg);
if(!self)
if(self.expired())
return;
}

Expand All @@ -112,14 +108,11 @@ class Valve::Private : public QObject
Valve::Valve(QZmq::Socket *sock, QObject *parent) :
QObject(parent)
{
d = new Private(this);
d = std::make_shared<Private>(this);
d->setup(sock);
}

Valve::~Valve()
{
delete d;
}
Valve::~Valve() = default;

bool Valve::isOpen() const
{
Expand Down Expand Up @@ -147,5 +140,3 @@ void Valve::close()
}

}

#include "qzmqvalve.moc"
3 changes: 2 additions & 1 deletion src/core/qzmq/src/qzmqvalve.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2012 Justin Karneges
* Copyright (C) 2025 Fastly, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
Expand Down Expand Up @@ -54,7 +55,7 @@ class Valve : public QObject
private:
class Private;
friend class Private;
Private *d;
std::shared_ptr<Private> d;
};

}
Expand Down
1 change: 0 additions & 1 deletion src/core/statsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <assert.h>
#include <QVector>
#include <QDateTime>
#include <QPointer>
#include <QJsonDocument>
#include <QJsonObject>
#include "qzmqsocket.h"
Expand Down
25 changes: 11 additions & 14 deletions src/core/zhttpmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2012-2021 Fanout, Inc.
* Copyright (C) 2025 Fastly, Inc.
*
* This file is part of Pushpin.
*
Expand All @@ -25,7 +26,6 @@
#include <assert.h>
#include <QStringList>
#include <QHash>
#include <QPointer>
#include "qzmqsocket.h"
#include "qzmqvalve.h"
#include "tnetstring.h"
Expand Down Expand Up @@ -516,7 +516,7 @@ class ZhttpManager::Private : public QObject

void client_req_readyRead()
{
QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;

while(client_req_sock->canRead())
{
Expand Down Expand Up @@ -563,7 +563,7 @@ class ZhttpManager::Private : public QObject
if(req)
{
req->handle(id.id, id.seq, p);
if(!self)
if(self.expired())
return;

continue;
Expand Down Expand Up @@ -605,7 +605,7 @@ class ZhttpManager::Private : public QObject
return;
}

QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;

foreach(const ZhttpResponsePacket::Id &id, p.ids)
{
Expand All @@ -614,7 +614,7 @@ class ZhttpManager::Private : public QObject
if(sock)
{
sock->handle(id.id, id.seq, p);
if(!self)
if(self.expired())
return;

continue;
Expand All @@ -625,7 +625,7 @@ class ZhttpManager::Private : public QObject
if(req)
{
req->handle(id.id, id.seq, p);
if(!self)
if(self.expired())
return;

continue;
Expand Down Expand Up @@ -805,7 +805,7 @@ class ZhttpManager::Private : public QObject
return;
}

QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;

foreach(const ZhttpRequestPacket::Id &id, p.ids)
{
Expand All @@ -814,7 +814,7 @@ class ZhttpManager::Private : public QObject
if(sock)
{
sock->handle(id.id, id.seq, p);
if(!self)
if(self.expired())
return;

continue;
Expand All @@ -825,7 +825,7 @@ class ZhttpManager::Private : public QObject
if(req)
{
req->handle(id.id, id.seq, p);
if(!self)
if(self.expired())
return;

continue;
Expand Down Expand Up @@ -982,13 +982,10 @@ class ZhttpManager::Private : public QObject
ZhttpManager::ZhttpManager(QObject *parent) :
QObject(parent)
{
d = new Private(this);
d = std::make_shared<Private>(this);
}

ZhttpManager::~ZhttpManager()
{
delete d;
}
ZhttpManager::~ZhttpManager() = default;

int ZhttpManager::connectionCount() const
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/zhttpmanager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2012-2013 Fanout, Inc.
* Copyright (C) 2025 Fastly, Inc.
*
* This file is part of Pushpin.
*
Expand Down Expand Up @@ -79,7 +80,7 @@ class ZhttpManager : public QObject
private:
class Private;
friend class Private;
Private *d;
std::shared_ptr<Private> d;

friend class ZhttpRequest;
friend class ZWebSocket;
Expand Down
24 changes: 10 additions & 14 deletions src/core/zhttprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "zhttprequest.h"

#include <assert.h>
#include <QPointer>
#include "zhttprequestpacket.h"
#include "zhttpresponsepacket.h"
#include "bufferlist.h"
Expand Down Expand Up @@ -411,7 +410,7 @@ class ZhttpRequest::Private : public QObject

void tryWrite()
{
QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;

if(state == ClientRequesting)
{
Expand Down Expand Up @@ -494,7 +493,7 @@ class ZhttpRequest::Private : public QObject
}
}

if(!self)
if(self.expired())
return;

trySendPause();
Expand Down Expand Up @@ -1045,9 +1044,9 @@ class ZhttpRequest::Private : public QObject
}
else if(state == ClientRequesting)
{
QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;
tryWrite();
if(!self)
if(self.expired())
return;

if(writableChanged)
Expand Down Expand Up @@ -1131,23 +1130,23 @@ class ZhttpRequest::Private : public QObject
cleanup();
}

QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;

if(!packet.body.isEmpty())
q->bytesWritten(packet.body.size());
else if(!packet.more)
q->bytesWritten(0);

if(!self)
if(self.expired())
return;

trySendPause();
}
else if(state == ServerResponding)
{
QPointer<QObject> self = this;
std::weak_ptr<Private> self = q->d;
tryWrite();
if(!self)
if(self.expired())
return;

if(writableChanged)
Expand Down Expand Up @@ -1187,13 +1186,10 @@ class ZhttpRequest::Private : public QObject
ZhttpRequest::ZhttpRequest(QObject *parent) :
HttpRequest(parent)
{
d = new Private(this);
d = std::make_shared<Private>(this);
}

ZhttpRequest::~ZhttpRequest()
{
delete d;
}
ZhttpRequest::~ZhttpRequest() = default;

ZhttpRequest::Rid ZhttpRequest::rid() const
{
Expand Down
Loading

0 comments on commit 865ade8

Please sign in to comment.