Skip to content

Commit d81a1a3

Browse files
committedAug 17, 2017
Return std::unique_ptrs where possible
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.
1 parent e325895 commit d81a1a3

12 files changed

+113
-122
lines changed
 

‎src/core/auth/qgsauthmanager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ bool QgsAuthManager::registerCoreAuthMethods()
782782
mAuthMethods.clear();
783783
Q_FOREACH ( const QString &authMethodKey, QgsAuthMethodRegistry::instance()->authMethodList() )
784784
{
785-
mAuthMethods.insert( authMethodKey, QgsAuthMethodRegistry::instance()->authMethod( authMethodKey ) );
785+
mAuthMethods.insert( authMethodKey, QgsAuthMethodRegistry::instance()->authMethod( authMethodKey ).release() );
786786
}
787787

788788
return !mAuthMethods.isEmpty();

‎src/core/auth/qgsauthmethodregistry.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void QgsAuthMethodRegistry::setLibraryDirectory( const QDir &path )
260260
// typedef for the QgsDataProvider class factory
261261
typedef QgsAuthMethod *classFactoryFunction_t();
262262

263-
QgsAuthMethod *QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
263+
std::unique_ptr<QgsAuthMethod> QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
264264
{
265265
// load the plugin
266266
QString lib = library( authMethodKey );
@@ -299,7 +299,7 @@ QgsAuthMethod *QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
299299
return nullptr;
300300
}
301301

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

345-
QLibrary *QgsAuthMethodRegistry::authMethodLibrary( const QString &authMethodKey ) const
345+
std::unique_ptr<QLibrary> QgsAuthMethodRegistry::authMethodLibrary( const QString &authMethodKey ) const
346346
{
347-
QLibrary *myLib = new QLibrary( library( authMethodKey ) );
347+
std::unique_ptr< QLibrary > myLib( new QLibrary( library( authMethodKey ) ) );
348348

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

351351
if ( myLib->load() )
352352
return myLib;
353353

354354
QgsDebugMsg( "Cannot load library: " + myLib->errorString() );
355-
356-
delete myLib;
357-
358355
return nullptr;
359356
}
360357

‎src/core/auth/qgsauthmethodregistry.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QLibrary>
2424
#include <QMap>
2525
#include <QString>
26+
#include <memory>
2627

2728
#include "qgis_core.h"
2829

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

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

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

9495
//! Return list of available auth methods by their keys
9596
QStringList authMethodList() const;

‎src/core/geometry/qgsgeometry.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,70 +134,70 @@ bool QgsGeometry::isNull() const
134134

135135
QgsGeometry QgsGeometry::fromWkt( const QString &wkt )
136136
{
137-
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkt( wkt );
137+
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::geomFromWkt( wkt );
138138
if ( !geom )
139139
{
140140
return QgsGeometry();
141141
}
142-
return QgsGeometry( geom );
142+
return QgsGeometry( geom.release() );
143143
}
144144

145145
QgsGeometry QgsGeometry::fromPoint( const QgsPointXY &point )
146146
{
147-
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPoint( point );
147+
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::fromPoint( point ) );
148148
if ( geom )
149149
{
150-
return QgsGeometry( geom );
150+
return QgsGeometry( geom.release() );
151151
}
152152
return QgsGeometry();
153153
}
154154

155155
QgsGeometry QgsGeometry::fromPolyline( const QgsPolyline &polyline )
156156
{
157-
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPolyline( polyline );
157+
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromPolyline( polyline );
158158
if ( geom )
159159
{
160-
return QgsGeometry( geom );
160+
return QgsGeometry( geom.release() );
161161
}
162162
return QgsGeometry();
163163
}
164164

165165
QgsGeometry QgsGeometry::fromPolygon( const QgsPolygon &polygon )
166166
{
167-
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPolygon( polygon );
167+
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromPolygon( polygon );
168168
if ( geom )
169169
{
170-
return QgsGeometry( geom );
170+
return QgsGeometry( geom.release() );
171171
}
172172
return QgsGeometry();
173173
}
174174

175175
QgsGeometry QgsGeometry::fromMultiPoint( const QgsMultiPoint &multipoint )
176176
{
177-
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPoint( multipoint );
177+
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPoint( multipoint );
178178
if ( geom )
179179
{
180-
return QgsGeometry( geom );
180+
return QgsGeometry( geom.release() );
181181
}
182182
return QgsGeometry();
183183
}
184184

185185
QgsGeometry QgsGeometry::fromMultiPolyline( const QgsMultiPolyline &multiline )
186186
{
187-
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPolyline( multiline );
187+
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPolyline( multiline );
188188
if ( geom )
189189
{
190-
return QgsGeometry( geom );
190+
return QgsGeometry( geom.release() );
191191
}
192192
return QgsGeometry();
193193
}
194194

195195
QgsGeometry QgsGeometry::fromMultiPolygon( const QgsMultiPolygon &multipoly )
196196
{
197-
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPolygon( multipoly );
197+
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPolygon( multipoly );
198198
if ( geom )
199199
{
200-
return QgsGeometry( geom );
200+
return QgsGeometry( geom.release() );
201201
}
202202
return QgsGeometry();
203203
}
@@ -246,7 +246,7 @@ void QgsGeometry::fromWkb( unsigned char *wkb, int length )
246246
delete d->geometry;
247247
}
248248
QgsConstWkbPtr ptr( wkb, length );
249-
d->geometry = QgsGeometryFactory::geomFromWkb( ptr );
249+
d->geometry = QgsGeometryFactory::geomFromWkb( ptr ).release();
250250
delete [] wkb;
251251
}
252252

