Skip to content
Permalink
Browse files

Modernize memory management

(cherry picked from commit 08fbe21)
  • Loading branch information
nyalldawson committed Jan 7, 2020
1 parent 1e6ebbe commit 1ad3c2a033ae67983f42c5bbc42cfc6b639eb74c
Showing with 11 additions and 10 deletions.
  1. +5 −4 src/core/gps/qgsgpsconnection.cpp
  2. +2 −2 src/core/gps/qgsgpsconnection.h
  3. +4 −4 src/core/gps/qgsgpsdconnection.cpp
@@ -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 );
@@ -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();
}

@@ -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
@@ -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 ) ) );
@@ -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 )

0 comments on commit 1ad3c2a

Please sign in to comment.
You can’t perform that action at this time.