Skip to content

Commit e92ab14

Browse files
committed
rename to string_copy_free
Also adds the context argument, though it is not used yet.
1 parent d749ee1 commit e92ab14

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

gen/generator.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function rewrite(ex::Expr, cursor::Clang.CLFunctionDecl)
2626
if return_type_is_const_char(cursor)
2727
:(unsafe_string($cc))
2828
else
29-
:(transform_c_string($cc))
29+
:(string_copy_free($cc))
3030
end
3131
else
3232
cc

src/LibGEOS.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,6 @@ export Point,
9494
STRtree,
9595
query
9696

97-
function transform_c_string(s::Cstring)::String
98-
copy = unsafe_string(s)
99-
global _context
100-
GEOSFree_r(_context.ptr, Ptr{Cvoid}(s))
101-
return copy
102-
end
103-
104-
include("libgeos_api.jl")
105-
10697
mutable struct GEOSError <: Exception
10798
msg::String
10899
end
@@ -117,7 +108,7 @@ function geosjl_errorhandler(message::Ptr{UInt8}, userdata)
117108
end
118109

119110
mutable struct GEOSContext
120-
ptr::GEOSContextHandle_t
111+
ptr::Ptr{Cvoid} # GEOSContextHandle_t
121112

122113
function GEOSContext()
123114
context = new(GEOS_init_r())
@@ -131,6 +122,15 @@ mutable struct GEOSContext
131122
end
132123
end
133124

125+
"Get a copy of a string from GEOS, freeing the GEOS managed memory."
126+
function string_copy_free(s::Cstring, context::GEOSContext = _context)::String
127+
copy = unsafe_string(s)
128+
GEOSFree_r(context.ptr, pointer(s))
129+
return copy
130+
end
131+
132+
include("libgeos_api.jl")
133+
134134
mutable struct WKTReader
135135
ptr::Ptr{GEOSWKTReader}
136136

src/libgeos_api.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ function GEOSRelatePattern_r(handle, g1, g2, pat)
12891289
end
12901290

