From d19c617955ab56e40aeefa469bdc42d9fa7378b2 Mon Sep 17 00:00:00 2001 From: Bart Janssens Date: Tue, 14 Feb 2023 22:07:03 +0100 Subject: [PATCH] Clean up function argument types and test overload --- src/CxxWrap.jl | 23 ++++++++++++----------- test/functions.jl | 19 ++++++++----------- test/types.jl | 22 +++++++++++++++++++--- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/CxxWrap.jl b/src/CxxWrap.jl index 2086d39..1463d4b 100644 --- a/src/CxxWrap.jl +++ b/src/CxxWrap.jl @@ -494,24 +494,25 @@ argument_overloads(t::Type{Ptr{T}}) where {T <: Number} = [Array{T,1}] Create a Union containing the type and a smart pointer to any type derived from it """ function ptrunion(::Type{T}) where {T} - ST = T - if T == allocated_type(supertype(T)) - ST = supertype(T) - end - result{T2 <: ST} = Union{T2, SmartPointer{T2}} + result{T2 <: T} = Union{T2, SmartPointer{T2}} return result end -smart_pointer_type(t::Type) = smart_pointer_type(cpp_trait_type(t), t) -smart_pointer_type(::Type{IsNormalType}, t::Type) = t -smart_pointer_type(::Type{IsCxxType}, x::Type{T}) where {T} = ptrunion(x) - -function smart_pointer_type(::Type{<:SmartPointer{T}}) where {T} +valuetype(t::Type) = valuetype(cpp_trait_type(t), t) +valuetype(::Type{IsNormalType}, ::Type{T}) where {T} = T +function valuetype(::Type{IsCxxType}, ::Type{T}) where {T} + ST = supertype(T) + if T == allocated_type(ST) + return ST + end + return T +end +function valuetype(::Type{<:SmartPointer{T}}) where {T} result{T2 <: T} = SmartPointer{T2} return result end -map_julia_arg_type(t::Type) = Union{Base.invokelatest(smart_pointer_type,t),argument_overloads(t)...} +map_julia_arg_type(t::Type) = Union{Base.invokelatest(valuetype,t),argument_overloads(t)...} map_julia_arg_type(a::Type{StrictlyTypedNumber{T}}) where {T} = T map_julia_arg_type(a::Type{StrictlyTypedNumber{CxxBool}}) = Union{Bool,CxxBool} map_julia_arg_type(x::Type{CxxBool}) = Union{Bool,CxxBool} diff --git a/test/functions.jl b/test/functions.jl index 3373cf3..d6c2bfe 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -191,19 +191,16 @@ cppdref[] = 1.0 @test CppTestFunctions.process_irrational(π, 2) == 2*π -if CxxWrap.libcxxwrapversion() > v"0.7.0" - @test CppTestFunctions.open("foo") == "foo" +@test CppTestFunctions.open("foo") == "foo" + +let bref = Ref{Cuchar}(0) + @test bref[] == false + CppTestFunctions.boolref(bref) + @test bref[] == true + CppTestFunctions.boolref(bref) + @test bref[] == false end -if CxxWrap.libcxxwrapversion() > v"0.7.1" - let bref = Ref{Cuchar}(0) - @test bref[] == false - CppTestFunctions.boolref(bref) - @test bref[] == true - CppTestFunctions.boolref(bref) - @test bref[] == false - end -end end # testset end diff --git a/test/types.jl b/test/types.jl index 2ac18a6..52261bc 100644 --- a/test/types.jl +++ b/test/types.jl @@ -129,6 +129,25 @@ CppTypes.set(w, "hello") @test CppTypes.greet(w) == "hello" @test CppTypes.greet_lambda(w) == "hello" +if CxxWrap.libcxxwrapversion() > v"0.9.4" + wr = CxxRef(w) + wcr = ConstCxxRef(w) + wp = CxxPtr(w) + wcp = ConstCxxPtr(w) + @test CppTypes.greet_byvalue(w) == "hello" + @test CppTypes.greet_byvalue(wr[]) == "hello" + @test CppTypes.greet_overload(w) == "hello_byval" + @test CppTypes.greet_overload(wr) == "hello_byref" + @test CppTypes.greet_overload(wr[]) == "hello_byval" + @test CppTypes.greet_overload(wcr) == "hello_byconstref" + @test CppTypes.greet_overload(wcr[]) == "hello_byval" + @test CppTypes.greet_overload(wp) == "hello_bypointer" + @test CppTypes.greet_overload(wp[]) == "hello_byval" + @test CppTypes.greet_overload(wcp) == "hello_byconstpointer" + @test CppTypes.greet_overload(wcp[]) == "hello_byval" + @test CppTypes.greet_overload(swf) == "shared factory hello_bysharedptr" +end + w = CppTypes.World("constructed") @test CppTypes.greet(w) == "constructed" @@ -138,9 +157,6 @@ w_copy = copy(w) @test w_assigned == w @test w_copy != w -#w_lambda = CppTypes.World("Hi", "Lambda") -#@test CppTypes.greet(w_lambda) == "Hi Lambda" - # Destroy w: w and w_assigned should be dead, w_copy alive finalize(w) #finalize(w_lambda)