Skip to content

Commit

Permalink
Return std::unique_ptrs where possible
Browse files Browse the repository at this point in the history
When a class isn't exposed to Python, we should return a
std::unique_ptr whenever a returned pointer value is owned
by the caller.
  • Loading branch information
nyalldawson committed Aug 17, 2017
1 parent e325895 commit d81a1a3
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/core/auth/qgsauthmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ bool QgsAuthManager::registerCoreAuthMethods()
mAuthMethods.clear();
Q_FOREACH ( const QString &authMethodKey, QgsAuthMethodRegistry::instance()->authMethodList() )
{
mAuthMethods.insert( authMethodKey, QgsAuthMethodRegistry::instance()->authMethod( authMethodKey ) );
mAuthMethods.insert( authMethodKey, QgsAuthMethodRegistry::instance()->authMethod( authMethodKey ).release() );
}

return !mAuthMethods.isEmpty();
Expand Down
11 changes: 4 additions & 7 deletions src/core/auth/qgsauthmethodregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void QgsAuthMethodRegistry::setLibraryDirectory( const QDir &path )
// typedef for the QgsDataProvider class factory
typedef QgsAuthMethod *classFactoryFunction_t();

QgsAuthMethod *QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
std::unique_ptr<QgsAuthMethod> QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
{
// load the plugin
QString lib = library( authMethodKey );
Expand Down Expand Up @@ -299,7 +299,7 @@ QgsAuthMethod *QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
return nullptr;
}

QgsAuthMethod *authMethod = classFactory();
std::unique_ptr< QgsAuthMethod > authMethod( classFactory() );
if ( !authMethod )
{
QgsMessageLog::logMessage( QObject::tr( "Unable to instantiate the auth method plugin %1" ).arg( lib ) );
Expand Down Expand Up @@ -342,19 +342,16 @@ QFunctionPointer QgsAuthMethodRegistry::function( QString const &authMethodKey,
}
}

