diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2019-01-09 17:05:21 +0100 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2019-01-09 17:05:21 +0100 |
| commit | d6dad2cee4307f6b190a96dd48942645060919cc (patch) | |
| tree | 5819c2626b6361dd007df884255ab713437939dd /src/ctx.cpp | |
| parent | 5b75e5fdccc531f7b0e3dcd636fa1ff3500bb071 (diff) | |
| download | PROJ-d6dad2cee4307f6b190a96dd48942645060919cc.tar.gz PROJ-d6dad2cee4307f6b190a96dd48942645060919cc.zip | |
Allocate projCtx_t as a C++ object
Diffstat (limited to 'src/ctx.cpp')
| -rw-r--r-- | src/ctx.cpp | 94 |
1 files changed, 49 insertions, 45 deletions
diff --git a/src/ctx.cpp b/src/ctx.cpp index 195f3b7f..a94eaf54 100644 --- a/src/ctx.cpp +++ b/src/ctx.cpp @@ -29,12 +29,10 @@ #include <stdlib.h> #include <string.h> +#include <new> + #include "proj_experimental.h" #include "proj_internal.h" -#include "proj_internal.h" - -static projCtx_t default_context; -static volatile int default_context_initialized = 0; /************************************************************************/ /* pj_get_ctx() */ @@ -81,61 +79,68 @@ void proj_assign_context( PJ* pj, PJ_CONTEXT* ctx ) } /************************************************************************/ -/* pj_get_default_ctx() */ +/* createDefault() */ /************************************************************************/ -projCtx pj_get_default_ctx() - +projCtx_t projCtx_t::createDefault() { - /* If already initialized, don't bother locking */ - if( default_context_initialized ) - return &default_context; + projCtx_t ctx; + ctx.debug_level = PJ_LOG_NONE; + ctx.logger = pj_stderr_logger; + ctx.fileapi = pj_get_default_fileapi(); - pj_acquire_lock(); - - /* Ask again, since it may have been initialized in another thread */ - if( !default_context_initialized ) + if( getenv("PROJ_DEBUG") != nullptr ) { - default_context.last_errno = 0; - default_context.debug_level = PJ_LOG_NONE; - default_context.logger = pj_stderr_logger; - default_context.app_data = nullptr; - default_context.fileapi = pj_get_default_fileapi(); - default_context.cpp_context = nullptr; - default_context.use_proj4_init_rules = -1; - default_context.epsg_file_exists = -1; - - if( getenv("PROJ_DEBUG") != nullptr ) - { - if( atoi(getenv("PROJ_DEBUG")) >= -PJ_LOG_DEBUG_MINOR ) - default_context.debug_level = atoi(getenv("PROJ_DEBUG")); - else - default_context.debug_level = PJ_LOG_DEBUG_MINOR; - } - default_context_initialized = 1; + if( atoi(getenv("PROJ_DEBUG")) >= -PJ_LOG_DEBUG_MINOR ) + ctx.debug_level = atoi(getenv("PROJ_DEBUG")); + else + ctx.debug_level = PJ_LOG_DEBUG_MINOR; } + return ctx; +} + +/************************************************************************/ +/* projCtx_t(const projCtx_t& other) */ +/************************************************************************/ - pj_release_lock(); +projCtx_t::projCtx_t(const projCtx_t& other) +{ + debug_level = other.debug_level; + logger = other.logger; + logger_app_data = other.logger_app_data; + fileapi = other.fileapi; + epsg_file_exists = other.epsg_file_exists; +} +/************************************************************************/ +/* pj_get_default_ctx() */ +/************************************************************************/ + +projCtx pj_get_default_ctx() + +{ + // C++11 rules guarantee a thread-safe instanciation. + static projCtx_t default_context(projCtx_t::createDefault()); return &default_context; } /************************************************************************/ +/* ~projCtx_t() */ +/************************************************************************/ + +projCtx_t::~projCtx_t() +{ + proj_context_delete_cpp_context(cpp_context); +} + +/************************************************************************/ /* pj_ctx_alloc() */ /************************************************************************/ projCtx pj_ctx_alloc() { - projCtx ctx = (projCtx_t *) malloc(sizeof(projCtx_t)); - if (nullptr==ctx) - return nullptr; - memcpy( ctx, pj_get_default_ctx(), sizeof(projCtx_t) ); - ctx->last_errno = 0; - ctx->cpp_context = nullptr; - ctx->use_proj4_init_rules = -1; - - return ctx; + return new (std::nothrow) projCtx_t(*pj_get_default_ctx()); } /************************************************************************/ @@ -145,8 +150,7 @@ projCtx pj_ctx_alloc() void pj_ctx_free( projCtx ctx ) { - proj_context_delete_cpp_context( ctx->cpp_context ); - pj_dealloc( ctx ); + delete ctx; } /************************************************************************/ @@ -210,7 +214,7 @@ void pj_ctx_set_app_data( projCtx ctx, void *new_app_data ) { if (nullptr==ctx) return; - ctx->app_data = new_app_data; + ctx->logger_app_data = new_app_data; } /************************************************************************/ @@ -222,7 +226,7 @@ void *pj_ctx_get_app_data( projCtx ctx ) { if (nullptr==ctx) return nullptr; - return ctx->app_data; + return ctx->logger_app_data; } /************************************************************************/ |
