Skip to content

azurefx/CUnion.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CUnion.jl

Build Status

This package provides C-style primitive union types for Julia.

Usage

Add the macro @union to a struct definition to make a C union:

@union struct U
  x::UInt8
  y::UInt16
end

Then U can be instantiated with one of its field types:

julia> u = U(Int8(-1))
U1(0x00ff)

julia> u1.y
0x00ff

julia> reinterpret(Int16, u)
255

Nested anonymous structs are also supported. For example, the LARGE_INTEGER type from Win32

typedef union _LARGE_INTEGER {
  struct {
    DWORD LowPart;
    LONG HighPart;
  } DUMMYSTRUCTNAME;
  struct {
    DWORD LowPart;
    LONG HighPart;
  } u;
  LONGLONG QuadPart;
} LARGE_INTEGER;

Can be written as

@union struct LargeInteger
  struct u
    low::UInt32
    high::UInt32
  end
  quad::UInt64
end
julia> li=LargeInteger(0x00112233aabbccdd)
LargeInteger(0x00112233aabbccdd)

julia> li.u.high
0x00112233

TODO

  1. Improved reinterpret_cast performance?
  2. Convenient methods to update fields?
  3. Sub-typing and generic support?

About

C-like unions for Julia.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages