-
Notifications
You must be signed in to change notification settings - Fork 68
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
Constructors for union structs #304
Comments
I think this is a special case where all 3 fields have the same size, and the size is equal to the size of the union.
|
By using the current generated field access methods, the simplest way to init a
|
Right, we could take the example of struct VkClearValue
data::NTuple{16, UInt8}
end
function Base.getproperty(x::Ptr{VkClearValue}, f::Symbol)
f === :color && return Ptr{VkClearColorValue}(x + 0)
f === :depthStencil && return Ptr{VkClearDepthStencilValue}(x + 0)
return getfield(x, f)
end There, the two fields are 8 and 16 bytes respectively. Although to be general we should probably take into account that some fields may start at different offsets. Instead of the previously mentioned constructor we could generate a pointer to the provided data, reinterpret it with const __U_VkClearValue = Union{VkClearColorValue,VkClearDepthStencilValue}
function VkClearValue(data::__U_VkClearValue)
ref = Ref{VkClearValue}()
x = Base.unsafe_convert(Ptr{VkClearValue}, ref)
if data isa VkClearColorValue
x.color = data
elseif data isa VkClearDepthStencilValue
x.depthStencil = data
end
GC.@preserve ref unsafe_load(x)
end |
yeah, that sounds like a good solution. |
Or maybe we could just dereference |
Yep. |
function Base.getproperty(x::VkClearValue, f::Symbol)
r = Ref{VkClearValue}(x)
ptr = Base.unsafe_convert(Ptr{VkClearValue}, r)
fptr = getproperty(ptr, f)
GC.@preserve r unsafe_load(fptr) # change to r[]
end |
Implemented in #305. |
The current master generates union structs like so:
where the
data
field is reinterpreted when accessed to match C unions.As mentioned in this comment, because union structs hold raw bytes, it's not so convenient to instantiate them so we could define a constructor for union structs that help populate their bytes. We could generate something very similar to the snippet in the comment:
So I am thinking of extending this method to emit the constructor method along with the
getproperty
/setproperty!
methods. Would you accept a PR for adding this feature?The text was updated successfully, but these errors were encountered: