mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-24 13:38:34 +01:00
Compare commits
No commits in common. "dd09f0af25529baa85ee770e505cf94ffac69504" and "cca02b53961c2cf66fc1c020013c773aea1998e8" have entirely different histories.
dd09f0af25
...
cca02b5396
1 changed files with 10 additions and 45 deletions
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using MLEM.Data.Json;
|
||||
using MLEM.Misc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data {
|
||||
|
@ -25,16 +26,13 @@ namespace MLEM.Data {
|
|||
/// </code>
|
||||
/// </remarks>
|
||||
[JsonConverter(typeof(DynamicEnumConverter))]
|
||||
public abstract class DynamicEnum {
|
||||
public abstract class DynamicEnum : GenericDataHolder {
|
||||
|
||||
private static readonly Dictionary<Type, Dictionary<BigInteger, DynamicEnum>> Values = new Dictionary<Type, Dictionary<BigInteger, DynamicEnum>>();
|
||||
private static readonly Dictionary<Type, Dictionary<BigInteger, DynamicEnum>> FlagCache = new Dictionary<Type, Dictionary<BigInteger, DynamicEnum>>();
|
||||
private static readonly Dictionary<Type, Dictionary<string, DynamicEnum>> ParseCache = new Dictionary<Type, Dictionary<string, DynamicEnum>>();
|
||||
private static readonly Dictionary<string, DynamicEnum> ParseCache = new Dictionary<string, DynamicEnum>();
|
||||
|
||||
private readonly BigInteger value;
|
||||
|
||||
private Dictionary<DynamicEnum, bool> allFlagsCache;
|
||||
private Dictionary<DynamicEnum, bool> anyFlagsCache;
|
||||
private string name;
|
||||
|
||||
/// <summary>
|
||||
|
@ -49,38 +47,13 @@ namespace MLEM.Data {
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this enum value has ALL of the given <see cref="DynamicEnum"/> flags on it.
|
||||
/// Returns true if this enum value has the given <see cref="DynamicEnum"/> flag on it.
|
||||
/// This operation is equivalent to <see cref="Enum.HasFlag"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="HasAnyFlag"/>
|
||||
/// <param name="flags">The flags to query</param>
|
||||
/// <returns>True if all of the flags are present, false otherwise</returns>
|
||||
public bool HasFlag(DynamicEnum flags) {
|
||||
if (this.allFlagsCache == null)
|
||||
this.allFlagsCache = new Dictionary<DynamicEnum, bool>();
|
||||
if (!this.allFlagsCache.TryGetValue(flags, out var ret)) {
|
||||
// & is very memory-intensive, so we cache the return value
|
||||
ret = (GetValue(this) & GetValue(flags)) == GetValue(flags);
|
||||
this.allFlagsCache.Add(flags, ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this enum value has ANY of the given <see cref="DynamicEnum"/> flags on it
|
||||
/// </summary>
|
||||
/// <seealso cref="HasFlag"/>
|
||||
/// <param name="flags">The flags to query</param>
|
||||
/// <returns>True if one of the flags is present, false otherwise</returns>
|
||||
public bool HasAnyFlag(DynamicEnum flags) {
|
||||
if (this.anyFlagsCache == null)
|
||||
this.anyFlagsCache = new Dictionary<DynamicEnum, bool>();
|
||||
if (!this.anyFlagsCache.TryGetValue(flags, out var ret)) {
|
||||
// & is very memory-intensive, so we cache the return value
|
||||
ret = (GetValue(this) & GetValue(flags)) != 0;
|
||||
this.anyFlagsCache.Add(flags, ret);
|
||||
}
|
||||
return ret;
|
||||
/// <param name="flag">The flag to query</param>
|
||||
/// <returns>True if the flag is present, false otherwise</returns>
|
||||
public bool HasFlag(DynamicEnum flag) {
|
||||
return (GetValue(this) & GetValue(flag)) == GetValue(flag);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
@ -112,10 +85,6 @@ namespace MLEM.Data {
|
|||
Values.Add(typeof(T), dict);
|
||||
}
|
||||
|
||||
// cached parsed values and names might be incomplete with new values
|
||||
FlagCache.Remove(typeof(T));
|
||||
ParseCache.Remove(typeof(T));
|
||||
|
||||
if (dict.ContainsKey(value))
|
||||
throw new ArgumentException($"Duplicate value {value}", nameof(value));
|
||||
if (dict.Values.Any(v => v.name == name))
|
||||
|
@ -285,11 +254,7 @@ namespace MLEM.Data {
|
|||
/// <param name="strg">The string to parse into a dynamic enum value</param>
|
||||
/// <returns>The parsed enum value, or null if parsing fails</returns>
|
||||
public static DynamicEnum Parse(Type type, string strg) {
|
||||
if (!ParseCache.TryGetValue(type, out var cache)) {
|
||||
cache = new Dictionary<string, DynamicEnum>();
|
||||
ParseCache.Add(type, cache);
|
||||
}
|
||||
if (!cache.TryGetValue(strg, out var cached)) {
|
||||
if (!ParseCache.TryGetValue(strg, out var cached)) {
|
||||
BigInteger? accum = null;
|
||||
foreach (var val in strg.Split('|')) {
|
||||
foreach (var defined in GetValues(type)) {
|
||||
|
@ -301,7 +266,7 @@ namespace MLEM.Data {
|
|||
}
|
||||
if (accum != null)
|
||||
cached = GetEnumValue(type, accum.Value);
|
||||
cache.Add(strg, cached);
|
||||
ParseCache.Add(strg, cached);
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue