Skip to content

Commit 71d6c4a

Browse files
committed
Get methods for props
1 parent 16ba9e7 commit 71d6c4a

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

Source/DynamicProperties/Props.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Collections.Generic;
35
using System.Runtime.CompilerServices;
@@ -13,21 +15,21 @@ public sealed class Props : Disposable, IComparable<Props>
1315
private static uint _idCounter = 0;
1416
private static uint _nextId() => _idCounter++;
1517

16-
public readonly uint UniqueId = _nextId();
18+
public uint UniqueId { get; } = _nextId();
1719

18-
public readonly int Priority;
20+
public int Priority { get; }
1921

2022
private readonly Dictionary<int, Prop> props = [];
2123

2224
internal IEnumerable<int> ManagedIds => props.Keys;
2325

2426
internal delegate void EntriesChangedHandler(Props props);
2527

26-
internal EntriesChangedHandler OnEntriesChanged = null;
28+
internal EntriesChangedHandler? OnEntriesChanged = null;
2729

2830
internal delegate void ValueChangedHandler(Props props, int? id);
2931

30-
internal ValueChangedHandler OnValueChanged = null;
32+
internal ValueChangedHandler? OnValueChanged = null;
3133

3234
internal bool SuppressEagerUpdate = false;
3335
internal bool NeedsEntriesUpdate = false;
@@ -43,10 +45,10 @@ public Props(int priority)
4345
}
4446

4547
/// Ordered by lowest to highest priority. Equal priority is disambiguated by unique IDs.
46-
public int CompareTo(Props other)
48+
public int CompareTo(Props? other)
4749
{
4850
if (ReferenceEquals(this, other)) return 0;
49-
if (other is null) return 1;
51+
if (other == null) return 1;
5052
var priorityCmp = Priority.CompareTo(other.Priority);
5153
return priorityCmp != 0 ? priorityCmp : UniqueId.CompareTo(other.UniqueId);
5254
}
@@ -137,7 +139,7 @@ public bool Remove(int id)
137139

138140
#endregion
139141

140-
#region Has
142+
#region Has/Get
141143

142144
[MethodImpl(MethodImplOptions.AggressiveInlining)]
143145
private bool _internalHas<T>(int id) => props.TryGetValue(id, out var prop) && prop is Prop<T>;
@@ -148,6 +150,18 @@ public bool Remove(int id)
148150
public bool HasTexture(int id) => _internalHas<Texture>(id);
149151
public bool HasVector(int id) => _internalHas<Vector4>(id);
150152

153+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
154+
private T? _internalGet<T, TProp>(int id) where TProp : Prop<T> =>
155+
props.TryGetValue(id, out var prop) && prop is TProp typedProp
156+
? typedProp.Value
157+
: default;
158+
159+
public Color GetColorOrDefault(int id) => _internalGet<Color, PropColor>(id);
160+
public float GetFloatOrDefault(int id) => _internalGet<float, PropFloat>(id);
161+
public int GetIntOrDefault(int id) => _internalGet<int, PropInt>(id);
162+
public Texture? GetTextureOrDefault(int id) => _internalGet<Texture, PropTexture>(id);
163+
public Vector4 GetVectorOrDefault(int id) => _internalGet<Vector4, PropVector>(id);
164+
151165
#endregion
152166

153167
[MethodImpl(MethodImplOptions.AggressiveInlining)]

0 commit comments

Comments
 (0)