Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Created August 14, 2017 17:44
Show Gist options
  • Save jakesays-old/1cb97007550ee8ce70ba852c2df95ec4 to your computer and use it in GitHub Desktop.
Save jakesays-old/1cb97007550ee8ce70ba852c2df95ec4 to your computer and use it in GitHub Desktop.
Struct that tracks the dirty state of a property
using System;
using System.Collections;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Std.BasicTypes
{
[Serializable]
public struct TrackedBackingStore<TStoreType>
{
private TStoreType _value;
private readonly StateVector _state;
private readonly int _stateIndex;
private readonly Func<TStoreType, TStoreType, bool> _equalOperator;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TrackedBackingStore(PropertyTrackingManager manager, int id,
Func<TStoreType, TStoreType, bool> equalOperator)
: this()
{
_state = manager.State;
_stateIndex = id;
_equalOperator = equalOperator;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TStoreType Get()
{
return _value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Set(TStoreType value)
{
if (_equalOperator(_value, value))
{
return;
}
_value = value;
_state.Set(_stateIndex, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment