Advertisement
TheBirkIsReal

type info example of polystructs

Jan 2nd, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.28 KB | None | 0 0
  1. package test
  2.  
  3. import "core:fmt"
  4. import "core:runtime"
  5.  
  6. Bar :: struct(T: typeid) #packed {
  7.     val: T,
  8.     other_data: int,
  9. }
  10.  
  11. Foo :: struct(T: typeid) #packed {
  12.     bar: Bar(T),
  13.     t: u64,
  14. }
  15.  
  16. main :: proc() {
  17.     // T = int
  18.     fmt.printf("type_info_of(Foo(int))                      = %v\n", type_info_of(Foo(int))^);
  19.     fmt.printf("type_info_of(Foo(int)).variant              = %v\n", type_info_of(Foo(int))^.variant);
  20.     fmt.printf("type_info_of(Foo(int)).variant.base         = %v\n", type_info_of(Foo(int))^.variant.(runtime.Type_Info_Named).base^);
  21.     fmt.printf("type_info_of(Foo(int)).variant.base.variant = %v\n", type_info_of(Foo(int))^.variant.(runtime.Type_Info_Named).base^.variant);
  22.  
  23.     // T = u8
  24.     fmt.printf("type_info_of(Foo(u8 ))                      = %v\n", type_info_of(Foo(u8))^);
  25.     fmt.printf("type_info_of(Foo(u8 )).variant              = %v\n", type_info_of(Foo(u8))^.variant);
  26.     fmt.printf("type_info_of(Foo(u8 )).variant.base         = %v\n", type_info_of(Foo(u8))^.variant.(runtime.Type_Info_Named).base^);
  27.     fmt.printf("type_info_of(Foo(u8 )).variant.base.variant = %v\n", type_info_of(Foo(u8))^.variant.(runtime.Type_Info_Named).base^.variant);
  28. }
  29.  
  30. /* Output:
  31.  
  32. type_info_of(Foo(int))                      = Type_Info{size = 24, align = 1, id = Foo(int), variant = Type_Info_Named{name = Foo(int), base = struct #packed {bar: Bar(T), t: u64}}}
  33. type_info_of(Foo(int)).variant              = Type_Info_Named{name = Foo(int), base = struct #packed {bar: Bar(T), t: u64}}
  34. type_info_of(Foo(int)).variant.base         = Type_Info{size = 24, align = 1, id = struct #packed {bar: Bar(T), t: u64}, variant = Type_Info_Struct{types = [Bar(T), u64], names = [bar, t], offsets = [0, 16], usings = [false, false], is_packed = true, is_raw_union = false, custom_align = false}}
  35. type_info_of(Foo(int)).variant.base.variant = Type_Info_Struct{types = [Bar(T), u64], names = [bar, t], offsets = [0, 16], usings = [false, false], is_packed = true, is_raw_union = false, custom_align = false}
  36.                                                                                                                       ^
  37.                                                                                                                       Notice how the offsets differ
  38.  
  39. type_info_of(Foo(u8 ))                      = Type_Info{size = 17, align = 1, id = Foo(u8), variant = Type_Info_Named{name = Foo(u8), base = struct #packed {bar: Bar(T), t: u64}}}
  40. type_info_of(Foo(u8 )).variant              = Type_Info_Named{name = Foo(u8), base = struct #packed {bar: Bar(T), t: u64}}
  41. type_info_of(Foo(u8 )).variant.base         = Type_Info{size = 17, align = 1, id = struct #packed {bar: Bar(T), t: u64}, variant = Type_Info_Struct{types = [Bar(T), u64], names = [bar, t], offsets = [0, 9], usings = [false, false], is_packed = true, is_raw_union = false, custom_align = false}}
  42. type_info_of(Foo(u8 )).variant.base.variant = Type_Info_Struct{types = [Bar(T), u64], names = [bar, t], offsets = [0, 9], usings = [false, false], is_packed = true, is_raw_union = false, custom_align = false}
  43.                                                                                                                       ^
  44.                                                                                                                       Different offset
  45. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement