Proj version agnostic context storage

Sponsored by ICSM
This commit is contained in:
Nyall Dawson 2019-03-18 12:09:01 +10:00
parent ad7e8eac74
commit 4fc575940b
3 changed files with 138 additions and 0 deletions

View File

@ -284,6 +284,7 @@ SET(QGIS_CORE_SRCS
qgsprojectstorage.cpp
qgsprojectstorageregistry.cpp
qgsprojectversion.cpp
qgsprojutils.cpp
qgsproperty.cpp
qgspropertycollection.cpp
qgspropertytransformer.cpp
@ -940,6 +941,7 @@ SET(QGIS_CORE_HDRS
qgsprojectstorageregistry.h
qgsprojecttranslator.h
qgsprojectversion.h
qgsprojutils.h
qgsproperty.h
qgsproperty_p.h
qgspropertycollection.h

67
src/core/qgsprojutils.cpp Normal file
View File

@ -0,0 +1,67 @@
/***************************************************************************
qgsprojutils.h
-------------------
begin : March 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsprojutils.h"
#if PROJ_VERSION_MAJOR>=6
#include <proj.h>
#else
#include <proj_api.h>
#endif
#ifdef USE_THREAD_LOCAL
thread_local QgsProjContext QgsProjContext::sProjContext;
#else
QThreadStorage< QgsProjContext * > QgsProjContext::sProjContext;
#endif
QgsProjContext::QgsProjContext()
{
#if PROJ_VERSION_MAJOR>=6
mContext = proj_context_create();
#else
mContext = pj_ctx_alloc();
#endif
}
QgsProjContext::~QgsProjContext()
{
#if PROJ_VERSION_MAJOR>=6
proj_context_destroy( mContext );
#else
pj_ctx_free( mContext );
#endif
}
PJ_CONTEXT *QgsProjContext::get()
{
#ifdef USE_THREAD_LOCAL
return sProjContext.mContext;
#else
PJ_CONTEXT *pContext = nullptr;
if ( sProjContext.hasLocalData() )
{
pContext = sProjContext.localData()->mContext;
}
else
{
sProjContext.setLocalData( new QgsProjContext() );
pContext = sProjContext.localData()->mContext;
}
return pContext;
#endif
}

69
src/core/qgsprojutils.h Normal file
View File

@ -0,0 +1,69 @@
/***************************************************************************
qgsprojutils.h
-------------------
begin : March 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROJUTILS_H
#define QGSPROJUTILS_H
#include "qgis_core.h"
#include "qgsconfig.h"
#ifndef USE_THREAD_LOCAL
#include <QThreadStorage>
#endif
#define SIP_NO_FILE
#if PROJ_VERSION_MAJOR>=6
struct projCtx_t;
typedef struct projCtx_t PJ_CONTEXT;
#else
typedef void PJ_CONTEXT;
#endif
/**
* \class QgsProjContext
* \ingroup core
* Used to create and store a proj context object, correctly freeing the context upon destruction.
* \note Not available in Python bindings
*/
class CORE_EXPORT QgsProjContext
{
public:
QgsProjContext();
~QgsProjContext();
/**
* Returns a thread local instance of a proj context, safe for use in the current thread.
*/
static PJ_CONTEXT *get();
private:
PJ_CONTEXT *mContext = nullptr;
/**
* Thread local proj context storage. A new proj context will be created
* for every thread.
*/
#ifdef USE_THREAD_LOCAL
static thread_local QgsProjContext sProjContext;
#else
static QThreadStorage< QgsProjContext * > sProjContext;
#endif
};
#endif // QGSPROJUTILS_H