Skip to content

Commit

Permalink
ECS - add readonly struct IdRevision
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Aug 3, 2024
1 parent 0042cf1 commit aea8eaf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ECS/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public ref readonly Tags Tags { get {
/// <summary>Unique entity id.<br/>
/// Uniqueness relates to the <see cref="Entity"/>'s stored in its <see cref="EntityStore"/></summary>
[Browse(Never)]
[FieldOffset(8)] internal readonly long idRevision; // (8) - 4 (Id) + 2 (Revision) + 2 (padding)
[FieldOffset(8)] internal readonly IdRevision idRevision; // (8)
// ReSharper disable once InconsistentNaming
[FieldOffset(8)] public readonly int Id; // 4
[Browse(Never)]
Expand Down
32 changes: 32 additions & 0 deletions src/ECS/Entity/IdRevision.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Ullrich Praetz - https://github.com/friflo. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;
using static System.Diagnostics.DebuggerBrowsableState;
using Browse = System.Diagnostics.DebuggerBrowsableAttribute;

// ReSharper disable once CheckNamespace
namespace Friflo.Engine.ECS;

[StructLayout(LayoutKind.Explicit)]
internal readonly struct IdRevision : IEquatable<IdRevision>
{
[FieldOffset(0)] public readonly int Id; // 4
[FieldOffset(4)] public readonly short Revision; // 2

[Browse(Never)]
[FieldOffset(0)] internal readonly long value; // (8) - 4 (Id) + 2 (Revision) + 2 (padding)

public bool Equals (IdRevision other) => value == other.value;
public static bool operator == (IdRevision a, IdRevision b) => a.value == b.value;
public static bool operator != (IdRevision a, IdRevision b) => a.value != b.value;

public override bool Equals(object obj) => throw new NotImplementedException("by intenetion to avoid boxing");
public override int GetHashCode() => Id ^ Revision;

internal IdRevision(int id, short revision) {
Id = id;
Revision = revision;
}
}
29 changes: 29 additions & 0 deletions src/Tests-internal/ECS/Test_Entity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using Friflo.Engine.ECS;
using NUnit.Framework;
using Tests.ECS;
using Tests.Utils;
using static NUnit.Framework.Assert;

// ReSharper disable RedundantTypeDeclarationBody
Expand All @@ -9,6 +12,32 @@ namespace Internal.ECS {

public static class Test_Entity
{
[Test]
public static void Test_Entity_IdRevision()
{
var dict = new Dictionary<IdRevision, int>();
var idRef11 = new IdRevision(1,1);
var idRef21 = new IdRevision(2,1);

IsTrue(idRef11 == idRef11);

Check warning on line 22 in src/Tests-internal/ECS/Test_Entity.cs

View workflow job for this annotation

GitHub Actions / build

Comparison made to same variable; did you mean to compare something else?

Check warning on line 22 in src/Tests-internal/ECS/Test_Entity.cs

View workflow job for this annotation

GitHub Actions / build

Comparison made to same variable; did you mean to compare something else?

Check warning on line 22 in src/Tests-internal/ECS/Test_Entity.cs

View workflow job for this annotation

GitHub Actions / build

Comparison made to same variable; did you mean to compare something else?

Check warning on line 22 in src/Tests-internal/ECS/Test_Entity.cs

View workflow job for this annotation

GitHub Actions / build

Comparison made to same variable; did you mean to compare something else?
IsTrue(idRef11 != idRef21);

dict.Add(idRef11, 11);
dict.Add(idRef21, 21);

AreEqual(2, dict.Count);
AreEqual(11, dict[new IdRevision(1,1)]);

var start = Mem.GetAllocatedBytes();
_ = dict[new IdRevision(1,1)];
Mem.AssertNoAlloc(start);

Throws<NotImplementedException>(() => {
object obj = idRef11;
_ = obj.Equals(idRef11);
});
}

[Test]
public static void Test_Entity_Components()
{
Expand Down

0 comments on commit aea8eaf

Please sign in to comment.