From d52e4793fe1c240a3dcd6e6ea37eb27514c2e3d4 Mon Sep 17 00:00:00 2001 From: teinarss Date: Mon, 6 Jul 2020 20:27:24 +0200 Subject: [PATCH] Refactor classes to structs --- OpenRA.Game/Map/CellRegion.cs | 4 +++- OpenRA.Game/Map/ProjectedCellRegion.cs | 4 +++- OpenRA.Game/TraitDictionary.cs | 8 ++++--- OpenRA.Mods.Common/Traits/World/ActorMap.cs | 25 +++++++++++++++------ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/OpenRA.Game/Map/CellRegion.cs b/OpenRA.Game/Map/CellRegion.cs index 7acf253748..e4707149dd 100644 --- a/OpenRA.Game/Map/CellRegion.cs +++ b/OpenRA.Game/Map/CellRegion.cs @@ -117,7 +117,7 @@ namespace OpenRA return GetEnumerator(); } - public sealed class CellRegionEnumerator : IEnumerator + public struct CellRegionEnumerator : IEnumerator { readonly CellRegion r; @@ -128,9 +128,11 @@ namespace OpenRA CPos current; public CellRegionEnumerator(CellRegion region) + : this() { r = region; Reset(); + current = new MPos(u, v).ToCPos(r.gridType); } public bool MoveNext() diff --git a/OpenRA.Game/Map/ProjectedCellRegion.cs b/OpenRA.Game/Map/ProjectedCellRegion.cs index 68f271bcdf..985d892949 100644 --- a/OpenRA.Game/Map/ProjectedCellRegion.cs +++ b/OpenRA.Game/Map/ProjectedCellRegion.cs @@ -76,7 +76,7 @@ namespace OpenRA return GetEnumerator(); } - public sealed class ProjectedCellRegionEnumerator : IEnumerator + public struct ProjectedCellRegionEnumerator : IEnumerator { readonly ProjectedCellRegion r; @@ -86,9 +86,11 @@ namespace OpenRA PPos current; public ProjectedCellRegionEnumerator(ProjectedCellRegion region) + : this() { r = region; Reset(); + current = new PPos(u, v); } public bool MoveNext() diff --git a/OpenRA.Game/TraitDictionary.cs b/OpenRA.Game/TraitDictionary.cs index 93262299af..774563c27c 100644 --- a/OpenRA.Game/TraitDictionary.cs +++ b/OpenRA.Game/TraitDictionary.cs @@ -179,13 +179,14 @@ namespace OpenRA System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } - class MultipleEnumerator : IEnumerator + struct MultipleEnumerator : IEnumerator { readonly List actors; readonly List traits; readonly uint actor; int index; public MultipleEnumerator(TraitContainer container, uint actor) + : this() { actors = container.actors; traits = container.traits; @@ -236,7 +237,7 @@ namespace OpenRA } } - class AllEnumerable : IEnumerable> + struct AllEnumerable : IEnumerable> { readonly TraitContainer container; public AllEnumerable(TraitContainer container) { this.container = container; } @@ -244,12 +245,13 @@ namespace OpenRA System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } - class AllEnumerator : IEnumerator> + struct AllEnumerator : IEnumerator> { readonly List actors; readonly List traits; int index; public AllEnumerator(TraitContainer container) + : this() { actors = container.actors; traits = container.traits; diff --git a/OpenRA.Mods.Common/Traits/World/ActorMap.cs b/OpenRA.Mods.Common/Traits/World/ActorMap.cs index 238fb1bb24..73eea60cd7 100644 --- a/OpenRA.Mods.Common/Traits/World/ActorMap.cs +++ b/OpenRA.Mods.Common/Traits/World/ActorMap.cs @@ -216,21 +216,32 @@ namespace OpenRA.Mods.Common.Traits } } - sealed class ActorsAtEnumerator : IEnumerator + struct ActorsAtEnumerator : IEnumerator { InfluenceNode node; - public ActorsAtEnumerator(InfluenceNode node) { this.node = node; } + Actor current; + + public ActorsAtEnumerator(InfluenceNode node) + { + this.node = node; + current = null; + } + public void Reset() { throw new NotSupportedException(); } - public Actor Current { get; private set; } - object IEnumerator.Current { get { return Current; } } + public Actor Current + { + get { return current; } + } + + object IEnumerator.Current { get { return current; } } public void Dispose() { } public bool MoveNext() { while (node != null) { - Current = node.Actor; + current = node.Actor; node = node.Next; - if (!Current.Disposed) + if (!current.Disposed) return true; } @@ -238,7 +249,7 @@ namespace OpenRA.Mods.Common.Traits } } - sealed class ActorsAtEnumerable : IEnumerable + struct ActorsAtEnumerable : IEnumerable { readonly InfluenceNode node; public ActorsAtEnumerable(InfluenceNode node) { this.node = node; }