File: anonymous_union.rb

package info (click to toggle)
ruby-cstruct 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 412 kB
  • sloc: ruby: 1,008; makefile: 7
file content (42 lines) | stat: -rw-r--r-- 707 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# CStruct Examples
require 'cstruct'

# example:
# struct U in C\C++ (32-bit platform): 
# 
# struct U
# {
#    union{
#       int x;
#       int y;
#    }value; /* values is anonymous union's instance */
# }; 

# struct U in Ruby: 
class U < CStruct
  union:value do
    int32:x
    int32:y
  end
end

# or like this (use brace):
# class U < CStruct
#    union (:position) {
#        int32:x
#        int32:y    
#    }
# end


# create a U's instance
u = U.new

# Notice! Assign is different from C language.
# 'value' can not be omitted !
u.value.x = 10

puts "sizeof(U) = #{U.__size__}" # "__size__" is alias of "size" . You may use 'size'.
puts "u.value.x = #{u.value.x }, u.value.y = #{u.value.y }"