[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static EnumData Data(Type enumType, object Value) { /* /!\ This conversion will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ if (enumType is null) { throw new ArgumentNullException(nameof(enumType)); } int vIdx = CastTo <int> .From(Value); if (vIdx < 0) { throw new IndexOutOfRangeException(); } Contract.EndContractBlock(); int enumIndex = EnumMetaTable.Meta.Lookup(enumType.TypeHandle); if (enumIndex > -1) { if (vIdx > 0 && vIdx < EnumMetaTable.Count(enumIndex)) { var dataLookup = EnumMetaTable.Get(enumIndex, vIdx); return(dataLookup); } } throw new Exception($"Unable to find keyword for enum value {System.Enum.GetName(enumType, Value)} in meta-enum table"); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static bool TryKeyword(Type enumType, object Value, out string outKeyword) { if (enumType is null) { throw new ArgumentNullException(nameof(enumType)); } if (Value is null) { throw new ArgumentNullException(nameof(Value)); } Contract.EndContractBlock(); int enumIndex = EnumMetaTable.Meta.Lookup(enumType.TypeHandle); if (enumIndex < 0) { /* Enum has no index */ outKeyword = null; return(false); } /* /!\ This conversion will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ //outKeyword = EnumMetaTable.Get(enumIndex, CastTo<int>.From(Value)).Keyword; outKeyword = EnumMetaTable.Get(enumIndex, CastTo <int> .From(Value)).Keyword; return(true); }
/// <summary> /// Converts a string name into a positive integer value, preferrably via enum lookup. /// A return value of <c>null</c> will cause the name to be registered as a custom one. /// </summary> /// <param name="Name"></param> protected virtual int?Name_To_Value(string Name) { if (Lookup.TryEnum <T>(Name, out T outEnum)) { return(CastTo <Int32> .From <T>(outEnum)); } return(null);// name.GetHashCode(); }
protected T Value_To_Enum(int value) { T enumValue = CastTo <T> .From <int>(value); if (Lookup.TryKeyword <T>(enumValue, out string LUT) && !(LUT is null)) { return(enumValue); } throw new ArgumentOutOfRangeException(nameof(value)); }
protected bool Check_If_Value_Is_Custom(int value) { T enumValue = CastTo <T> .From <int>(value); if (Lookup.TryKeyword <T>(enumValue, out string LUT) && !(LUT is null)) { return(false); } return(true); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static bool TryData <T>(T Value, out EnumData outData) where T : struct { int enumIndex = EnumMetaTable.Meta.Lookup <T>(); if (enumIndex < 0) { /* Enum has no index */ outData = default; return(false); } /* /!\ This conversion will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ outData = EnumMetaTable.Get(enumIndex, CastTo <int> .From(Value)); return(true); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static string Keyword <T>(T Value) where T : struct { int enumIndex = EnumMetaTable.Meta.Lookup <T>(); if (enumIndex > -1) { /* /!\ This conversion will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ string keyword = EnumMetaTable.Get(enumIndex, CastTo <int> .From(Value)).Keyword; if (keyword is object) { return(keyword); } } throw new Exception($"Unable to find keyword for enum value {System.Enum.GetName(typeof(T), Value)} in meta-enum table"); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static bool TryKeyword <T>(T Value, out string outKeyword) where T : struct { int enumIndex = EnumMetaTable.Meta.Lookup <T>(); if (enumIndex < 0) { /* Enum has no index */ outKeyword = null; return(false); } /* /!\ This conversion will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ //outKeyword = EnumMetaTable.Get(enumIndex, CastTo<int>.From(Value)).Keyword; outKeyword = EnumMetaTable.Get(enumIndex, CastTo <int> .From(Value)).Keyword; return(true); }
/// <summary> /// Creates an name specifier from a Value index /// </summary> /// <param name="ValueID"></param> public AtomicName(int ValueID) { Name = Value_To_Name(ValueID); Value = ValueID; T enumValue = CastTo <T> .From <int>(Value); if (Lookup.TryKeyword <T>(enumValue, out string LUT) && !(LUT is null)) { EnumValue = enumValue; IsCustom = false; } else { IsCustom = true; } }
[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static bool TryEnum <T>(AtomicString Keyword, out T outEnum) where T : struct { int enumIndex = EnumMetaTable.Meta.Lookup <T>(); if (enumIndex < 0) { /* Enum has no index */ outEnum = default; return(false); } if (!EnumMetaTable.KEYWORD[enumIndex].TryGetValue(Keyword, out object outValue)) { outEnum = default; return(false); } outEnum = CastTo <T> .From(outValue); return(true); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static string Keyword(Type enumType, object Value) { if (enumType is null) { throw new ArgumentNullException(nameof(enumType)); } Contract.EndContractBlock(); int enumIndex = EnumMetaTable.Meta.Lookup(enumType.TypeHandle); if (enumIndex > -1) { /* /!\ This conversion will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ string keyword = EnumMetaTable.Get(enumIndex, CastTo <int> .From(Value)).Keyword; if (keyword is object) { return(keyword); } } throw new Exception($"Unable to find keyword for enum value {System.Enum.GetName(enumType, Value)} in meta-enum table"); }
/// <summary> /// Converts our integer value into its string name, supposedly through an enum lookup table /// </summary> /// <param name="value"></param> protected virtual string Value_To_Name(int value) { if (!IsCustom) { /* /!\ These conversions will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ T enumValue = CastTo <T> .From <int>(value); if (Lookup.TryKeyword <T>(enumValue, out string LUT) && !(LUT is null)) { return(LUT); } } /* Alright buster, we gotta do things the slow way */ if (NameRegistry.TryGetKey(value, out AtomicString name)) { return(name); } /* Well we tried */ return(null); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]// Small function which is called frequently in loops, inline it public static EnumData Data <T>(T Value) where T : struct { /* /!\ This conversion will f*****g EXPLODE if the given generic type does not have an integer backing type /!\ */ int vIdx = CastTo <int> .From(Value); if (vIdx < 0) { throw new IndexOutOfRangeException(); } Contract.EndContractBlock(); int enumIndex = EnumMetaTable.Meta.Lookup <T>(); if (enumIndex > -1) { if (vIdx > 0 && vIdx < EnumMetaTable.Count(enumIndex)) { var dataLookup = EnumMetaTable.Get(enumIndex, vIdx); return(dataLookup); } } throw new Exception($"Unable to find keyword for enum value {System.Enum.GetName(typeof(T), Value)} in meta-enum table"); }
/// <summary> /// Converts an enum value into a positive integer value, preferrably via LUT lookup or else this class wont be performant. /// </summary> /// <param name="enumValue"></param> protected virtual int?Enum_To_Value(T enumValue) { //return Convert.ToInt32(enumValue); return(CastTo <Int32> .From <T>(enumValue)); }