Skip to content

Commit

Permalink
refactoring context
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynorth committed Dec 17, 2023
1 parent 87f1f35 commit 282347b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
36 changes: 36 additions & 0 deletions src/proj-context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#define R_NO_REMAP
#include <R.h>
#include <Rinternals.h>
#include <proj.h>
#include "proj-context.h"

SEXP proj_context_xptr_create(PJ_CONTEXT* ctx) {
SEXP ctx_xptr = PROTECT(R_MakeExternalPtr(ctx, R_NilValue, R_NilValue));
R_RegisterCFinalizer(ctx_xptr, &proj_context_xptr_destroy);
UNPROTECT(1);
return ctx_xptr;
}

void proj_context_xptr_destroy(SEXP ctx_xptr) {
if (TYPEOF(ctx_xptr) != EXTPTRSXP) {
Rf_error("`ctx_xptr` must be an externalptr");
}

PJ_CONTEXT* ctx = (PJ_CONTEXT*) R_ExternalPtrAddr(ctx_xptr);
if (ctx != NULL) proj_context_destroy(ctx);
}

#if PROJ_VERSION_MAJOR < 8
static const char* proj_context_errno_string(PJ_CONTEXT*, int err) {
// deprecated in proj 8
return proj_errno_string(err);
}
#endif

void stop_proj_error(PJ_CONTEXT* ctx) {
int err = proj_context_errno(ctx);
const char* msg = proj_context_errno_string(ctx, err);

Rf_error("%s", msg);
}

7 changes: 7 additions & 0 deletions src/proj-context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <R.h>
#include <Rinternals.h>
#include <proj.h>

SEXP proj_context_xptr_create(PJ_CONTEXT* ctx);
void proj_context_xptr_destroy(SEXP ctx_xptr);
void stop_proj_error(PJ_CONTEXT* ctx);
25 changes: 2 additions & 23 deletions src/wk-trans.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <proj.h>
#include <stdio.h>
#include "wk-v1.h"
#include "proj-context.h"

typedef struct {
PJ* pj;
Expand Down Expand Up @@ -31,25 +32,6 @@ static void finalize(void* trans_data) {
free(data);
}

static void ctx_xptr_destroy(SEXP ctx_xptr) {
PJ_CONTEXT* ctx = (PJ_CONTEXT*)R_ExternalPtrAddr(ctx_xptr);
if (ctx != NULL) proj_context_destroy(ctx);
}

#if PROJ_VERSION_MAJOR < 8
const char* proj_context_errno_string(PJ_CONTEXT*, int err) {
// deprecated in proj 8
return proj_errno_string(err);
}
#endif

static const char* stop_proj_error(PJ_CONTEXT* ctx) {
int err = proj_context_errno(ctx);
const char* msg = proj_context_errno_string(ctx, err);

Rf_error("%s", msg);
}

SEXP C_proj_trans_new(SEXP source_crs, SEXP target_crs, SEXP use_z, SEXP use_m) {
wk_trans_t* trans = wk_trans_create();

Expand All @@ -67,10 +49,7 @@ SEXP C_proj_trans_new(SEXP source_crs, SEXP target_crs, SEXP use_z, SEXP use_m)
trans->trans_data = data;

PJ_CONTEXT* ctx = proj_context_create();
// ensure context is finalised
SEXP ctx_xptr = PROTECT(R_MakeExternalPtr(ctx, R_NilValue, R_NilValue));
R_RegisterCFinalizer(ctx_xptr, &ctx_xptr_destroy);

SEXP ctx_xptr = PROTECT(proj_context_xptr_create(ctx));
// ensure context stays valid
SEXP trans_xptr = PROTECT(wk_trans_create_xptr(trans, ctx_xptr, R_NilValue));

Expand Down

0 comments on commit 282347b

Please sign in to comment.