mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-27 20:18:01 +02:00
Add Type
overloads to Enum methods
This commit is contained in:
parent
08d292d3ea
commit
d1aafda19e
1 changed files with 170 additions and 39 deletions
|
@ -6,11 +6,10 @@ namespace System
|
||||||
struct Enum
|
struct Enum
|
||||||
{
|
{
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
[Comptime(ConstEval=true)]
|
public static int GetCount(Type type)
|
||||||
public static int GetCount<T>() where T : Enum
|
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (var field in typeof(T).GetFields())
|
for (var field in type.GetFields())
|
||||||
{
|
{
|
||||||
if (field.IsEnumCase)
|
if (field.IsEnumCase)
|
||||||
count++;
|
count++;
|
||||||
|
@ -18,24 +17,47 @@ namespace System
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
[Comptime(ConstEval=true)]
|
||||||
|
public static int GetCount<T>() where T : Enum
|
||||||
|
{
|
||||||
|
return GetCount(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public static int64 GetMinValue(Type type)
|
||||||
|
{
|
||||||
|
int64? minValue = null;
|
||||||
|
for (var data in GetValues(type))
|
||||||
|
{
|
||||||
|
if (minValue == null)
|
||||||
|
minValue = data;
|
||||||
|
else
|
||||||
|
minValue = Math.Min(minValue.Value, data);
|
||||||
|
}
|
||||||
|
return minValue.ValueOrDefault;
|
||||||
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
[Comptime(ConstEval=true)]
|
[Comptime(ConstEval=true)]
|
||||||
public static var GetMinValue<T>() where T : Enum
|
public static var GetMinValue<T>() where T : Enum
|
||||||
{
|
{
|
||||||
Compiler.SetReturnType(typeof(T));
|
Compiler.SetReturnType(typeof(T));
|
||||||
|
return GetMinValue(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
int? minValue = null;
|
[NoShow(true)]
|
||||||
for (var field in typeof(T).GetFields())
|
public static int64 GetMaxValue(Type type)
|
||||||
{
|
{
|
||||||
if (field.IsEnumCase)
|
int64? maxValue = null;
|
||||||
|
for (var data in GetValues(type))
|
||||||
{
|
{
|
||||||
if (minValue == null)
|
if (maxValue == null)
|
||||||
minValue = field.[Friend]mFieldData.mData;
|
maxValue = data;
|
||||||
else
|
else
|
||||||
minValue = Math.Min(minValue.Value, field.[Friend]mFieldData.mData);
|
maxValue = Math.Max(maxValue.Value, data);
|
||||||
}
|
}
|
||||||
}
|
return maxValue ?? -1;
|
||||||
return minValue.ValueOrDefault;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
|
@ -43,38 +65,37 @@ namespace System
|
||||||
public static var GetMaxValue<T>() where T : Enum
|
public static var GetMaxValue<T>() where T : Enum
|
||||||
{
|
{
|
||||||
Compiler.SetReturnType(typeof(T));
|
Compiler.SetReturnType(typeof(T));
|
||||||
|
return GetMaxValue(typeof(T));
|
||||||
int? maxValue = null;
|
|
||||||
for (var field in typeof(T).GetFields())
|
|
||||||
{
|
|
||||||
if (field.IsEnumCase)
|
|
||||||
{
|
|
||||||
if (maxValue == null)
|
|
||||||
maxValue = field.[Friend]mFieldData.mData;
|
|
||||||
else
|
|
||||||
maxValue = Math.Max(maxValue.Value, field.[Friend]mFieldData.mData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (maxValue == null)
|
|
||||||
return -1;
|
|
||||||
return maxValue.ValueOrDefault;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public static void EnumToString(Type type, String strBuffer, int64 iVal)
|
public static void EnumToString(Type type, String strBuffer, int64 iVal)
|
||||||
{
|
{
|
||||||
for (var field in type.GetFields())
|
for (var (name, data) in GetEnumerator(type))
|
||||||
{
|
{
|
||||||
if (field.[Friend]mFieldData.mFlags.HasFlag(.EnumCase) &&
|
if (data == iVal)
|
||||||
*(int64*)&field.[Friend]mFieldData.[Friend]mData == iVal)
|
|
||||||
{
|
{
|
||||||
strBuffer.Append(field.Name);
|
strBuffer.Append(name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iVal.ToString(strBuffer);
|
iVal.ToString(strBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public static Result<int64> Parse(Type type, StringView str, bool ignoreCase = false)
|
||||||
|
{
|
||||||
|
for (var (name, data) in GetEnumerator(type))
|
||||||
|
{
|
||||||
|
if (str.Equals(name, ignoreCase))
|
||||||
|
return .Ok(data);
|
||||||
|
if (int64.Parse(str) case .Ok(let val) && val == data)
|
||||||
|
return .Ok(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return .Err;
|
||||||
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public static Result<T> Parse<T>(StringView str, bool ignoreCase = false) where T : enum
|
public static Result<T> Parse<T>(StringView str, bool ignoreCase = false) where T : enum
|
||||||
{
|
{
|
||||||
|
@ -89,6 +110,18 @@ namespace System
|
||||||
return .Err;
|
return .Err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public static bool IsDefined(Type type, int64 value)
|
||||||
|
{
|
||||||
|
for (var data in GetValues(type))
|
||||||
|
{
|
||||||
|
if (data == value)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public static bool IsDefined<T>(T value) where T : Enum
|
public static bool IsDefined<T>(T value) where T : Enum
|
||||||
where T : enum
|
where T : enum
|
||||||
|
@ -102,6 +135,12 @@ namespace System
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public static EnumEnumerator GetEnumerator(Type type)
|
||||||
|
{
|
||||||
|
return .(type);
|
||||||
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public static EnumEnumerator<TEnum> GetEnumerator<TEnum>()
|
public static EnumEnumerator<TEnum> GetEnumerator<TEnum>()
|
||||||
where TEnum : enum
|
where TEnum : enum
|
||||||
|
@ -109,6 +148,12 @@ namespace System
|
||||||
return .();
|
return .();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public static EnumValuesEnumerator GetValues(Type type)
|
||||||
|
{
|
||||||
|
return .(type);
|
||||||
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public static EnumValuesEnumerator<TEnum> GetValues<TEnum>()
|
public static EnumValuesEnumerator<TEnum> GetValues<TEnum>()
|
||||||
where TEnum : enum
|
where TEnum : enum
|
||||||
|
@ -116,6 +161,12 @@ namespace System
|
||||||
return .();
|
return .();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public static EnumNamesEnumerator GetNames(Type type)
|
||||||
|
{
|
||||||
|
return .(type);
|
||||||
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public static EnumNamesEnumerator<TEnum> GetNames<TEnum>()
|
public static EnumNamesEnumerator<TEnum> GetNames<TEnum>()
|
||||||
where TEnum : enum
|
where TEnum : enum
|
||||||
|
@ -124,15 +175,14 @@ namespace System
|
||||||
}
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
private struct EnumFieldsEnumerator<TEnum>
|
private struct EnumFieldsEnumerator
|
||||||
where TEnum : enum
|
|
||||||
{
|
{
|
||||||
TypeInstance mTypeInstance;
|
TypeInstance mTypeInstance;
|
||||||
int32 mIdx;
|
int32 mIdx;
|
||||||
|
|
||||||
public this()
|
public this(Type type)
|
||||||
{
|
{
|
||||||
mTypeInstance = typeof(TEnum) as TypeInstance;
|
mTypeInstance = type as TypeInstance;
|
||||||
mIdx = -1;
|
mIdx = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,9 +240,36 @@ namespace System
|
||||||
}
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public struct EnumEnumerator<TEnum> : EnumFieldsEnumerator<TEnum>, IEnumerator<(StringView name, TEnum value)>
|
public struct EnumEnumerator : EnumFieldsEnumerator, IEnumerator<(StringView name, int64 value)>
|
||||||
|
{
|
||||||
|
public this(Type type) : base(type)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public new (StringView name, int64 value) Current
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return ((.)base.Current.[Friend]mFieldData.[Friend]mName, *(int64*)&base.Current.[Friend]mFieldData.[Friend]mData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public new Result<(StringView name, int64 value)> GetNext() mut
|
||||||
|
{
|
||||||
|
if (!MoveNext())
|
||||||
|
return .Err;
|
||||||
|
return Current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public struct EnumEnumerator<TEnum> : EnumFieldsEnumerator, IEnumerator<(StringView name, TEnum value)>
|
||||||
where TEnum : enum
|
where TEnum : enum
|
||||||
{
|
{
|
||||||
|
public this() : base(typeof(TEnum))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public new (StringView name, TEnum value) Current
|
public new (StringView name, TEnum value) Current
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -210,9 +287,36 @@ namespace System
|
||||||
}
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public struct EnumValuesEnumerator<TEnum> : EnumFieldsEnumerator<TEnum>, IEnumerator<TEnum>
|
public struct EnumValuesEnumerator : EnumFieldsEnumerator, IEnumerator<int64>
|
||||||
|
{
|
||||||
|
public this(Type type) : base(type)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public new int64 Current
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return *(int64*)&base.Current.[Friend]mFieldData.[Friend]mData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public new Result<int64> GetNext() mut
|
||||||
|
{
|
||||||
|
if (!MoveNext())
|
||||||
|
return .Err;
|
||||||
|
return Current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public struct EnumValuesEnumerator<TEnum> : EnumFieldsEnumerator, IEnumerator<TEnum>
|
||||||
where TEnum : enum
|
where TEnum : enum
|
||||||
{
|
{
|
||||||
|
public this() : base(typeof(TEnum))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public new TEnum Current
|
public new TEnum Current
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
@ -230,9 +334,36 @@ namespace System
|
||||||
}
|
}
|
||||||
|
|
||||||
[NoShow(true)]
|
[NoShow(true)]
|
||||||
public struct EnumNamesEnumerator<TEnum> : EnumFieldsEnumerator<TEnum>, IEnumerator<StringView>
|
public struct EnumNamesEnumerator : EnumFieldsEnumerator, IEnumerator<StringView>
|
||||||
|
{
|
||||||
|
public this(Type type) : base(type)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public new StringView Current
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return (.)base.Current.[Friend]mFieldData.[Friend]mName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public new Result<StringView> GetNext() mut
|
||||||
|
{
|
||||||
|
if (!MoveNext())
|
||||||
|
return .Err;
|
||||||
|
return Current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[NoShow(true)]
|
||||||
|
public struct EnumNamesEnumerator<TEnum> : EnumFieldsEnumerator, IEnumerator<StringView>
|
||||||
where TEnum : enum
|
where TEnum : enum
|
||||||
{
|
{
|
||||||
|
public this() : base(typeof(TEnum))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public new StringView Current
|
public new StringView Current
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue