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
};

‎src/core/geometry/qgsgeometryfactory.cpp

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "qgswkbtypes.h"
3131
#include "qgslogger.h"
3232

33-
QgsAbstractGeometry *QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
33+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
3434
{
3535
if ( !wkbPtr )
3636
return nullptr;
@@ -49,9 +49,7 @@ QgsAbstractGeometry *QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
4949
}
5050
wkbPtr -= 1 + sizeof( int );
5151

52-
QgsAbstractGeometry *geom = nullptr;
53-
54-
geom = geomFromWkbType( type );
52+
std::unique_ptr< QgsAbstractGeometry > geom = geomFromWkbType( type );
5553

5654
if ( geom )
5755
{
@@ -63,86 +61,83 @@ QgsAbstractGeometry *QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
6361
{
6462
Q_UNUSED( e );
6563
QgsDebugMsg( "WKB exception: " + e.what() );
66-
delete geom;
67-
geom = nullptr;
6864
}
6965
}
7066

7167
return geom;
7268
}
7369

74-
QgsAbstractGeometry *QgsGeometryFactory::geomFromWkt( const QString &text )
70+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkt( const QString &text )
7571
{
7672
QString trimmed = text.trimmed();
77-
QgsAbstractGeometry *geom = nullptr;
73+
std::unique_ptr< QgsAbstractGeometry> geom;
7874
if ( trimmed.startsWith( QLatin1String( "Point" ), Qt::CaseInsensitive ) )
7975
{
80-
geom = new QgsPoint();
76+
geom.reset( new QgsPoint() );
8177
}
8278
else if ( trimmed.startsWith( QLatin1String( "LineString" ), Qt::CaseInsensitive ) )
8379
{
84-
geom = new QgsLineString();
80+
geom.reset( new QgsLineString() );
8581
}
8682
else if ( trimmed.startsWith( QLatin1String( "CircularString" ), Qt::CaseInsensitive ) )
8783
{
88-
geom = new QgsCircularString();
84+
geom.reset( new QgsCircularString() );
8985
}
9086
else if ( trimmed.startsWith( QLatin1String( "CompoundCurve" ), Qt::CaseInsensitive ) )
9187
{
92-
geom = new QgsCompoundCurve();
88+
geom.reset( new QgsCompoundCurve() );
9389
}
9490
else if ( trimmed.startsWith( QLatin1String( "Polygon" ), Qt::CaseInsensitive ) )
9591
{
96-
geom = new QgsPolygonV2();
92+
geom.reset( new QgsPolygonV2() );
9793
}
9894
else if ( trimmed.startsWith( QLatin1String( "CurvePolygon" ), Qt::CaseInsensitive ) )
9995
{
100-
geom = new QgsCurvePolygon();
96+
geom.reset( new QgsCurvePolygon() );
10197
}
10298
else if ( trimmed.startsWith( QLatin1String( "MultiPoint" ), Qt::CaseInsensitive ) )
10399
{
104-
geom = new QgsMultiPointV2();
100+
geom.reset( new QgsMultiPointV2() );
105101
}
106102
else if ( trimmed.startsWith( QLatin1String( "MultiCurve" ), Qt::CaseInsensitive ) )
107103
{
108-
geom = new QgsMultiCurve();
104+
geom.reset( new QgsMultiCurve() );
109105
}
110106
else if ( trimmed.startsWith( QLatin1String( "MultiLineString" ), Qt::CaseInsensitive ) )
111107
{
112-
geom = new QgsMultiLineString();
108+
geom.reset( new QgsMultiLineString() );
113109
}
114110
else if ( trimmed.startsWith( QLatin1String( "MultiSurface" ), Qt::CaseInsensitive ) )
115111
{
116-
geom = new QgsMultiSurface();
112+
geom.reset( new QgsMultiSurface() );
117113
}
118114
else if ( trimmed.startsWith( QLatin1String( "MultiPolygon" ), Qt::CaseInsensitive ) )
119115
{
120-
geom = new QgsMultiPolygonV2();
116+
geom.reset( new QgsMultiPolygonV2() );
121117
}
122118
else if ( trimmed.startsWith( QLatin1String( "GeometryCollection" ), Qt::CaseInsensitive ) )
123119
{
124-
geom = new QgsGeometryCollection();
120+
geom.reset( new QgsGeometryCollection() );
125121
}
126122

127123
if ( geom )
128124
{
129125
if ( !geom->fromWkt( text ) )
130126
{
131-
delete geom;
132127
return nullptr;
133128
}
134129
}
135130
return geom;
136131
}
137132

