Skip to content

Commit

Permalink
Use unique_ptrs for proj object storage
Browse files Browse the repository at this point in the history
Sponsored by ICSM
  • Loading branch information
nyalldawson committed Apr 5, 2019
1 parent 95c0d4f commit c2cac5a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/qgsprojutils.sip.in
Expand Up @@ -10,6 +10,7 @@




class QgsProjUtils
{
%Docstring
Expand All @@ -27,6 +28,7 @@ Utility functions for working with the proj library.
%Docstring
Returns the proj library major version number.
%End

};

/************************************************************************
Expand Down
6 changes: 2 additions & 4 deletions src/core/qgscoordinatetransform.cpp
Expand Up @@ -691,8 +691,8 @@ void QgsCoordinateTransform::transformCoords( int numPoints, double *x, double *
QString dir = ( direction == ForwardTransform ) ? QObject::tr( "forward transform" ) : QObject::tr( "inverse transform" );

#if PROJ_VERSION_MAJOR>=6
PJ *src = proj_get_source_crs( QgsProjContext::get(), projData );
PJ *dest = proj_get_source_crs( QgsProjContext::get(), projData );
QgsProjUtils::proj_pj_unique_ptr src( proj_get_source_crs( QgsProjContext::get(), projData ) );
QgsProjUtils::proj_pj_unique_ptr dest( proj_get_source_crs( QgsProjContext::get(), projData ) );
QString msg = QObject::tr( "%1 of\n"
"%2"
"PROJ: %3\n"
Expand All @@ -701,8 +701,6 @@ void QgsCoordinateTransform::transformCoords( int numPoints, double *x, double *
points,
proj_as_proj_string( QgsProjContext::get(), projData, PJ_PROJ_5, nullptr ),
QString::fromUtf8( proj_errno_string( projResult ) ) );
proj_destroy( src );
proj_destroy( dest );
#else
char *srcdef = pj_get_def( sourceProj, 0 );
char *dstdef = pj_get_def( destProj, 0 );
Expand Down
9 changes: 4 additions & 5 deletions src/core/qgsellipsoidutils.cpp
Expand Up @@ -359,17 +359,18 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
PROJ_STRING_LIST codesIt = codes;
while ( char *code = *codesIt )
{
if ( PJ *ellipsoid = proj_create_from_database( context, authority, code, PJ_CATEGORY_ELLIPSOID, 0, nullptr ) )
QgsProjUtils::proj_pj_unique_ptr ellipsoid( proj_create_from_database( context, authority, code, PJ_CATEGORY_ELLIPSOID, 0, nullptr ) );
if ( ellipsoid.get() )
{
EllipsoidDefinition def;
QString name = QString( proj_get_name( ellipsoid ) );
QString name = QString( proj_get_name( ellipsoid.get() ) );
def.acronym = QStringLiteral( "%1:%2" ).arg( authority, code );
name.replace( '_', ' ' );
def.description = QStringLiteral( "%1 (%2:%3)" ).arg( name, authority, code );

double semiMajor, semiMinor, invFlattening;
int semiMinorComputed = 0;
if ( proj_ellipsoid_get_parameters( context, ellipsoid, &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
if ( proj_ellipsoid_get_parameters( context, ellipsoid.get(), &semiMajor, &semiMinor, &semiMinorComputed, &invFlattening ) )
{
def.parameters.semiMajor = semiMajor;
def.parameters.semiMinor = semiMinor;
Expand All @@ -386,8 +387,6 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
def.parameters.valid = false;
}

proj_destroy( ellipsoid );

defs << def;
sEllipsoidCache.insert( def.acronym, def.parameters );
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsprojutils.cpp
Expand Up @@ -65,3 +65,10 @@ PJ_CONTEXT *QgsProjContext::get()
return pContext;
#endif
}

#if PROJ_VERSION_MAJOR>=6
void QgsProjUtils::ProjPJDeleter::operator()( PJ *object )
{
proj_destroy( object );
}
#endif
32 changes: 32 additions & 0 deletions src/core/qgsprojutils.h
Expand Up @@ -21,11 +21,19 @@

#include "qgis_core.h"
#include "qgsconfig.h"
#include <memory>

#if !defined(USE_THREAD_LOCAL) || defined(Q_OS_WIN)
#include <QThreadStorage>
#endif

#if PROJ_VERSION_MAJOR>=6
#ifndef SIP_RUN
struct PJconsts;
typedef struct PJconsts PJ;
#endif
#endif

/**
* \class QgsProjUtils
* \ingroup core
Expand All @@ -43,6 +51,30 @@ class CORE_EXPORT QgsProjUtils
{
return PROJ_VERSION_MAJOR;
}

#ifndef SIP_RUN
#if PROJ_VERSION_MAJOR >= 6

/**
* Destroys Proj PJ objects.
*/
struct ProjPJDeleter
{

/**
* Destroys an PJ \a object, using the correct proj calls.
*/
void CORE_EXPORT operator()( PJ *object );

};

/**
* Scoped Proj PJ object.
*/
using proj_pj_unique_ptr = std::unique_ptr< PJ, ProjPJDeleter >;

#endif
#endif
};

#ifndef SIP_RUN
Expand Down

0 comments on commit c2cac5a

Please sign in to comment.