Skip to content

Commit

Permalink
Merge pull request #4751 from nyalldawson/unique_ptr
Browse files Browse the repository at this point in the history
Return std::unique_ptrs where possible
  • Loading branch information
nyalldawson committed Aug 17, 2017
2 parents fad5973 + e0d1ddc commit 26a911c
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 131 deletions.
2 changes: 1 addition & 1 deletion src/core/auth/qgsauthmanager.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ bool QgsAuthManager::registerCoreAuthMethods()
mAuthMethods.clear(); mAuthMethods.clear();
Q_FOREACH ( const QString &authMethodKey, QgsAuthMethodRegistry::instance()->authMethodList() ) 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(); return !mAuthMethods.isEmpty();
Expand Down
11 changes: 4 additions & 7 deletions src/core/auth/qgsauthmethodregistry.cpp
Original file line number Original file line 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 for the QgsDataProvider class factory
typedef QgsAuthMethod *classFactoryFunction_t(); typedef QgsAuthMethod *classFactoryFunction_t();


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


QgsAuthMethod *authMethod = classFactory(); std::unique_ptr< QgsAuthMethod > authMethod( classFactory() );
if ( !authMethod ) if ( !authMethod )
{ {
QgsMessageLog::logMessage( QObject::tr( "Unable to instantiate the auth method plugin %1" ).arg( lib ) ); 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() ); QgsDebugMsg( "Library name is " + myLib->fileName() );


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


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

delete myLib;

return nullptr; return nullptr;
} }


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


#include "qgis_core.h" #include "qgis_core.h"


Expand Down Expand Up @@ -67,7 +68,7 @@ class CORE_EXPORT QgsAuthMethodRegistry
\param authMethodKey identificator of the auth method \param authMethodKey identificator of the auth method
\returns instance of auth method or nullptr on error \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 /** Return the auth method capabilities
\param authMethodKey identificator of the auth method \param authMethodKey identificator of the auth method
Expand All @@ -89,7 +90,7 @@ class CORE_EXPORT QgsAuthMethodRegistry
const QString &functionName ); const QString &functionName );


//! Return the library object associated with an auth method key //! 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 //! Return list of available auth methods by their keys
QStringList authMethodList() const; QStringList authMethodList() const;
Expand Down
42 changes: 21 additions & 21 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -134,70 +134,70 @@ bool QgsGeometry::isNull() const


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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

0 comments on commit 26a911c

Please sign in to comment.