Created
November 16, 2017 23:38
-
-
Save jakesays-old/1b0cdf1f79eeb711d70952ba6b42ce53 to your computer and use it in GitHub Desktop.
This file contains 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.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