Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Modernize memory management
(cherry picked from commit 08fbe21)
  • Loading branch information
nyalldawson committed Jan 14, 2020
1 parent 1e6ebbe commit 1ad3c2a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/core/gps/qgsgpsconnection.cpp
Expand Up @@ -68,7 +68,9 @@ QgsGpsInformation::FixStatus QgsGpsInformation::fixStatus() const
}


QgsGpsConnection::QgsGpsConnection( QIODevice *dev ): QObject( nullptr ), mSource( dev ), mStatus( NotConnected )
QgsGpsConnection::QgsGpsConnection( QIODevice *dev )
: QObject( nullptr )
, mSource( dev )
{
clearLastGPSInformation();
QObject::connect( dev, &QIODevice::readyRead, this, &QgsGpsConnection::parseData );
Expand Down Expand Up @@ -111,14 +113,13 @@ void QgsGpsConnection::cleanupSource()
{
mSource->close();
}
delete mSource;
mSource = nullptr;
mSource.reset();
}

void QgsGpsConnection::setSource( QIODevice *source )
{
cleanupSource();
mSource = source;
mSource.reset( source );
clearLastGPSInformation();
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/gps/qgsgpsconnection.h
Expand Up @@ -156,11 +156,11 @@ class CORE_EXPORT QgsGpsConnection : public QObject

protected:
//! Data source (e.g. serial device, socket, file,...)
QIODevice *mSource = nullptr;
std::unique_ptr< QIODevice > mSource;
//! Last state of the gps related variables (e.g. position, time, ...)
QgsGpsInformation mLastGPSInformation;
//! Connection status
Status mStatus;
Status mStatus = NotConnected;

private:
//! Closes and deletes mSource
Expand Down
8 changes: 4 additions & 4 deletions src/core/gps/qgsgpsdconnection.cpp
Expand Up @@ -24,7 +24,7 @@ QgsGpsdConnection::QgsGpsdConnection( const QString &host, qint16 port, const QS
: QgsNmeaConnection( new QTcpSocket() )
, mDevice( device )
{
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource.get() );

QObject::connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
QObject::connect( socket, SIGNAL( error( QAbstractSocket::SocketError ) ), this, SLOT( error( QAbstractSocket::SocketError ) ) );
Expand All @@ -33,15 +33,15 @@ QgsGpsdConnection::QgsGpsdConnection( const QString &host, qint16 port, const QS

void QgsGpsdConnection::connected()
{
QgsDebugMsg( QStringLiteral( "connected!" ) );
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
QgsDebugMsgLevel( QStringLiteral( "connected!" ), 2 );
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource.get() );
socket->write( QStringLiteral( "?WATCH={\"enable\":true,\"nmea\":true,\"raw\":true%1};" ).arg( mDevice.isEmpty() ? mDevice : QStringLiteral( ",\"device\":%1" ).arg( mDevice ) ).toUtf8() );
}

void QgsGpsdConnection::error( QAbstractSocket::SocketError socketError )
{
#ifdef QGISDEBUG
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource.get() );
QgsDebugMsg( QStringLiteral( "error: %1 %2" ).arg( socketError ).arg( socket->errorString() ) );
#else
Q_UNUSED( socketError )
Expand Down

0 comments on commit 1ad3c2a

Please sign in to comment.