Skip to content

Commit

Permalink
Use unique_ptr instead of raw pointers in many places
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed Mar 15, 2024
1 parent 283bee6 commit 5049a2a
Show file tree
Hide file tree
Showing 22 changed files with 63 additions and 107 deletions.
13 changes: 6 additions & 7 deletions src/base/QXmppLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,24 @@ QXmppLoggerPrivate::QXmppLoggerPrivate()
{
}

///
/// Constructs a new QXmppLogger.
///
/// \param parent

///
QXmppLogger::QXmppLogger(QObject *parent)
: QObject(parent), d(new QXmppLoggerPrivate())
: QObject(parent),
d(std::make_unique<QXmppLoggerPrivate>())
{
// make it possible to pass QXmppLogger::MessageType between threads
qRegisterMetaType<QXmppLogger::MessageType>("QXmppLogger::MessageType");
}

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

///
/// Returns the default logger.
///

QXmppLogger *QXmppLogger::getLogger()
{
if (!m_logger) {
Expand Down
4 changes: 3 additions & 1 deletion src/base/QXmppLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "QXmppGlobal.h"

#include <memory>

#include <QObject>

#ifdef QXMPP_LOGGABLE_TRACE
Expand Down Expand Up @@ -94,7 +96,7 @@ public Q_SLOTS:

private:
static QXmppLogger *m_logger;
QXmppLoggerPrivate *d;
const std::unique_ptr<QXmppLoggerPrivate> d;
};

/// \brief The QXmppLoggable class represents a source of logging messages.
Expand Down
16 changes: 6 additions & 10 deletions src/base/QXmppSasl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,12 @@ class QXmppSaslClientPrivate
};

QXmppSaslClient::QXmppSaslClient(QObject *parent)
: QXmppLoggable(parent), d(new QXmppSaslClientPrivate)
: QXmppLoggable(parent),
d(std::make_unique<QXmppSaslClientPrivate>())
{
}

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

///
/// Returns a list of supported mechanisms.
Expand Down Expand Up @@ -655,14 +653,12 @@ class QXmppSaslServerPrivate
};

QXmppSaslServer::QXmppSaslServer(QObject *parent)
: QXmppLoggable(parent), d(new QXmppSaslServerPrivate)
: QXmppLoggable(parent),
d(std::make_unique<QXmppSaslServerPrivate>())
{
}

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

/// Creates an SASL server for the given mechanism.
QXmppSaslServer *QXmppSaslServer::create(const QString &mechanism, QObject *parent)
Expand Down
4 changes: 2 additions & 2 deletions src/base/QXmppSasl_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class QXMPP_AUTOTEST_EXPORT QXmppSaslClient : public QXmppLoggable
static QXmppSaslClient *create(const QString &mechanism, QObject *parent = nullptr);

private:
QXmppSaslClientPrivate *d;
const std::unique_ptr<QXmppSaslClientPrivate> d;
};

class QXMPP_AUTOTEST_EXPORT QXmppSaslServer : public QXmppLoggable
Expand Down Expand Up @@ -89,7 +89,7 @@ class QXMPP_AUTOTEST_EXPORT QXmppSaslServer : public QXmppLoggable
static QXmppSaslServer *create(const QString &mechanism, QObject *parent = nullptr);

private:
QXmppSaslServerPrivate *d;
const std::unique_ptr<QXmppSaslServerPrivate> d;
};

class QXMPP_AUTOTEST_EXPORT QXmppSaslDigestMd5
Expand Down
3 changes: 1 addition & 2 deletions src/base/QXmppStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ QXmppStreamPrivate::~QXmppStreamPrivate()
///
QXmppStream::QXmppStream(QObject *parent)
: QXmppLoggable(parent),
d(new QXmppStreamPrivate(this))
d(std::make_unique<QXmppStreamPrivate>(this))
{
}

Expand All @@ -93,7 +93,6 @@ QXmppStream::QXmppStream(QObject *parent)
QXmppStream::~QXmppStream()
{
cancelOngoingIqs();
delete d;
}

///
Expand Down
2 changes: 1 addition & 1 deletion src/base/QXmppStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private Q_SLOTS:
void processData(const QString &data);
bool handleIqResponse(const QDomElement &);