138-
QgsAbstractGeometry *QgsGeometryFactory::fromPoint( const QgsPointXY &point )
133+
std::unique_ptr< QgsAbstractGeometry > QgsGeometryFactory::fromPoint( const QgsPointXY &point )
139134
{
140-
return new QgsPoint( point.x(), point.y() );
135+
return std::unique_ptr< QgsAbstractGeometry >( new QgsPoint( point.x(), point.y() ) );
141136
}
142137

143-
QgsAbstractGeometry *QgsGeometryFactory::fromMultiPoint( const QgsMultiPoint &multipoint )
138+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromMultiPoint( const QgsMultiPoint &multipoint )
144139
{
145-
QgsMultiPointV2 *mp = new QgsMultiPointV2();
140+
std::unique_ptr< QgsMultiPointV2 > mp( new QgsMultiPointV2() );
146141
QgsMultiPoint::const_iterator ptIt = multipoint.constBegin();
147142
for ( ; ptIt != multipoint.constEnd(); ++ptIt )
148143
{
@@ -152,55 +147,55 @@ QgsAbstractGeometry *QgsGeometryFactory::fromMultiPoint( const QgsMultiPoint &mu
152147
return mp;
153148
}
154149

155-
QgsAbstractGeometry *QgsGeometryFactory::fromPolyline( const QgsPolyline &polyline )
150+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromPolyline( const QgsPolyline &polyline )
156151
{
157152
return linestringFromPolyline( polyline );
158153
}
159154

160-
QgsAbstractGeometry *QgsGeometryFactory::fromMultiPolyline( const QgsMultiPolyline &multiline )
155+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromMultiPolyline( const QgsMultiPolyline &multiline )
161156
{
162-
QgsMultiLineString *mLine = new QgsMultiLineString();
157+
std::unique_ptr< QgsMultiLineString > mLine( new QgsMultiLineString() );
163158
for ( int i = 0; i < multiline.size(); ++i )
164159
{
165-
mLine->addGeometry( fromPolyline( multiline.at( i ) ) );
160+
mLine->addGeometry( fromPolyline( multiline.at( i ) ).release() );
166161
}
167162
return mLine;
168163
}
169164

170-
QgsAbstractGeometry *QgsGeometryFactory::fromPolygon( const QgsPolygon &polygon )
165+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromPolygon( const QgsPolygon &polygon )
171166
{
172-
QgsPolygonV2 *poly = new QgsPolygonV2();
167+
std::unique_ptr< QgsPolygonV2 > poly( new QgsPolygonV2() );
173168

174169
QList<QgsCurve *> holes;
175170
for ( int i = 0; i < polygon.size(); ++i )
176171
{
177-
QgsLineString *l = linestringFromPolyline( polygon.at( i ) );
172+
std::unique_ptr< QgsLineString > l = linestringFromPolyline( polygon.at( i ) );
178173
l->close();
179174

180175
if ( i == 0 )
181176
{
182-
poly->setExteriorRing( l );
177+
poly->setExteriorRing( l.release() );
183178
}
184179
else
185180
{
186-
holes.push_back( l );
181+
holes.push_back( l.release() );
187182
}
188183
}
189184
poly->setInteriorRings( holes );
190185
return poly;
191186
}
192187

193-
QgsAbstractGeometry *QgsGeometryFactory::fromMultiPolygon( const QgsMultiPolygon &multipoly )
188+
std::unique_ptr< QgsAbstractGeometry > QgsGeometryFactory::fromMultiPolygon( const QgsMultiPolygon &multipoly )
194189
{
195-
QgsMultiPolygonV2 *mp = new QgsMultiPolygonV2();
190+
std::unique_ptr< QgsMultiPolygonV2 > mp( new QgsMultiPolygonV2() );
196191
for ( int i = 0; i < multipoly.size(); ++i )
197192
{
198-
mp->addGeometry( fromPolygon( multipoly.at( i ) ) );
193+
mp->addGeometry( fromPolygon( multipoly.at( i ) ).release() );
199194
}
200195
return mp;
201196
}
202197

203-
QgsAbstractGeometry *QgsGeometryFactory::fromRect( const QgsRectangle &rect )
198+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromRect( const QgsRectangle &rect )
204199
{
205200
QgsPolyline ring;
206201
ring.append( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) );
@@ -215,7 +210,7 @@ QgsAbstractGeometry *QgsGeometryFactory::fromRect( const QgsRectangle &rect )
215210
return fromPolygon( polygon );
216211
}
217212

218-
QgsLineString *QgsGeometryFactory::linestringFromPolyline( const QgsPolyline &polyline )
213+
std::unique_ptr<QgsLineString> QgsGeometryFactory::linestringFromPolyline( const QgsPolyline &polyline )
219214
{
220215
QVector< double > x;
221216
x.reserve( polyline.size() );
@@ -227,39 +222,39 @@ QgsLineString *QgsGeometryFactory::linestringFromPolyline( const QgsPolyline &po
227222
x << it->x();
228223
y << it->y();
229224
}
230-
QgsLineString *line = new QgsLineString( x, y );
225+
std::unique_ptr< QgsLineString > line( new QgsLineString( x, y ) );
231226
return line;
232227
}
233228

234-
QgsAbstractGeometry *QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
229+
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
235230
{
236231
QgsWkbTypes::Type type = QgsWkbTypes::flatType( t );
237232
switch ( type )
238233
{
239234
case QgsWkbTypes::Point:
240-
return new QgsPoint();
235+
return std::unique_ptr<QgsAbstractGeometry>( new QgsPoint() );
241236
case QgsWkbTypes::LineString:
242-
return new QgsLineString();
237+
return std::unique_ptr<QgsAbstractGeometry>( new QgsLineString() );
243238
case QgsWkbTypes::CircularString:
244-
return new QgsCircularString();
239+
return std::unique_ptr<QgsAbstractGeometry>( new QgsCircularString() );
245240
case QgsWkbTypes::CompoundCurve:
246-
return new QgsCompoundCurve();
241+
return std::unique_ptr<QgsAbstractGeometry>( new QgsCompoundCurve() );
247242
case QgsWkbTypes::Polygon:
248-
return new QgsPolygonV2();
243+
return std::unique_ptr<QgsAbstractGeometry>( new QgsPolygonV2() );
249244
case QgsWkbTypes::CurvePolygon:
250-
return new QgsCurvePolygon();
245+
return std::unique_ptr<QgsAbstractGeometry>( new QgsCurvePolygon() );
251246
case QgsWkbTypes::MultiLineString:
252-
return new QgsMultiLineString();
247+
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiLineString() );
253248
case QgsWkbTypes::MultiPolygon:
254-
return new QgsMultiPolygonV2();
249+
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiPolygonV2() );
255250
case QgsWkbTypes::MultiPoint:
256-
return new QgsMultiPointV2();
251+
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiPointV2() );
257252
case QgsWkbTypes::MultiCurve:
258-
return new QgsMultiCurve();
253+
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiCurve() );
259254
case QgsWkbTypes::MultiSurface:
260-
return new QgsMultiSurface();
255+
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiSurface() );
261256
case QgsWkbTypes::GeometryCollection:
262-
return new QgsGeometryCollection();
257+
return std::unique_ptr<QgsAbstractGeometry>( new QgsGeometryCollection() );
263258
default:
264259
return nullptr;
265260
}

