mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-08-25 20:16:05 -05:00
Refactoring
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public sealed class CacheEnum
|
||||
{
|
||||
public static T Parse<T>(string name, bool ignoreCase = false) where T : struct, Enum
|
||||
{
|
||||
if(ignoreCase)
|
||||
{
|
||||
return CacheParse<T>.ParseIgnoreCase(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CacheParse<T>.Parse(name);
|
||||
}
|
||||
}
|
||||
|
||||
public static T TryParseOrDefault<T>(string name, bool ignoreCase = false, T defaultValue=default(T)) where T : struct, Enum
|
||||
{
|
||||
try
|
||||
{
|
||||
if(ignoreCase)
|
||||
{
|
||||
return CacheParse<T>.ParseIgnoreCase(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CacheParse<T>.Parse(name);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public static T[] GetValues<T>() where T : struct, Enum
|
||||
{
|
||||
return CacheValues<T>.Values;
|
||||
}
|
||||
|
||||
private static class CacheParse<T> where T : struct, Enum
|
||||
{
|
||||
private static Dictionary<string, T> _values = new Dictionary<string, T>();
|
||||
private static Dictionary<string, T> _ignoreCaseValues = new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
static CacheParse()
|
||||
{
|
||||
}
|
||||
|
||||
public static T ParseIgnoreCase(string name)
|
||||
{
|
||||
if(_ignoreCaseValues.TryGetValue(name, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
T result;
|
||||
value = Enum.TryParse<T>(name, true, out result)
|
||||
? result
|
||||
: throw new ArgumentException(nameof(result));
|
||||
_ignoreCaseValues.Add(name, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public static T Parse(string name)
|
||||
{
|
||||
if(_values.TryGetValue(name, out var value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
T result;
|
||||
value = Enum.TryParse<T>(name, false, out result)
|
||||
? result
|
||||
: throw new ArgumentException(nameof(result));
|
||||
_values.Add(name, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class CacheValues<T> where T : struct, Enum
|
||||
{
|
||||
public static readonly T[] Values;
|
||||
|
||||
static CacheValues()
|
||||
{
|
||||
Values = Enum.GetValues(typeof(T)) as T[];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
@@ -7,14 +7,7 @@ namespace UniGLTF
|
||||
{
|
||||
public static T Parse<T>(string name, bool ignoreCase = false) where T : struct, Enum
|
||||
{
|
||||
if (ignoreCase)
|
||||
{
|
||||
return CachedEnumType<T>.ParseIgnoreCase(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CachedEnumType<T>.Parse(name);
|
||||
}
|
||||
return CachedEnumType<T>.Parse(name, ignoreCase);
|
||||
}
|
||||
|
||||
public static T TryParseOrDefault<T>(string name, bool ignoreCase = false, T defaultValue = default)
|
||||
@@ -22,14 +15,7 @@ namespace UniGLTF
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ignoreCase)
|
||||
{
|
||||
return CachedEnumType<T>.ParseIgnoreCase(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return CachedEnumType<T>.Parse(name);
|
||||
}
|
||||
return Parse<T>(name, ignoreCase: ignoreCase);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -5,8 +5,8 @@ namespace UniGLTF
|
||||
{
|
||||
internal static class CachedEnumType<T> where T : struct, Enum
|
||||
{
|
||||
private static Dictionary<string, T> _values = new Dictionary<string, T>();
|
||||
private static Dictionary<string, T> _ignoreCaseValues = new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase);
|
||||
private static readonly Dictionary<string, T> _values = new Dictionary<string, T>();
|
||||
private static readonly Dictionary<string, T> _ignoreCaseValues = new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase);
|
||||
private static T[] _allValues;
|
||||
|
||||
public static T[] Values
|
||||
@@ -22,38 +22,21 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
public static T ParseIgnoreCase(string name)
|
||||
public static T Parse(string name, bool ignoreCase)
|
||||
{
|
||||
if(_ignoreCaseValues.TryGetValue(name, out var value))
|
||||
var caches = ignoreCase ? _ignoreCaseValues : _values;
|
||||
if (caches.TryGetValue(name, out var ignoreCaseValue))
|
||||
{
|
||||
return value;
|
||||
return ignoreCaseValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
T result;
|
||||
value = Enum.TryParse<T>(name, true, out result)
|
||||
? result
|
||||
: throw new ArgumentException(nameof(result));
|
||||
_ignoreCaseValues.Add(name, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public static T Parse(string name)
|
||||
{
|
||||
if(_values.TryGetValue(name, out var value))
|
||||
if (Enum.TryParse<T>(name, ignoreCase, out var result))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
T result;
|
||||
value = Enum.TryParse<T>(name, false, out result)
|
||||
? result
|
||||
: throw new ArgumentException(nameof(result));
|
||||
_values.Add(name, value);
|
||||
return value;
|
||||
_values.Add(name, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new ArgumentException(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user