QXmppStreamPrivate *const d;
const std::unique_ptr<QXmppStreamPrivate> d;
};

#endif // QXMPPSTREAM_H
18 changes: 6 additions & 12 deletions src/base/QXmppStun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,11 +2003,9 @@ void QXmppIceComponentPrivate::writeStun(const QXmppStunMessage &message, QXmppI
/// Constructs a new QXmppIceComponent.
///
QXmppIceComponent::QXmppIceComponent(int component, QXmppIcePrivate *config, QObject *parent)
: QXmppLoggable(parent)
: QXmppLoggable(parent),
d(std::make_unique<QXmppIceComponentPrivate>(component, config, this))
{

d = new QXmppIceComponentPrivate(component, config, this);

d->timer = new QTimer(this);
d->timer->setInterval(500);
connect(d->timer, &QTimer::timeout,
Expand Down Expand Up @@ -2037,7 +2035,6 @@ QXmppIceComponent::QXmppIceComponent(int component, QXmppIcePrivate *config, QOb
QXmppIceComponent::~QXmppIceComponent()
{
qDeleteAll(d->pairs);
delete d;
}

///
Expand Down Expand Up @@ -2579,9 +2576,9 @@ QXmppIceConnectionPrivate::QXmppIceConnectionPrivate()
/// \param parent
///
QXmppIceConnection::QXmppIceConnection(QObject *parent)
: QXmppLoggable(parent), d(new QXmppIceConnectionPrivate())
: QXmppLoggable(parent),
d(std::make_unique<QXmppIceConnectionPrivate>())
{

// timer to limit connection time to 30 seconds
d->connectTimer = new QTimer(this);
d->connectTimer->setInterval(30000);
Expand All @@ -2590,10 +2587,7 @@ QXmppIceConnection::QXmppIceConnection(QObject *parent)
this, &QXmppIceConnection::slotTimeout);
}

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

///
/// Returns the given component of this ICE connection.
Expand All @@ -2619,7 +2613,7 @@ void QXmppIceConnection::addComponent(int component)
return;
}

auto *socket = new QXmppIceComponent(component, d, this);
auto *socket = new QXmppIceComponent(component, d.get(), this);
socket->d->setTurnServer(d->turnHost, d->turnPort);
socket->d->setTurnUser(d->turnUser);
socket->d->setTurnPassword(d->turnPassword);
Expand Down
4 changes: 2 additions & 2 deletions src/base/QXmppStun.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private Q_SLOTS:
private:
QXmppIceComponent(int component, QXmppIcePrivate *config, QObject *parent = nullptr);

QXmppIceComponentPrivate *d;
const std::unique_ptr<QXmppIceComponentPrivate> d;
friend class QXmppIceComponentPrivate;
friend class QXmppIceConnection;
};
Expand Down Expand Up @@ -304,7 +304,7 @@ private Q_SLOTS:
void slotTimeout();

private:
QXmppIceConnectionPrivate *d;
const std::unique_ptr<QXmppIceConnectionPrivate> d;
};

#endif
9 changes: 3 additions & 6 deletions src/client/QXmppCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,19 +555,16 @@ void QXmppCallPrivate::terminate(QXmppJingleIq::Reason::Type reasonType)
///

QXmppCall::QXmppCall(const QString &jid, QXmppCall::Direction direction, QXmppCallManager *parent)
: QXmppLoggable(parent)
: QXmppLoggable(parent),
d(std::make_unique<QXmppCallPrivate>(this))
{
d = new QXmppCallPrivate(this);
d->direction = direction;
d->jid = jid;
d->ownJid = parent->client()->configuration().jid();
d->manager = parent;
}

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

///
/// Call this method if you wish to accept an incoming call.
Expand Down
2 changes: 1 addition & 1 deletion src/client/QXmppCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class QXMPP_EXPORT QXmppCall : public QXmppLoggable

QXmppCall(const QString &jid, QXmppCall::Direction direction, QXmppCallManager *parent);