‎src/core/geometry/qgsgeometryfactory.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,28 @@ class CORE_EXPORT QgsGeometryFactory
5151
/** Construct geometry from a WKB string.
5252
* Updates position of the passed WKB pointer.
5353
*/
54-
static QgsAbstractGeometry *geomFromWkb( QgsConstWkbPtr &wkb );
54+
static std::unique_ptr< QgsAbstractGeometry > geomFromWkb( QgsConstWkbPtr &wkb );
5555

5656
/** Construct geometry from a WKT string.
5757
*/
58-
static QgsAbstractGeometry *geomFromWkt( const QString &text );
58+
static std::unique_ptr< QgsAbstractGeometry > geomFromWkt( const QString &text );
5959

6060
//! Construct geometry from a point
61-
static QgsAbstractGeometry *fromPoint( const QgsPointXY &point );
61+
static std::unique_ptr< QgsAbstractGeometry > fromPoint( const QgsPointXY &point );
6262
//! Construct geometry from a multipoint
63-
static QgsAbstractGeometry *fromMultiPoint( const QgsMultiPoint &multipoint );
63+
static std::unique_ptr< QgsAbstractGeometry > fromMultiPoint( const QgsMultiPoint &multipoint );
6464
//! Construct geometry from a polyline
65-
static QgsAbstractGeometry *fromPolyline( const QgsPolyline &polyline );
65+
static std::unique_ptr< QgsAbstractGeometry > fromPolyline( const QgsPolyline &polyline );
6666
//! Construct geometry from a multipolyline
67-
static QgsAbstractGeometry *fromMultiPolyline( const QgsMultiPolyline &multiline );
67+
static std::unique_ptr< QgsAbstractGeometry > fromMultiPolyline( const QgsMultiPolyline &multiline );
6868
//! Construct geometry from a polygon
69-
static QgsAbstractGeometry *fromPolygon( const QgsPolygon &polygon );
69+
static std::unique_ptr< QgsAbstractGeometry > fromPolygon( const QgsPolygon &polygon );
7070
//! Construct geometry from a multipolygon
71-
static QgsAbstractGeometry *fromMultiPolygon( const QgsMultiPolygon &multipoly );
71+
static std::unique_ptr< QgsAbstractGeometry > fromMultiPolygon( const QgsMultiPolygon &multipoly );
7272
//! Construct geometry from a rectangle
73-
static QgsAbstractGeometry *fromRect( const QgsRectangle &rect );
73+
static std::unique_ptr< QgsAbstractGeometry > fromRect( const QgsRectangle &rect );
7474
//! Return empty geometry from wkb type
75-
static QgsAbstractGeometry *geomFromWkbType( QgsWkbTypes::Type t );
75+
static std::unique_ptr< QgsAbstractGeometry > geomFromWkbType( QgsWkbTypes::Type t );
7676

