Skip to content

Commit c2cac5a

Browse files
committed
Use unique_ptrs for proj object storage
Sponsored by ICSM
1 parent 95c0d4f commit c2cac5a

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

python/core/auto_generated/qgsprojutils.sip.in

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212

13+
1314
class QgsProjUtils
1415
{
1516
%Docstring
@@ -27,6 +28,7 @@ Utility functions for working with the proj library.
2728
%Docstring
2829
Returns the proj library major version number.
2930
%End
31+
3032
};
3133

3234
/************************************************************************

src/core/qgscoordinatetransform.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,8 @@ void QgsCoordinateTransform::transformCoords( int numPoints, double *x, double *
691691
QString dir = ( direction == ForwardTransform ) ? QObject::tr( "forward transform" ) : QObject::tr( "inverse transform" );
692692

693693
#if PROJ_VERSION_MAJOR>=6
694-
PJ *src = proj_get_source_crs( QgsProjContext::get(), projData );
695-
PJ *dest = proj_get_source_crs( QgsProjContext::get(), projData );
694+
QgsProjUtils::proj_pj_unique_ptr src( proj_get_source_crs( QgsProjContext::get(), projData ) );
695+
QgsProjUtils::proj_pj_unique_ptr dest( proj_get_source_crs( QgsProjContext::get(), projData ) );
696696
QString msg = QObject::tr( "%1 of\n"
697697
"%2"
698698
"PROJ: %3\n"
@@ -701,8 +701,6 @@ void QgsCoordinateTransform::transformCoords( int numPoints, double *x, double *
701701
points,
702702
proj_as_proj_string( QgsProjContext::get(), projData, PJ_PROJ_5, nullptr ),
703703
QString::fromUtf8( proj_errno_string( projResult ) ) );
704-
proj_destroy( src );
705-
proj_destroy( dest );
706704
#else
707705
char *srcdef = pj_get_def( sourceProj, 0 );
708706
char *dstdef = pj_get_def( destProj, 0 );

src/core/qgsellipsoidutils.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,18 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
359359
PROJ_STRING_LIST codesIt = codes;
360360
while ( char *code = *codesIt )
361361
{
362-
if ( PJ *ellipsoid = proj_create_from_database( context, authority, code, PJ_CATEGORY_ELLIPSOID, 0, nullptr ) )
362+
QgsProjUtils::proj_pj_unique_ptr ellipsoid( proj_create_from_database( context, authority, code, PJ_CATEGORY_ELLIPSOID, 0, nullptr ) );
363+
if ( ellipsoid.get() )
363364
{
364365
EllipsoidDefinition def;
365-
QString name = QString( proj_get_name( ellipsoid ) );
366+
QString name = QString( proj_get_name( ellipsoid.get() ) );
366367
def.acronym = QStringLiteral( "%1:%2" ).arg( authority, code );
367368
name.replace( '_', ' ' );
368369
def.description = QStringLiteral( "%1 (%2:%3)" ).arg( name, authority, code );
369370

370371
double semiMajor, semiMinor, invFlattening;
371372
int semiMinorComputed = 0;
372-
if ( proj_ellipsoid_get_parameters( context, ellipsoid, &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
373+
if ( proj_ellipsoid_get_parameters( context, ellipsoid.get(), &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
373374
{
374375
def.parameters.semiMajor = semiMajor;
375376
def.parameters.semiMinor = semiMinor;
@@ -386,8 +387,6 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
386387
def.parameters.valid = false;
387388
}
388389

389-
proj_destroy( ellipsoid );
390-
391390
defs << def;
392391
sEllipsoidCache.insert( def.acronym, def.parameters );
393392
}

src/core/qgsprojutils.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ PJ_CONTEXT *QgsProjContext::get()
6565
return pContext;
6666
#endif
6767
}
68+
69+
#if PROJ_VERSION_MAJOR>=6
70+
void QgsProjUtils::ProjPJDeleter::operator()( PJ *object )
71+
{
72+
proj_destroy( object );
73+
}
74+
#endif

src/core/qgsprojutils.h

+32
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121

2222
#include "qgis_core.h"
2323
#include "qgsconfig.h"
24+
#include <memory>
2425

2526
#if !defined(USE_THREAD_LOCAL) || defined(Q_OS_WIN)
2627
#include <QThreadStorage>
2728
#endif
2829

30+
#if PROJ_VERSION_MAJOR>=6
31+
#ifndef SIP_RUN
32+
struct PJconsts;
33+
typedef struct PJconsts PJ;
34+
#endif
35+
#endif
36+
2937
/**
3038
* \class QgsProjUtils
3139
* \ingroup core
@@ -43,6 +51,30 @@ class CORE_EXPORT QgsProjUtils
4351
{
4452
return PROJ_VERSION_MAJOR;
4553
}
54+
55+
#ifndef SIP_RUN
56+
#if PROJ_VERSION_MAJOR >= 6
57+
58+
/**
59+
* Destroys Proj PJ objects.
60+
*/
61+
struct ProjPJDeleter
62+
{
63+
64+
/**
65+
* Destroys an PJ \a object, using the correct proj calls.
66+
*/
67+
void CORE_EXPORT operator()( PJ *object );
68+
69+
};
70+
71+
/**
72+
* Scoped Proj PJ object.
73+
*/
74+
using proj_pj_unique_ptr = std::unique_ptr< PJ, ProjPJDeleter >;
75+
76+
#endif
77+
#endif
4678
};
4779

4880
#ifndef SIP_RUN

0 commit comments

Comments
 (0)