Skip to content

Commit

Permalink
Allow passing references to value arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
barche committed Feb 17, 2023
1 parent d19c617 commit 23c59c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/CxxWrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,12 @@ function ptrunion(::Type{T}) where {T}
return result
end

valuetype(t::Type) = valuetype(cpp_trait_type(t), t)
valuetype(t::Type) = valuetype(Base.invokelatest(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
if T == Base.invokelatest(allocated_type,ST) # Case of a C++ by-value argument
return Union{ST, CxxRef{ST}, ConstCxxRef{ST}}
end
return T
end
Expand All @@ -512,7 +512,7 @@ function valuetype(::Type{<:SmartPointer{T}}) where {T}
return result
end

map_julia_arg_type(t::Type) = Union{Base.invokelatest(valuetype,t),argument_overloads(t)...}
map_julia_arg_type(t::Type) = Union{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 Expand Up @@ -684,7 +684,7 @@ function wrap_reference_converters(julia_mod)
Core.eval(julia_mod, :($(@__MODULE__).dereferenced_type(::Type{$st}) = $reftype))
Core.eval(julia_mod, :(Base.convert(::Type{$st}, x::$bt) = x))
Core.eval(julia_mod, :(Base.convert(::Type{$st}, x::$reftype) = x))
Core.eval(julia_mod, :(Base.cconvert(::Type{$reftype}, x::$bt) = $reftype(x.cpp_object)))
Core.eval(julia_mod, :(Base.cconvert(::Type{$reftype}, x::Union{CxxRef{<:$st},ConstCxxRef{<:$st},$bt}) = $reftype(x.cpp_object)))
Core.eval(julia_mod, :(Base.unsafe_convert(::Type{$reftype}, x::$st) = $reftype(x.cpp_object)))
Core.eval(julia_mod, :(Base.:(==)(a::Union{CxxRef{<:$st},ConstCxxRef{<:$st},$bt}, b::$reftype) = (a.cpp_object == b.cpp_object)))
Core.eval(julia_mod, :(Base.:(==)(a::$reftype, b::Union{CxxRef{<:$st},ConstCxxRef{<:$st},$bt}) = (b == a)))
Expand Down
13 changes: 7 additions & 6 deletions test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,17 @@ if CxxWrap.libcxxwrapversion() > v"0.9.4"
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_byvalue(wr) == "hello"
@test CppTypes.greet_byvalue(wcr) == "hello"
@test CppTypes.greet_overload(w) == "hello_byref"
@test CppTypes.greet_overload(wr) == "hello_byref"
@test CppTypes.greet_overload(wr[]) == "hello_byval"
@test CppTypes.greet_overload(wr[]) == "hello_byref"
@test CppTypes.greet_overload(wcr) == "hello_byconstref"
@test CppTypes.greet_overload(wcr[]) == "hello_byval"
@test CppTypes.greet_overload(wcr[]) == "hello_byref"
@test CppTypes.greet_overload(wp) == "hello_bypointer"
@test CppTypes.greet_overload(wp[]) == "hello_byval"
@test CppTypes.greet_overload(wp[]) == "hello_byref"
@test CppTypes.greet_overload(wcp) == "hello_byconstpointer"
@test CppTypes.greet_overload(wcp[]) == "hello_byval"
@test CppTypes.greet_overload(wcp[]) == "hello_byref"
@test CppTypes.greet_overload(swf) == "shared factory hello_bysharedptr"
end

Expand Down

0 comments on commit 23c59c9

Please sign in to comment.