仿真平台内核初版 -tlib库 包含<sparc arm riscv powerPC>
This commit is contained in:
37
lib/termsharp/Misc/SimpleCache.cs
Normal file
37
lib/termsharp/Misc/SimpleCache.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Copyright (c) Antmicro
|
||||
//
|
||||
// Full license details are defined in the 'LICENSE' file.
|
||||
//
|
||||
using System;
|
||||
|
||||
namespace TermSharp.Misc
|
||||
{
|
||||
internal sealed class SimpleCache<TFrom, TTo> where TFrom : IGenerationAware
|
||||
{
|
||||
public SimpleCache(Func<TFrom, TTo> factory)
|
||||
{
|
||||
generation = -1;
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public TTo GetValue(TFrom from)
|
||||
{
|
||||
if(generation != from.Generation)
|
||||
{
|
||||
var resultAsDisposable = lastResult as IDisposable;
|
||||
if(resultAsDisposable != null)
|
||||
{
|
||||
resultAsDisposable.Dispose();
|
||||
}
|
||||
lastResult = factory(from);
|
||||
generation = from.Generation;
|
||||
}
|
||||
return lastResult;
|
||||
}
|
||||
|
||||
private TTo lastResult;
|
||||
private Func<TFrom, TTo> factory;
|
||||
private int generation;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user