12911291
function GEOSRelate_r(handle, g1, g2)
1292-
transform_c_string(
1292+
string_copy_free(
12931293
@ccall(
12941294
libgeos.GEOSRelate_r(
12951295
handle::GEOSContextHandle_t,
@@ -1309,7 +1309,7 @@ function GEOSRelatePatternMatch_r(handle, mat, pat)
13091309
end
13101310

13111311
function GEOSRelateBoundaryNodeRule_r(handle, g1, g2, bnr)
1312-
transform_c_string(
1312+
string_copy_free(
13131313
@ccall(
13141314
libgeos.GEOSRelateBoundaryNodeRule_r(
13151315
handle::GEOSContextHandle_t,
@@ -1330,7 +1330,7 @@ function GEOSisValid_r(handle, g)
13301330
end
13311331

13321332
function GEOSisValidReason_r(handle, g)
1333-
transform_c_string(
1333+
string_copy_free(
13341334
@ccall(
13351335
libgeos.GEOSisValidReason_r(
13361336
handle::GEOSContextHandle_t,
@@ -1408,7 +1408,7 @@ function GEOSRemoveRepeatedPoints_r(handle, g, tolerance)
14081408
end
14091409

14101410
function GEOSGeomType_r(handle, g)
1411-
transform_c_string(
1411+
string_copy_free(
14121412
@ccall(
14131413
libgeos.GEOSGeomType_r(
14141414
handle::GEOSContextHandle_t,
@@ -1827,7 +1827,7 @@ function GEOSWKTWriter_destroy_r(handle, writer)
18271827
end
18281828

18291829
function GEOSWKTWriter_write_r(handle, writer, g)
1830-
transform_c_string(
1830+
string_copy_free(
18311831
@ccall(
18321832
libgeos.GEOSWKTWriter_write_r(
18331833
handle::GEOSContextHandle_t,
@@ -2038,7 +2038,7 @@ function GEOSGeoJSONWriter_destroy_r(handle, writer)
20382038
end
20392039

20402040
function GEOSGeoJSONWriter_writeGeometry_r(handle, writer, g, indent)
2041-
transform_c_string(
2041+
string_copy_free(
20422042
@ccall(
20432043
libgeos.GEOSGeoJSONWriter_writeGeometry_r(
20442044
handle::GEOSContextHandle_t,
@@ -2308,7 +2308,7 @@ function GEOSGeom_destroy(g)
23082308
end
23092309

23102310
function GEOSGeomType(g)
2311-
transform_c_string(@ccall(libgeos.GEOSGeomType(g::Ptr{GEOSGeometry})::Cstring))
2311+
string_copy_free(@ccall(libgeos.GEOSGeomType(g::Ptr{GEOSGeometry})::Cstring))
23122312
end
23132313

23142314
function GEOSGeomTypeId(g)
@@ -2454,7 +2454,7 @@ function GEOSisValid(g)
24542454
end
24552455

24562456
function GEOSisValidReason(g)
2457-
transform_c_string(@ccall(libgeos.GEOSisValidReason(g::Ptr{GEOSGeometry})::Cstring))
2457+
string_copy_free(@ccall(libgeos.GEOSisValidReason(g::Ptr{GEOSGeometry})::Cstring))
24582458
end
24592459

24602460
function GEOSisValidDetail(g, flags, reason, location)
@@ -3057,7 +3057,7 @@ function GEOSRelatePattern(g1, g2, pat)
30573057
end
30583058

30593059
function GEOSRelate(g1, g2)
3060-
transform_c_string(
3060+
string_copy_free(
30613061
@ccall(libgeos.GEOSRelate(g1::Ptr{GEOSGeometry}, g2::Ptr{GEOSGeometry})::Cstring)
30623062
)
30633063
end
@@ -3067,7 +3067,7 @@ function GEOSRelatePatternMatch(mat, pat)
30673067
end
30683068

30693069
function GEOSRelateBoundaryNodeRule(g1, g2, bnr)
3070-
transform_c_string(
3070+
string_copy_free(
30713071
@ccall(
30723072
libgeos.GEOSRelateBoundaryNodeRule(
30733073
g1::Ptr{GEOSGeometry},
@@ -3294,7 +3294,7 @@ function GEOSWKTWriter_destroy(writer)
32943294
end
32953295

32963296
function GEOSWKTWriter_write(writer, g)
3297-
transform_c_string(
3297+
string_copy_free(
32983298
@ccall(
32993299
libgeos.GEOSWKTWriter_write(
33003300
writer::Ptr{GEOSWKTWriter},
@@ -3450,7 +3450,7 @@ function GEOSGeoJSONWriter_destroy(writer)
34503450
end
34513451

34523452
function GEOSGeoJSONWriter_writeGeometry(writer, g, indent)
3453-
transform_c_string(
3453+
string_copy_free(
34543454
@ccall(
34553455
libgeos.GEOSGeoJSONWriter_writeGeometry(
34563456
writer::Ptr{GEOSGeoJSONWriter},
@@ -3511,7 +3511,7 @@ function GEOSGeomFromWKT_r(handle, wkt)
35113511
end
35123512

35133513
function GEOSGeomToWKT_r(handle, g)
3514-
transform_c_string(
3514+
string_copy_free(
35153515
@ccall(
35163516
libgeos.GEOSGeomToWKT_r(
35173517
handle::GEOSContextHandle_t,
@@ -3577,7 +3577,7 @@ function GEOSGeomFromWKT(wkt)
35773577
end
35783578

35793579
function GEOSGeomToWKT(g)
3580-
transform_c_string(@ccall(libgeos.GEOSGeomToWKT(g::Ptr{GEOSGeometry})::Cstring))
3580+
string_copy_free(@ccall(libgeos.GEOSGeomToWKT(g::Ptr{GEOSGeometry})::Cstring))
35813581
end
35823582

35833583
function GEOS_getWKBOutputDims()

test/test_invalid_geometry.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
polygon = LibGEOS._readgeom("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),
1010
(15 15, 15 20, 20 20, 20 15, 15 15))")
1111
@test !LibGEOS.isValid(polygon)
12-
@test LibGEOS.GEOSisValidReason_r(LibGEOS._context.ptr, polygon) == "Hole lies outside shell[15 15]"
12+
@test LibGEOS.GEOSisValidReason_r(LibGEOS._context.ptr, polygon) ==
13+
"Hole lies outside shell[15 15]"
1314
end

0 commit comments

Comments
 (0)