Skip to content
Permalink
Browse files

Use unique_ptrs for proj object storage

Sponsored by ICSM
  • Loading branch information
nyalldawson committed Apr 5, 2019
1 parent 95c0d4f commit c2cac5aab06f9a66b3616db7f39dfa8dc9e41599
@@ -10,6 +10,7 @@




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

};

/************************************************************************
@@ -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"
@@ -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 );
@@ -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;
@@ -386,8 +387,6 @@ QList<QgsEllipsoidUtils::EllipsoidDefinition> QgsEllipsoidUtils::definitions()
def.parameters.valid = false;
}

proj_destroy( ellipsoid );

defs << def;
sEllipsoidCache.insert( def.acronym, def.parameters );
}
@@ -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
@@ -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
@@ -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

0 comments on commit c2cac5a

Please sign in to comment.
You can’t perform that action at this time.