QLibrary *QgsAuthMethodRegistry::authMethodLibrary( const QString &authMethodKey ) const
std::unique_ptr<QLibrary> QgsAuthMethodRegistry::authMethodLibrary( const QString &authMethodKey ) const
{
QLibrary *myLib = new QLibrary( library( authMethodKey ) );
std::unique_ptr< QLibrary > myLib( new QLibrary( library( authMethodKey ) ) );

QgsDebugMsg( "Library name is " + myLib->fileName() );

if ( myLib->load() )
return myLib;

QgsDebugMsg( "Cannot load library: " + myLib->errorString() );

delete myLib;

return nullptr;
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/auth/qgsauthmethodregistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QLibrary>
#include <QMap>
#include <QString>
#include <memory>

#include "qgis_core.h"

Expand Down Expand Up @@ -67,7 +68,7 @@ class CORE_EXPORT QgsAuthMethodRegistry
\param authMethodKey identificator of the auth method
\returns instance of auth method or nullptr on error
*/
QgsAuthMethod *authMethod( const QString &authMethodKey );
std::unique_ptr< QgsAuthMethod > authMethod( const QString &authMethodKey );

/** Return the auth method capabilities
\param authMethodKey identificator of the auth method
Expand All @@ -89,7 +90,7 @@ class CORE_EXPORT QgsAuthMethodRegistry
const QString &functionName );

//! Return the library object associated with an auth method key
QLibrary *authMethodLibrary( const QString &authMethodKey ) const;
std::unique_ptr< QLibrary > authMethodLibrary( const QString &authMethodKey ) const;

//! Return list of available auth methods by their keys
QStringList authMethodList() const;
Expand Down
42 changes: 21 additions & 21 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,70 +134,70 @@ bool QgsGeometry::isNull() const

QgsGeometry QgsGeometry::fromWkt( const QString &wkt )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkt( wkt );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::geomFromWkt( wkt );
if ( !geom )
{
return QgsGeometry();
}
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}

QgsGeometry QgsGeometry::fromPoint( const QgsPointXY &point )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPoint( point );
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::fromPoint( point ) );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}

QgsGeometry QgsGeometry::fromPolyline( const QgsPolyline &polyline )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPolyline( polyline );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromPolyline( polyline );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}

QgsGeometry QgsGeometry::fromPolygon( const QgsPolygon &polygon )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPolygon( polygon );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromPolygon( polygon );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}

QgsGeometry QgsGeometry::fromMultiPoint( const QgsMultiPoint &multipoint )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPoint( multipoint );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPoint( multipoint );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}

QgsGeometry QgsGeometry::fromMultiPolyline( const QgsMultiPolyline &multiline )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPolyline( multiline );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPolyline( multiline );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}

QgsGeometry QgsGeometry::fromMultiPolygon( const QgsMultiPolygon &multipoly )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPolygon( multipoly );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPolygon( multipoly );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}
Expand Down Expand Up @@ -246,7 +246,7 @@ void QgsGeometry::fromWkb( unsigned char *wkb, int length )
delete d->geometry;
}
QgsConstWkbPtr ptr( wkb, length );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr ).release();
delete [] wkb;
}

Expand All @@ -259,7 +259,7 @@ void QgsGeometry::fromWkb( const QByteArray &wkb )
delete d->geometry;
}
QgsConstWkbPtr ptr( wkb );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr ).release();
}

GEOSGeometry *QgsGeometry::exportToGeos( double precision ) const
Expand Down Expand Up @@ -1155,16 +1155,16 @@ bool QgsGeometry::convertToMultiType()
return true;
}

QgsGeometryCollection *multiGeom = qgsgeometry_cast<QgsGeometryCollection *>
( QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::multiType( d->geometry->wkbType() ) ) );
std::unique_ptr< QgsAbstractGeometry >geom = QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::multiType( d->geometry->wkbType() ) );
QgsGeometryCollection *multiGeom = qgsgeometry_cast<QgsGeometryCollection *>( geom.get() );
if ( !multiGeom )
{
return false;
}

detach( true );
multiGeom->addGeometry( d->geometry );
d->geometry = multiGeom;
d->geometry = geom.release();
return true;
}

Expand Down Expand Up @@ -2026,11 +2026,11 @@ int QgsGeometry::avoidIntersections( const QList<QgsVectorLayer *> &avoidInterse
return 1;
}

QgsAbstractGeometry *diffGeom = QgsGeometryEditUtils::avoidIntersections( *( d->geometry ), avoidIntersectionsLayers, ignoreFeatures );
std::unique_ptr< QgsAbstractGeometry > diffGeom = QgsGeometryEditUtils::avoidIntersections( *( d->geometry ), avoidIntersectionsLayers, ignoreFeatures );
if ( diffGeom )
{
detach( false );
d->geometry = diffGeom;
d->geometry = diffGeom.release();
}
return 0;
}
Expand Down
11 changes: 5 additions & 6 deletions src/core/geometry/qgsgeometrycollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ bool QgsGeometryCollection::fromWkb( QgsConstWkbPtr &wkbPtr )
mGeometries.clear();
for ( int i = 0; i < nGeometries; ++i )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkb( wkbPtr ); // also updates wkbPtr
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::geomFromWkb( wkbPtr ) ); // also updates wkbPtr
if ( geom )
{
if ( !addGeometry( geom ) )
if ( !addGeometry( geom.release() ) )
{
qDeleteAll( mGeometries );
mGeometries = geometryListBackup;
Expand Down Expand Up @@ -597,11 +597,10 @@ bool QgsGeometryCollection::hasCurvedSegments() const

QgsAbstractGeometry *QgsGeometryCollection::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
{
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkbType( mWkbType );
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( geom );
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::geomFromWkbType( mWkbType ) );
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( geom.get() );
if ( !geomCollection )
{
delete geom;
return clone();
}

Expand All @@ -610,7 +609,7 @@ QgsAbstractGeometry *QgsGeometryCollection::segmentize( double tolerance, Segmen
{
geomCollection->addGeometry( ( *geomIt )->segmentize( tolerance, toleranceType ) );
}
return geomCollection;
return geom.release();
}

double QgsGeometryCollection::vertexAngle( QgsVertexId vertex ) const
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeometryeditutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ bool QgsGeometryEditUtils::deletePart( QgsAbstractGeometry *geom, int partNum )
return c->removeGeometry( partNum );
}

QgsAbstractGeometry *QgsGeometryEditUtils::avoidIntersections( const QgsAbstractGeometry &geom,
std::unique_ptr<QgsAbstractGeometry> QgsGeometryEditUtils::avoidIntersections( const QgsAbstractGeometry &geom,
const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
QHash<QgsVectorLayer *, QSet<QgsFeatureId> > ignoreFeatures )
{
Expand Down Expand Up @@ -281,7 +281,7 @@ QgsAbstractGeometry *QgsGeometryEditUtils::avoidIntersections( const QgsAbstract
return nullptr;
}

QgsAbstractGeometry *diffGeom = geomEngine->difference( combinedGeometries );
std::unique_ptr< QgsAbstractGeometry > diffGeom( geomEngine->difference( combinedGeometries ) );

delete combinedGeometries;
return diffGeom;
Expand Down
3 changes: 2 additions & 1 deletion src/core/geometry/qgsgeometryeditutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class QgsVectorLayer;
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include <QMap>
#include <memory>

/** \ingroup core
* \class QgsGeometryEditUtils
Expand Down Expand Up @@ -69,7 +70,7 @@ class QgsGeometryEditUtils
* \param avoidIntersectionsLayers list of layers to check for intersections
* \param ignoreFeatures map of layer to feature id of features to ignore
*/
static QgsAbstractGeometry *avoidIntersections( const QgsAbstractGeometry &geom,
static std::unique_ptr< QgsAbstractGeometry > avoidIntersections( const QgsAbstractGeometry &geom,
const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
QHash<QgsVectorLayer *, QSet<QgsFeatureId> > ignoreFeatures = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
};
Expand Down
Loading

0 comments on commit d81a1a3

Please sign in to comment.