Skip to content

Commit

Permalink
Clean up function argument types and test overload
Browse files Browse the repository at this point in the history
  • Loading branch information
barche committed Feb 15, 2023
1 parent ae6783c commit d19c617
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
23 changes: 12 additions & 11 deletions src/CxxWrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
19 changes: 8 additions & 11 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand Down

0 comments on commit d19c617

Please sign in to comment.