QXmppCallPrivate *d;
const std::unique_ptr<QXmppCallPrivate> d;
friend class QXmppCallManager;
friend class QXmppCallManagerPrivate;
friend class QXmppCallPrivate;
Expand Down
7 changes: 2 additions & 5 deletions src/client/QXmppCallManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ QXmppCall *QXmppCallManagerPrivate::findCall(const QString &sid, QXmppCall::Dire
/// Voice-Over-IP calls.
///
QXmppCallManager::QXmppCallManager()
: d(std::make_unique<QXmppCallManagerPrivate>(this))
{
d = new QXmppCallManagerPrivate(this);
}

///
/// Destroys the QXmppCallManager object.
///
QXmppCallManager::~QXmppCallManager()
{
delete d;
}
QXmppCallManager::~QXmppCallManager() = default;

/// \cond
QStringList QXmppCallManager::discoveryFeatures() const
Expand Down
2 changes: 1 addition & 1 deletion src/client/QXmppCallManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private Q_SLOTS:
void _q_presenceReceived(const QXmppPresence &presence);

private:
QXmppCallManagerPrivate *d;
const std::unique_ptr<QXmppCallManagerPrivate> d;
friend class QXmppCall;
friend class QXmppCallPrivate;
friend class QXmppCallManagerPrivate;
Expand Down
17 changes: 5 additions & 12 deletions src/server/QXmppIncomingClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,17 @@ QString QXmppIncomingClientPrivate::origin() const
}
}

///
/// Constructs a new incoming client stream.
///
/// \param socket The socket for the XMPP stream.
/// \param domain The local domain.
/// \param parent The parent QObject for the stream (optional).
///

QXmppIncomingClient::QXmppIncomingClient(QSslSocket *socket, const QString &domain, QObject *parent)
: QXmppStream(parent)
: QXmppStream(parent),
d(std::make_unique<QXmppIncomingClientPrivate>(this))
{

d = new QXmppIncomingClientPrivate(this);
d->domain = domain;

if (socket) {
Expand All @@ -107,18 +106,12 @@ QXmppIncomingClient::QXmppIncomingClient(QSslSocket *socket, const QString &doma
this, &QXmppIncomingClient::onTimeout);
}

/// Destroys the current stream.
///

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

///
/// Returns true if the socket is connected, the client is authenticated
/// and a resource is bound.
///

bool QXmppIncomingClient::isConnected() const
{
return QXmppStream::isConnected() &&
Expand Down
3 changes: 1 addition & 2 deletions src/server/QXmppIncomingClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ private Q_SLOTS:
void onTimeout();

private:
Q_DISABLE_COPY(QXmppIncomingClient)
QXmppIncomingClientPrivate *d;
const std::unique_ptr<QXmppIncomingClientPrivate> d;
friend class QXmppIncomingClientPrivate;
};

Expand Down
16 changes: 5 additions & 11 deletions src/server/QXmppIncomingServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,17 @@ QString QXmppIncomingServerPrivate::origin() const
}
}

///
/// Constructs a new incoming server stream.
///
/// \param socket The socket for the XMPP stream.
/// \param domain The local domain.
/// \param parent The parent QObject for the stream (optional).
///

QXmppIncomingServer::QXmppIncomingServer(QSslSocket *socket, const QString &domain, QObject *parent)
: QXmppStream(parent)
: QXmppStream(parent),
d(std::make_unique<QXmppIncomingServerPrivate>(this))
{

d = new QXmppIncomingServerPrivate(this);
d->domain = domain;

if (socket) {
Expand All @@ -69,16 +68,11 @@ QXmppIncomingServer::QXmppIncomingServer(QSslSocket *socket, const QString &doma
info(QStringLiteral("Incoming server connection from %1").arg(d->origin()));
}

/// Destroys the current stream.

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

///
/// Returns the stream's identifier.
///

QString QXmppIncomingServer::localStreamId() const
{
return d->localStreamId;
Expand Down
3 changes: 1 addition & 2 deletions src/server/QXmppIncomingServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ private Q_SLOTS:
void slotSocketDisconnected();

private:
Q_DISABLE_COPY(QXmppIncomingServer)
QXmppIncomingServerPrivate *d;
const std::unique_ptr<QXmppIncomingServerPrivate> d;
friend class QXmppIncomingServerPrivate;
};

Expand Down
Loading

0 comments on commit 5049a2a

Please sign in to comment.