Skip to content

Commit

Permalink
Allow unsafe_wrapping PointerWrappers (#71)
Browse files Browse the repository at this point in the history
* allow unsafe_wrapping PointerWrappers

* add test

* add test for different argument types

* add more tests

* Update test/tests_basic.jl

Co-authored-by: Hendrik Ranocha <[email protected]>

---------

Co-authored-by: Hendrik Ranocha <[email protected]>
  • Loading branch information
JoshuaLampert and ranocha authored Mar 23, 2023
1 parent e639537 commit 1e39336
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/pointerwrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Base.setindex!(pw::PointerWrapper, value, i::Integer=1) = unsafe_store!(pw, valu
# When `unsafe_load`ing a PointerWrapper object, we really want to load the underlying object
Base.unsafe_load(pw::PointerWrapper, i::Integer=1) = unsafe_load(pointer(pw), i)

# When `unsafe_wrap`ping a PointerWrapper object, we really want to wrap the underlying array
Base.unsafe_wrap(AType::Union{Type{Array},Type{Array{T}},Type{Array{T,N}}},
pw::PointerWrapper, dims::NTuple{N,Int}; own::Bool = false) where {T,N} = unsafe_wrap(AType, pointer(pw), dims; own)

# If value is of the wrong type, try to convert it
Base.unsafe_store!(pw::PointerWrapper{T}, value, i::Integer=1) where T = unsafe_store!(pw, convert(T, value), i)

Expand Down
15 changes: 15 additions & 0 deletions test/tests_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ end
@test p4est_pw.connectivity.num_trees isa PointerWrapper{Int32}

@test pointer(p4est_pw) isa Ptr{P4est.LibP4est.p4est}

# `unsafe_wrap`ping a `PointerWrapper`
n_vertices::Int = connectivity_pw.num_vertices[]
vertices = @test_nowarn unsafe_wrap(Array, connectivity_pw.vertices, (3, n_vertices))
@test vertices isa Array{Float64, 2}
@test unsafe_wrap(Array{Float64}, connectivity_pw.vertices, (3, n_vertices)) isa Array{Float64, 2}
@test unsafe_wrap(Array{Float64, 2}, connectivity_pw.vertices, (3, n_vertices)) isa Array{Float64, 2}

@test size(vertices) == (3, n_vertices)
@test vertices[1, 1] == connectivity_pw.vertices[1] == 0.0
@test_nowarn vertices[1, 1] = 1.0
@test vertices[1, 1] == connectivity_pw.vertices[1] == 1.0
@test_nowarn connectivity_pw.vertices[1] = 2.0
@test vertices[1, 1] == connectivity_pw.vertices[1] == 2.0

@test_nowarn p4est_destroy(p4est_pw)
@test_nowarn p4est_connectivity_destroy(connectivity_pw)
end
Expand Down

0 comments on commit 1e39336

Please sign in to comment.