Current section

13 Versions

Jump to

Compare versions

3 files changed
+38 additions
-7 deletions
  @@ -11,4 +11,4 @@
11 11 {<<"maintainers">>,[<<"Chris Duranti">>]}.
12 12 {<<"name">>,<<"reproject">>}.
13 13 {<<"requirements">>,[]}.
14 - {<<"version">>,<<"0.1.5">>}.
14 + {<<"version">>,<<"0.1.6">>}.
  @@ -13,7 +13,7 @@ defmodule Reproject.Mixfile do
13 13
14 14 def project do
15 15 [app: :reproject,
16 - version: "0.1.5",
16 + version: "0.1.6",
17 17 elixir: ">= 1.3.0 and <= 1.6.0",
18 18 build_embedded: Mix.env == :prod,
19 19 start_permanent: Mix.env == :prod,
  @@ -11,6 +11,37 @@
11 11 #define ok(x) enif_make_tuple2(env, reproject_atoms.ok, x)
12 12 #define error(x) enif_make_tuple2(env, reproject_atoms.error, enif_make_string(env, x, ERL_NIF_LATIN1))
13 13
14 + namespace {
15 + template<typename T>
16 + class simple_ptr {
17 + public:
18 + simple_ptr(T* ptr_, void (*destructor_)(void*))
19 + : ptr(ptr_), destructor(destructor_)
20 + {}
21 +
22 + ~simple_ptr() {
23 + if(ptr) destructor(ptr);
24 + }
25 +
26 + T* get() const {
27 + return ptr;
28 + }
29 +
30 + T* operator->() const {
31 + return ptr;
32 + }
33 +
34 + operator bool() const {
35 + return ptr;
36 + }
37 + private:
38 + simple_ptr(simple_ptr<T> const&); // prevent copies
39 +
40 + T* ptr;
41 + void (*destructor)(void*);
42 + };
43 + }
44 +
14 45 static ErlNifResourceType *pj_cd_type = NULL;
15 46
16 47 static struct {
  @@ -63,7 +94,7 @@ static ERL_NIF_TERM create(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
63 94 return error("Failed to initialize the projection");
64 95 }
65 96
66 - std::shared_ptr<pj_cd> cd((pj_cd*)enif_alloc_resource(pj_cd_type, sizeof(pj_cd)), enif_release_resource);
97 + simple_ptr<pj_cd> cd((pj_cd*)enif_alloc_resource(pj_cd_type, sizeof(pj_cd)), enif_release_resource);
67 98
68 99 if (!(cd->pj = pj_init_plus(proj_buf))) {
69 100 return error(pj_strerrno(pj_errno));
  @@ -94,7 +125,7 @@ static ERL_NIF_TERM create_from_wkt(ErlNifEnv* env, int argc, const ERL_NIF_TERM
94 125 return error("Failed to initialize the wkt from erlang side");
95 126 }
96 127
97 - std::shared_ptr<void> hSR(OSRNewSpatialReference(&wkt_buf[0]), CPLFree);
128 + simple_ptr<void> hSR(OSRNewSpatialReference(&wkt_buf[0]), CPLFree);
98 129 if (!hSR) {
99 130 return error("Failed to initialize OGRSpatialReferenceH");
100 131 }
  @@ -113,9 +144,9 @@ static ERL_NIF_TERM create_from_wkt(ErlNifEnv* env, int argc, const ERL_NIF_TERM
113 144 if (OSRExportToProj4(hSR.get(), &proj_buf_raw) != OGRERR_NONE) {
114 145 return error("Failed to export wkt to proj4");
115 146 }
116 - std::shared_ptr<char> proj_buf(proj_buf_raw, CPLFree);
147 + simple_ptr<char> proj_buf(proj_buf_raw, CPLFree);
117 148
118 - std::shared_ptr<pj_cd> cd((pj_cd*) enif_alloc_resource(pj_cd_type, sizeof(pj_cd)), enif_release_resource);
149 + simple_ptr<pj_cd> cd((pj_cd*) enif_alloc_resource(pj_cd_type, sizeof(pj_cd)), enif_release_resource);
119 150
120 151 if (!(cd->pj = pj_init_plus(proj_buf.get()))) {
121 152 return error(pj_strerrno(pj_errno));
  @@ -131,7 +162,7 @@ static ERL_NIF_TERM expand(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
131 162 return error("Failed to get the resource - did you initialize it with create/1?");
132 163 }
133 164
134 - std::shared_ptr<char> expanded(pj_get_def(p->pj, 0), pj_dalloc);
165 + simple_ptr<char> expanded(pj_get_def(p->pj, 0), pj_dalloc);
135 166 int expanded_len = strlen(expanded.get());
136 167
137 168 ERL_NIF_TERM res;