rabidus-test/Assets/Plugins/QFSW/Quantum Console/Source/Scripts/Parsing/PolymorphicCachedQcParser.cs

29 lines
985 B
C#
Raw Permalink Normal View History

2023-08-22 15:41:12 +03:00
using System;
using System.Collections.Generic;
namespace QFSW.QC
{
/// <summary>
/// Parser for all types inheriting from a single type.
/// Caches results and reuses them if the incoming string has already been parsed.
/// </summary>
/// <typeparam name="T">Base type of the types to parse.</typeparam>
public abstract class PolymorphicCachedQcParser<T> : PolymorphicQcParser<T> where T : class
{
private readonly Dictionary<(string, Type), T> _cacheLookup = new Dictionary<(string, Type), T>();
public override object Parse(string value, Type type, Func<string, Type, object> recursiveParser)
{
(string value, Type type) key = (value, type);
if (_cacheLookup.ContainsKey(key))
{
return _cacheLookup[key];
}
T result = (T)base.Parse(value, type, recursiveParser);
_cacheLookup[key] = result;
return result;
}
}
}