Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

Commit 350c69e

Browse files
rdeitsSimonDanisch
authored andcommitted
convert HomogenousMesh constructor to an @generated function to work around segfault (#93)
* convert HomogenousMesh constructor to an @generated function to work around segfault fixes #92 * fix test * fix bug and correct typo * rearrange test to fix merge conflicts
1 parent 9caec85 commit 350c69e

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/meshes.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,27 @@ end
7979
isvoid{T}(::Type{T}) = false
8080
isvoid(::Type{Void}) = true
8181
isvoid{T}(::Type{Vector{T}}) = isvoid(T)
82-
function (::Type{HM1}){HM1 <: HomogenousMesh}(primitive::HomogenousMesh)
82+
83+
@generated function (::Type{HM1})(primitive::HM2) where {HM1 <: HomogenousMesh, HM2 <: HomogenousMesh}
8384
fnames = fieldnames(HM1)
84-
args = ntuple(nfields(HM1)) do i
85-
field, target_type = fnames[i], fieldtype(HM1, i)
86-
soure_type = fieldtype(typeof(primitive), i)
87-
isleaftype(fieldtype(HM1, i)) || return getfield(primitive, field) # target is not defined
88-
if !isvoid(target_type) && isvoid(soure_type) # target not there yet, maybe we can decompose though (e.g. normals)
89-
return decompose(HM1.parameters[i], primitive)
85+
expr = Expr(:call, HM1)
86+
for i in 1:nfields(HM1)
87+
field = fnames[i]
88+
target_type = fieldtype(HM1, i)
89+
source_type = fieldtype(HM2, i)
90+
if !isleaftype(fieldtype(HM1, i)) # target is not defined
91+
push!(expr.args, :(getfield(primitive, $(QuoteNode(field)))))
92+
elseif !isvoid(target_type) && isvoid(source_type) # target not there yet, maybe we can decompose though (e.g. normals)
93+
push!(expr.args, :(decompose($(HM1.parameters[i]), primitive)))
9094
elseif isvoid(target_type)
91-
return target_type()
95+
push!(expr.args, :($(target_type())))
9296
else
93-
return convert(target_type, getfield(primitive, field))
97+
push!(expr.args, :(convert($target_type, getfield(primitive, $(QuoteNode(field))))))
9498
end
9599
end
96-
HM1(args...)
100+
expr
97101
end
98102

99-
100103
#Should be:
101104
#function call{M <: HMesh, VT <: Point, FT <: Face}(::Type{M}, vertices::Vector{VT}, faces::Vector{FT})
102105
# Haven't gotten around to implement the types correctly with abstract types in FixedSizeArrays

test/meshes.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ end
4040
#@fact readall(io) --> s #Win32 and Win64 have different ordering it seems.
4141
end
4242

43+
@testset "construction" begin
44+
VT = vertextype(GLNormalMesh)
45+
FT = facetype(GLNormalMesh)
46+
vs = [VT(0., 0, 0), VT(1., 0, 0), VT(0., 1, 0)]
47+
fs = [FT(1, 2, 3)]
4348

49+
# test for https://github.com/JuliaGeometry/GeometryTypes.jl/issues/92
50+
m = HomogenousMesh(vs, fs)
51+
@test HomogenousMesh(m) == m
52+
end
4453

4554
@testset "Primitives" begin
4655
# issue #16

0 commit comments

Comments
 (0)