diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2024-08-30 22:40:14 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2024-08-30 22:40:14 +0200 |
| commit | 8ad4427a25c2fdfeb39c2dd7d91c392b48b5a2a3 (patch) | |
| tree | 619704ccd3e858354fc72217b5a114548262a2ed | |
| parent | db3bcd2cea70955b0e7c1d910ddc66c3843f6eb1 (diff) | |
Add a few more reflect.bit_field_* helpers.
| -rw-r--r-- | core/reflect/reflect.odin | 91 |
1 files changed, 68 insertions, 23 deletions
diff --git a/core/reflect/reflect.odin b/core/reflect/reflect.odin index 2026ffef9..c04afb380 100644 --- a/core/reflect/reflect.odin +++ b/core/reflect/reflect.odin @@ -1015,6 +1015,74 @@ bit_set_is_big_endian :: proc(value: any, loc := #caller_location) -> bool { } +Bit_Field :: struct { + name: string, + type: ^Type_Info, + size: uintptr, // Size in bits + offset: uintptr, // Offset in bits + tag: Struct_Tag, +} + +@(require_results) +bit_fields_zipped :: proc(T: typeid) -> (fields: #soa[]Bit_Field) { + ti := runtime.type_info_base(type_info_of(T)) + if s, ok := ti.variant.(runtime.Type_Info_Bit_Field); ok { + return soa_zip( + name = s.names[:s.field_count], + type = s.types[:s.field_count], + size = s.bit_sizes[:s.field_count], + offset = s.bit_offsets[:s.field_count], + tag = ([^]Struct_Tag)(s.tags)[:s.field_count], + ) + } + return nil +} + +@(require_results) +bit_field_names :: proc(T: typeid) -> []string { + ti := runtime.type_info_base(type_info_of(T)) + if s, ok := ti.variant.(runtime.Type_Info_Bit_Field); ok { + return s.names[:s.field_count] + } + return nil +} + +@(require_results) +bit_field_types :: proc(T: typeid) -> []^Type_Info { + ti := runtime.type_info_base(type_info_of(T)) + if s, ok := ti.variant.(runtime.Type_Info_Bit_Field); ok { + return s.types[:s.field_count] + } + return nil +} + +@(require_results) +bit_field_sizes :: proc(T: typeid) -> []uintptr { + ti := runtime.type_info_base(type_info_of(T)) + if s, ok := ti.variant.(runtime.Type_Info_Bit_Field); ok { + return s.bit_sizes[:s.field_count] + } + return nil +} + +@(require_results) +bit_field_offsets :: proc(T: typeid) -> []uintptr { + ti := runtime.type_info_base(type_info_of(T)) + if s, ok := ti.variant.(runtime.Type_Info_Bit_Field); ok { + return s.bit_offsets[:s.field_count] + } + return nil +} + +@(require_results) +bit_field_tags :: proc(T: typeid) -> []Struct_Tag { + ti := runtime.type_info_base(type_info_of(T)) + if s, ok := ti.variant.(runtime.Type_Info_Bit_Field); ok { + return transmute([]Struct_Tag)s.tags[:s.field_count] + } + return nil +} + @(require_results) as_bool :: proc(a: any) -> (value: bool, valid: bool) { if a == nil { return } @@ -1698,27 +1766,4 @@ equal :: proc(a, b: any, including_indirect_array_recursion := false, recursion_ runtime.print_typeid(a.id) runtime.print_string("\n") return true -} - -Bit_Field :: struct { - name: string, - type: ^Type_Info, - size: uintptr, - offset: uintptr, - tag: Struct_Tag, -} - -@(require_results) -bit_fields_zipped :: proc(T: typeid) -> (fields: #soa[]Bit_Field) { - ti := runtime.type_info_base(type_info_of(T)) - if s, ok := ti.variant.(runtime.Type_Info_Bit_Field); ok { - return soa_zip( - name = s.names[:s.field_count], - type = s.types[:s.field_count], - size = s.bit_sizes[:s.field_count], - offset = s.bit_offsets[:s.field_count], - tag = ([^]Struct_Tag)(s.tags)[:s.field_count], - ) - } - return nil }
\ No newline at end of file |