@@ -259,7 +259,7 @@ void QgsGeometry::fromWkb( const QByteArray &wkb )
259259
delete d->geometry;
260260
}
261261
QgsConstWkbPtr ptr( wkb );
262-
d->geometry = QgsGeometryFactory::geomFromWkb( ptr );
262+
d->geometry = QgsGeometryFactory::geomFromWkb( ptr ).release();
263263
}
264264

265265
GEOSGeometry *QgsGeometry::exportToGeos( double precision ) const
@@ -1155,16 +1155,16 @@ bool QgsGeometry::convertToMultiType()
11551155
return true;
11561156
}
11571157

1158-
QgsGeometryCollection *multiGeom = qgsgeometry_cast<QgsGeometryCollection *>
1159-
( QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::multiType( d->geometry->wkbType() ) ) );
1158+
std::unique_ptr< QgsAbstractGeometry >geom = QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::multiType( d->geometry->wkbType() ) );
1159+
QgsGeometryCollection *multiGeom = qgsgeometry_cast<QgsGeometryCollection *>( geom.get() );
11601160
if ( !multiGeom )
11611161
{
11621162
return false;
11631163
}
11641164

11651165
detach( true );
11661166
multiGeom->addGeometry( d->geometry );
1167-
d->geometry = multiGeom;
1167+
d->geometry = geom.release();
11681168
return true;
11691169
}
11701170

@@ -2026,11 +2026,11 @@ int QgsGeometry::avoidIntersections( const QList<QgsVectorLayer *> &avoidInterse
20262026
return 1;
20272027
}
20282028

2029-
QgsAbstractGeometry *diffGeom = QgsGeometryEditUtils::avoidIntersections( *( d->geometry ), avoidIntersectionsLayers, ignoreFeatures );
2029+
std::unique_ptr< QgsAbstractGeometry > diffGeom = QgsGeometryEditUtils::avoidIntersections( *( d->geometry ), avoidIntersectionsLayers, ignoreFeatures );
20302030
if ( diffGeom )
20312031
{
20322032
detach( false );
2033-
d->geometry = diffGeom;
2033+
d->geometry = diffGeom.release();
20342034
}
20352035
return 0;
20362036
}

‎src/core/geometry/qgsgeometrycollection.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ bool QgsGeometryCollection::fromWkb( QgsConstWkbPtr &wkbPtr )
219219
mGeometries.clear();
220220
for ( int i = 0; i < nGeometries; ++i )
221221
{
222-
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkb( wkbPtr ); // also updates wkbPtr
222+
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::geomFromWkb( wkbPtr ) ); // also updates wkbPtr
223223
if ( geom )
224224
{
225-
if ( !addGeometry( geom ) )
225+
if ( !addGeometry( geom.release() ) )
226226
{
227227
qDeleteAll( mGeometries );
228228
mGeometries = geometryListBackup;
@@ -597,11 +597,10 @@ bool QgsGeometryCollection::hasCurvedSegments() const
597597

598598
QgsAbstractGeometry *QgsGeometryCollection::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
599599
{
600-
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkbType( mWkbType );
601-
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( geom );
600+
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::geomFromWkbType( mWkbType ) );
601+
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( geom.get() );
602602
if ( !geomCollection )
603603
{
604-
delete geom;
605604
return clone();
606605
}
607606

@@ -610,7 +609,7 @@ QgsAbstractGeometry *QgsGeometryCollection::segmentize( double tolerance, Segmen
610609
{
611610
geomCollection->addGeometry( ( *geomIt )->segmentize( tolerance, toleranceType ) );
612611
}
613-
return geomCollection;
612+
return geom.release();
614613
}
615614

616615
double QgsGeometryCollection::vertexAngle( QgsVertexId vertex ) const

‎src/core/geometry/qgsgeometryeditutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ bool QgsGeometryEditUtils::deletePart( QgsAbstractGeometry *geom, int partNum )
221221
return c->removeGeometry( partNum );
222222
}
223223

224-
QgsAbstractGeometry *QgsGeometryEditUtils::avoidIntersections( const QgsAbstractGeometry &geom,
224+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryEditUtils::avoidIntersections( const QgsAbstractGeometry &geom,
225225
const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
226226
QHash<QgsVectorLayer *, QSet<QgsFeatureId> > ignoreFeatures )
227227
{
@@ -281,7 +281,7 @@ QgsAbstractGeometry *QgsGeometryEditUtils::avoidIntersections( const QgsAbstract
281281
return nullptr;
282282
}
283283

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

286286
delete combinedGeometries;
287287
return diffGeom;

‎src/core/geometry/qgsgeometryeditutils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class QgsVectorLayer;
2626
#include "qgsfeature.h"
2727
#include "qgsgeometry.h"
2828
#include <QMap>
29+
#include <memory>
2930

3031
/** \ingroup core
3132
* \class QgsGeometryEditUtils
@@ -69,7 +70,7 @@ class QgsGeometryEditUtils
6970
* \param avoidIntersectionsLayers list of layers to check for intersections
7071
* \param ignoreFeatures map of layer to feature id of features to ignore
7172
*/
72-
static QgsAbstractGeometry *avoidIntersections( const QgsAbstractGeometry &geom,
73+
static std::unique_ptr< QgsAbstractGeometry > avoidIntersections( const QgsAbstractGeometry &geom,
7374
const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
7475
QHash<QgsVectorLayer *, QSet<QgsFeatureId> > ignoreFeatures = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
7576
};

0 commit comments

Comments
 (0)
Please sign in to comment.