Created
August 14, 2017 17:44
-
-
Save jakesays-old/1cb97007550ee8ce70ba852c2df95ec4 to your computer and use it in GitHub Desktop.
Struct that tracks the dirty state of a property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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