| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | public delegate void Change<T>(T current, T previous); |
| | 5 | |
|
| | 6 | | public class BaseVariable<T> : IBaseVariable<T>, IEquatable<T> |
| | 7 | | { |
| | 8 | | public event Change<T> OnChange; |
| | 9 | |
|
| | 10 | | protected T value; |
| | 11 | |
|
| 0 | 12 | | public BaseVariable() { value = default; } |
| 0 | 13 | | public BaseVariable(T defaultValue) { value = defaultValue; } |
| | 14 | |
|
| 6 | 15 | | public T Get() { return value; } |
| | 16 | |
|
| 17092 | 17 | | public void Set(T newValue) { Set(newValue, !Equals(newValue)); } |
| | 18 | |
|
| | 19 | | public void Set(T newValue, bool notifyEvent) |
| | 20 | | { |
| 8658 | 21 | | var previous = value; |
| 8658 | 22 | | value = newValue; |
| | 23 | |
|
| 8658 | 24 | | if (notifyEvent) |
| | 25 | | { |
| 3627 | 26 | | OnChange?.Invoke(value, previous); |
| | 27 | | } |
| 5660 | 28 | | } |
| | 29 | |
|
| 8540 | 30 | | public virtual bool Equals(T other) { return EqualityComparer<T>.Default.Equals(value, other); } |
| | 31 | | } |