38 lines
666 B
C#
38 lines
666 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BITKit
|
|
{
|
|
|
|
public interface IDoubleBuffer<T>
|
|
{
|
|
T Current { get; }
|
|
void Release(T newValue);
|
|
event Action<T> OnRelease;
|
|
bool TryGetRelease(out T result);
|
|
}
|
|
|
|
public class DoubleBuffer<T> : IDoubleBuffer<T>
|
|
{
|
|
public T Current
|
|
{
|
|
get;
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
|
protected set;
|
|
}
|
|
|
|
public void Release(T newValue)
|
|
{
|
|
Current = newValue;
|
|
OnRelease?.Invoke(newValue);
|
|
}
|
|
|
|
public event Action<T> OnRelease;
|
|
private readonly Queue<T> _releases = new();
|
|
|
|
public bool TryGetRelease(out T result)
|
|
{
|
|
return _releases.TryDequeue(out result);
|
|
}
|
|
}
|
|
} |