7777
/**
7878
* Returns a new geometry collection matching a specified WKB \a type. For instance, if
@@ -81,7 +81,7 @@ class CORE_EXPORT QgsGeometryFactory
8181
static std::unique_ptr< QgsGeometryCollection > createCollectionOfType( QgsWkbTypes::Type type );
8282

8383
private:
84-
static QgsLineString *linestringFromPolyline( const QgsPolyline &polyline );
84+
static std::unique_ptr< QgsLineString > linestringFromPolyline( const QgsPolyline &polyline );
8585
};
8686

8787
#endif // QGSGEOMETRYFACTORY_H

‎src/core/qgsvectordataprovider.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
722722
return QgsGeometry();
723723
}
724724

725-
QgsAbstractGeometry *outputGeom = nullptr;
725+
std::unique_ptr< QgsAbstractGeometry > outputGeom;
726726

727727
//convert compoundcurve to circularstring (possible if compoundcurve consists of one circular string)
728728
if ( QgsWkbTypes::flatType( providerGeomType ) == QgsWkbTypes::CircularString )
@@ -735,7 +735,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
735735
const QgsCircularString *circularString = qgsgeometry_cast<const QgsCircularString *>( compoundCurve->curveAt( 0 ) );
736736
if ( circularString )
737737
{
738-
outputGeom = circularString->clone();
738+
outputGeom.reset( circularString->clone() );
739739
}
740740
}
741741
}
@@ -745,7 +745,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
745745
if ( QgsWkbTypes::isMultiType( providerGeomType ) && !QgsWkbTypes::isMultiType( geometry->wkbType() ) )
746746
{
747747
outputGeom = QgsGeometryFactory::geomFromWkbType( providerGeomType );
748-
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( outputGeom );
748+
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( outputGeom.get() );
749749
if ( geomCollection )
750750
{
751751
geomCollection->addGeometry( geometry->clone() );
@@ -758,8 +758,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
758758
QgsAbstractGeometry *curveGeom = outputGeom ? outputGeom->toCurveType() : geometry->toCurveType();
759759
if ( curveGeom )
760760
{
761-
delete outputGeom;
762-
outputGeom = curveGeom;
761+
outputGeom.reset( curveGeom );
763762
}
764763
}
765764

@@ -770,8 +769,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
770769
segmentizedGeom = outputGeom ? outputGeom->segmentize() : geometry->segmentize();
771770
if ( segmentizedGeom )
772771
{
773-
delete outputGeom;
774-
outputGeom = segmentizedGeom;
772+
outputGeom.reset( segmentizedGeom );
775773
}
776774
}
777775

@@ -780,22 +778,22 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
780778
{
781779
if ( !outputGeom )
782780
{
783-
outputGeom = geometry->clone();
781+
outputGeom.reset( geometry->clone() );
784782
}
785783
outputGeom->addZValue();
786784
}
787785
if ( QgsWkbTypes::hasM( providerGeomType ) )
788786
{
789787
if ( !outputGeom )
790788
{
791-
outputGeom = geometry->clone();
789+
outputGeom.reset( geometry->clone() );
792790
}
793791
outputGeom->addMValue();
794792
}
795793

796794
if ( outputGeom )
797795
{
798-
return QgsGeometry( outputGeom );
796+
return QgsGeometry( outputGeom.release() );
799797
}
800798
return QgsGeometry();
801799
}

‎tests/src/core/testqgsdistancearea.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void TestQgsDistanceArea::regression13601()
218218
QgsDistanceArea calc;
219219
calc.setEllipsoid( QStringLiteral( "NONE" ) );
220220
calc.setSourceCrs( QgsCoordinateReferenceSystem::fromSrsId( 1108L ) );
221-
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((252000 1389000, 265000 1389000, 265000 1385000, 252000 1385000, 252000 1389000))" ) ) );
221+
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((252000 1389000, 265000 1389000, 265000 1385000, 252000 1385000, 252000 1389000))" ) ).release() );
222222
QGSCOMPARENEAR( calc.measureArea( geom ), 52000000, 0.0001 );
223223
}
224224

@@ -230,21 +230,21 @@ void TestQgsDistanceArea::collections()
230230
myDa.setEllipsoid( QStringLiteral( "WGS84" ) );
231231

232232
//collection of lines, should be sum of line length
233-
QgsGeometry lines( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70) )" ) ) );
233+
QgsGeometry lines( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70) )" ) ).release() );
234234
double result = myDa.measureLength( lines );
235235
QGSCOMPARENEAR( result, 12006159, 1 );
236236
result = myDa.measureArea( lines );
237237
QVERIFY( qgsDoubleNear( result, 0 ) );
238238

239239
//collection of polygons
240-
QgsGeometry polys( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ) );
240+
QgsGeometry polys( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() );
241241
result = myDa.measureArea( polys );
242242
QGSCOMPARENEAR( result, 670434859475LL, 1 );
243243
result = myDa.measureLength( polys );
244244
QVERIFY( qgsDoubleNear( result, 0 ) );
245245

246246
//mixed collection
247-
QgsGeometry mixed( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70), Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ) );
247+
QgsGeometry mixed( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70), Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() );
248248
//measure area specifically
249249
result = myDa.measureArea( mixed );
250250
QGSCOMPARENEAR( result, 670434859475LL, 1 );
@@ -375,7 +375,7 @@ void TestQgsDistanceArea::regression14675()
375375
QgsDistanceArea calc;
376376
calc.setEllipsoid( QStringLiteral( "GRS80" ) );
377377
calc.setSourceCrs( QgsCoordinateReferenceSystem::fromSrsId( 145L ) );
378-
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((917593.5791854317067191 6833700.00807378999888897, 917596.43389983859378844 6833700.67099479306489229, 917599.53056440979707986 6833700.78673478215932846, 917593.5791854317067191 6833700.00807378999888897))" ) ) );
378+
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((917593.5791854317067191 6833700.00807378999888897, 917596.43389983859378844 6833700.67099479306489229, 917599.53056440979707986 6833700.78673478215932846, 917593.5791854317067191 6833700.00807378999888897))" ) ).release() );
379379
//lots of tolerance here - the formulas get quite unstable with small areas due to division by very small floats
380380
QGSCOMPARENEAR( calc.measureArea( geom ), 0.833010, 0.03 );
381381
}

‎tests/src/core/testqgsgeometry.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5293,7 +5293,7 @@ void TestQgsGeometry::wkbInOut()
52935293
void TestQgsGeometry::segmentizeCircularString()
52945294
{
52955295
QString wkt( QStringLiteral( "CIRCULARSTRING( 0 0, 0.5 0.5, 2 0 )" ) );
5296-
QgsCircularString *circularString = dynamic_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( wkt ) );
5296+
QgsCircularString *circularString = dynamic_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( wkt ).release() );
52975297
QVERIFY( circularString );
52985298
QgsLineString *lineString = circularString->curveToLine();
52995299
QVERIFY( lineString );
@@ -5311,11 +5311,11 @@ void TestQgsGeometry::directionNeutralSegmentation()
53115311
{
53125312
//Tests, if segmentation of a circularstring is the same in both directions
53135313
QString CWCircularStringWkt( QStringLiteral( "CIRCULARSTRING( 0 0, 0.5 0.5, 0.83 7.33 )" ) );
5314-
QgsCircularString *CWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CWCircularStringWkt ) );
5314+
QgsCircularString *CWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CWCircularStringWkt ).release() );
53155315
QgsLineString *CWLineString = CWCircularString->curveToLine();
53165316

53175317
QString CCWCircularStringWkt( QStringLiteral( "CIRCULARSTRING( 0.83 7.33, 0.5 0.5, 0 0 )" ) );
5318-
QgsCircularString *CCWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CCWCircularStringWkt ) );
5318+
QgsCircularString *CCWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CCWCircularStringWkt ).release() );
53195319
QgsLineString *CCWLineString = CCWCircularString->curveToLine();
53205320
QgsLineString *reversedCCWLineString = CCWLineString->reversed();
53215321

0 commit comments

Comments
 (0)
Please sign in to comment.