-
Notifications
You must be signed in to change notification settings - Fork 2
Add getindex for ConduitNode #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,10 @@ struct ConduitNode | |
| nptr = API.conduit_node_create() | ||
| return new(nptr) | ||
| end | ||
|
|
||
| function ConduitNode(ptr::Ptr{API.conduit_node}) | ||
| return new(ptr) | ||
| end | ||
| end | ||
| Base.unsafe_convert(::Type{Ptr{API.conduit_node}}, node::ConduitNode) = node.ptr | ||
|
|
||
|
|
@@ -23,11 +27,24 @@ function ConduitNode(f::Function) | |
| end | ||
|
|
||
| Base.setindex!(node::ConduitNode, val, path::String) = node_set!(node, path, val) | ||
| Base.getindex(node::ConduitNode, path::String) = node_get(node, path) | ||
|
|
||
| function node_get(node::ConduitNode, path::String) | ||
| if !node_has_path(node, path) | ||
| throw(KeyError(path)) | ||
| end | ||
| node_at_path = API.conduit_node_fetch(node, path) | ||
| dtype = API.conduit_node_dtype(node_at_path) | ||
| dtypename = Symbol(unsafe_string(API.conduit_datatype_name(dtype))) | ||
| return node_get(node_at_path, Val(dtypename)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Val here seems unnecessarily clever, and guarantees a dynamic dispatch. |
||
| end | ||
|
|
||
| for numtype in (:UInt8, :Int8, :UInt16, :Int16, :UInt32, :Int32, :UInt64, :Int64, :Float32, :Float64) | ||
| cnumtype = Symbol(lowercase("$(numtype)")) | ||
| node_set_path = Symbol("conduit_node_set_path_$(cnumtype)") | ||
| node_set_path_ptr = Symbol("conduit_node_set_path_$(cnumtype)_ptr") | ||
| node_as = Symbol("conduit_node_as_$(cnumtype)") | ||
| node_as_ptr = Symbol("conduit_node_as_$(cnumtype)_ptr") | ||
| @eval begin | ||
| function node_set!(node::ConduitNode, path::String, val::$numtype) | ||
| API.$node_set_path(node, path, val) | ||
|
|
@@ -37,6 +54,12 @@ for numtype in (:UInt8, :Int8, :UInt16, :Int16, :UInt32, :Int32, :UInt64, :Int64 | |
| API.$node_set_path_ptr(node, path, pointer(val), length(val)) | ||
| return node | ||
| end | ||
| function node_get(nodeptr::Ptr{API.conduit_node}, ::$(Val{cnumtype})) | ||
| return API.$node_as(nodeptr) | ||
| end | ||
| function node_get(nodeptr::Ptr{API.conduit_node}, ::$(Val{Array{cnumtype}})) | ||
| return API.$node_as_ptr(nodeptr) | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -50,6 +73,14 @@ function node_set!(node::ConduitNode, path::String, val::ConduitNode) | |
| return node | ||
| end | ||
|
|
||
| function node_get(nodeptr::Ptr{API.conduit_node}, ::Val{:object}) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's likely better to have a function that uses if else to select between symbols, rather than using Val |
||
| return ConduitNode(nodeptr) | ||
| end | ||
|
|
||
| function node_get(nodeptr::Ptr{API.conduit_node}, ::Val{:char8_str}) | ||
| return unsafe_string(API.conduit_node_as_char8_str(nodeptr)) | ||
| end | ||
|
|
||
| function node_has_path(node::ConduitNode, path::String) | ||
| return Bool(API.conduit_node_has_path(node, path)) | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the ownership model between Julia objects and the pointed to C-object? When is memory going to be released?