Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Created November 16, 2017 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakesays-old/1b0cdf1f79eeb711d70952ba6b42ce53 to your computer and use it in GitHub Desktop.
Save jakesays-old/1b0cdf1f79eeb711d70952ba6b42ce53 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.CompilerServices;
namespace Std.BasicTypes
{
[Serializable]
public struct TrackedBackingStore<TStoreType>
{
private TStoreType _value;
private readonly PropertyTrackingManager _manager;
private readonly int _stateIndex;
private Func<TStoreType, TStoreType, bool> _equalOperator;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TrackedBackingStore(PropertyTrackingManager manager, int id,
Func<TStoreType, TStoreType, bool> equalOperator)
: this()
{
_manager = manager;
_stateIndex = id;
_equalOperator = equalOperator;
}
public TStoreType Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Set(value);
}
public bool HasChanged
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _manager.State.Get(_stateIndex);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TStoreType Get()
{
return _value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(TStoreType value)
{
if (_equalOperator(_value, value))
{
return;
}
_value = value;
_manager.SetState(_stateIndex);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeValue(TStoreType value)
{
_value = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment