mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Add reflect.enum_name_from_value and reflect.enum_name_from_value_any
This commit is contained in:
+2
-27
@@ -1408,34 +1408,9 @@ fmt_soa_pointer :: proc(fi: ^Info, p: runtime.Raw_Soa_Pointer, verb: rune) {
|
|||||||
//
|
//
|
||||||
// Returns: The string representation of the enum value and a boolean indicating success.
|
// Returns: The string representation of the enum value and a boolean indicating success.
|
||||||
//
|
//
|
||||||
|
@(require_results)
|
||||||
enum_value_to_string :: proc(val: any) -> (string, bool) {
|
enum_value_to_string :: proc(val: any) -> (string, bool) {
|
||||||
v := val
|
return reflect.enum_name_from_value_any(val)
|
||||||
v.id = runtime.typeid_base(v.id)
|
|
||||||
type_info := type_info_of(v.id)
|
|
||||||
|
|
||||||
#partial switch e in type_info.variant {
|
|
||||||
case: return "", false
|
|
||||||
case runtime.Type_Info_Enum:
|
|
||||||
Enum_Value :: runtime.Type_Info_Enum_Value
|
|
||||||
|
|
||||||
ev_, ok := reflect.as_i64(val)
|
|
||||||
ev := Enum_Value(ev_)
|
|
||||||
|
|
||||||
if ok {
|
|
||||||
if len(e.values) == 0 {
|
|
||||||
return "", true
|
|
||||||
} else {
|
|
||||||
for val, idx in e.values {
|
|
||||||
if val == ev {
|
|
||||||
return e.names[idx], true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", false
|
|
||||||
}
|
}
|
||||||
// Returns the enum value of a string representation.
|
// Returns the enum value of a string representation.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -627,6 +627,43 @@ enum_from_name_any :: proc(Enum_Type: typeid, name: string) -> (value: Type_Info
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
|
enum_name_from_value :: proc(value: $Enum_Type) -> (name: string, ok: bool) where intrinsics.type_is_enum(Enum_Type) {
|
||||||
|
ti := type_info_base(type_info_of(Enum_Type))
|
||||||
|
e := ti.variant.(runtime.Type_Info_Enum) or_return
|
||||||
|
if len(e.values) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ev := Type_Info_Enum_Value(value)
|
||||||
|
for val, idx in e.values {
|
||||||
|
if val == ev {
|
||||||
|
return e.names[idx], true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
@(require_results)
|
||||||
|
enum_name_from_value_any :: proc(value: any) -> (name: string, ok: bool) {
|
||||||
|
if value.id == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ti := type_info_base(type_info_of(value.id))
|
||||||
|
e := ti.variant.(runtime.Type_Info_Enum) or_return
|
||||||
|
if len(e.values) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ev := Type_Info_Enum_Value(as_i64(value) or_return)
|
||||||
|
for val, idx in e.values {
|
||||||
|
if val == ev {
|
||||||
|
return e.names[idx], true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@(require_results)
|
@(require_results)
|
||||||
enum_field_names :: proc(Enum_Type: typeid) -> []string {
|
enum_field_names :: proc(Enum_Type: typeid) -> []string {
|
||||||
|
|||||||
Reference in New Issue
Block a user