Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit beb78e7

Browse files
committed
Code cleanup and formatting
1 parent b6334a5 commit beb78e7

File tree

4 files changed

+60
-91
lines changed

4 files changed

+60
-91
lines changed

Runtime/Accumulators/ValueAccumulator.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
21
using System.Collections.Generic;
32

43
namespace Zigurous.DataStructures
54
{
65
/// <summary>
76
/// Accumulates a set of stored values into a single total value.
87
/// </summary>
9-
public abstract class ValueAccumulator<T> : IDisposable
8+
public abstract class ValueAccumulator<T>
109
{
1110
/// <summary>
1211
/// Keeps track of all accumulated values. Values are stored by unique
@@ -24,15 +23,6 @@ public abstract class ValueAccumulator<T> : IDisposable
2423
/// </summary>
2524
public int Count => this.values.Count;
2625

27-
/// <summary>
28-
/// Disposes of all class resources.
29-
/// </summary>
30-
public virtual void Dispose()
31-
{
32-
this.values?.Clear();
33-
this.values = null;
34-
}
35-
3626
/// <summary>
3727
/// Returns the stored value with the given hash code identifier.
3828
/// </summary>
@@ -87,6 +77,15 @@ public void RemoveValue(int id)
8777
}
8878
}
8979

80+
/// <summary>
81+
/// Removes all stored values and resets the total accumulated value.
82+
/// </summary>
83+
public void Clear()
84+
{
85+
this.values.Clear();
86+
this.total = default(T);
87+
}
88+
9089
/// <summary>
9190
/// Increases the total accumulated value by a given amount.
9291
/// </summary>

Runtime/Modularity/Modules.cs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Zigurous.DataStructures
66
/// <summary>
77
/// Manages a list of registered entity modules.
88
/// </summary>
9-
public sealed class Modules<T> : IModular<T>, IDisposable where T: class
9+
public sealed class Modules<T> : IModular<T> where T: class
1010
{
1111
/// <summary>
1212
/// The modules registered to the entity.
@@ -23,6 +23,16 @@ public sealed class Modules<T> : IModular<T>, IDisposable where T: class
2323
/// </summary>
2424
public Action<T> unregistered;
2525

26+
/// <summary>
27+
/// The amount of modules registered to the entity.
28+
/// </summary>
29+
public int Count => this.items.Count;
30+
31+
/// <summary>
32+
/// Returns the module at the given index.
33+
/// </summary>
34+
public T this[int index] => this.items.ElementAt(index);
35+
2636
private Modules() {}
2737

2838
/// <summary>
@@ -53,38 +63,21 @@ public Modules(T[] items, Action<T> registered = null, Action<T> unregistered =
5363
}
5464
}
5565

56-
/// <summary>
57-
/// Disposes of all class resources.
58-
/// </summary>
59-
public void Dispose()
60-
{
61-
this.items = null;
62-
this.registered = null;
63-
this.unregistered = null;
64-
}
65-
66-
/// <summary>
67-
/// Returns the module at index.
68-
/// </summary>
69-
public T this[int index] => this.items.ElementAt(index);
70-
71-
/// <summary>
72-
/// The amount of modules registered to the entity.
73-
/// </summary>
74-
public int Length => this.items?.Count ?? 0;
75-
7666
/// <summary>
7767
/// Registers an item to the entity. Returns false if the item cannot be
7868
/// registered.
7969
/// </summary>
8070
public bool Register(T item)
8171
{
82-
if (item == null || (this.items?.Contains(item) ?? true)) {
72+
if (item == null || this.items.Contains(item)) {
8373
return false;
8474
}
8575

8676
this.items.Add(item);
87-
this.registered?.Invoke(item);
77+
78+
if (this.registered != null) {
79+
this.registered.Invoke(item);
80+
}
8881

8982
return true;
9083
}
@@ -97,7 +90,10 @@ public bool Unregister(T item)
9790
{
9891
if (item != null && this.items.Remove(item))
9992
{
100-
this.unregistered?.Invoke(item);
93+
if (this.unregistered != null) {
94+
this.unregistered.Invoke(item);
95+
}
96+
10197
return true;
10298
}
10399

@@ -109,7 +105,7 @@ public bool Unregister(T item)
109105
/// </summary>
110106
public bool IsRegistered(T item)
111107
{
112-
return this.items?.Contains(item) ?? false;
108+
return this.items.Contains(item);
113109
}
114110

115111
}

Runtime/ObjectPool/ObjectPool.cs

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ namespace Zigurous.DataStructures
1212
public sealed class ObjectPool<T> : IObjectPool<T> where T: class
1313
{
1414
/// <summary>
15-
/// Creates a new instance of an object.
15+
/// A function type that creates a new instance of an object.
1616
/// </summary>
1717
public delegate T Generator();
1818

19+
/// <summary>
20+
/// The function delegate that generates a new object.
21+
/// </summary>
22+
private Generator _generator;
23+
1924
/// <summary>
2025
/// The list of all objects waiting to be reused.
2126
/// </summary>
@@ -26,11 +31,6 @@ public sealed class ObjectPool<T> : IObjectPool<T> where T: class
2631
/// </summary>
2732
private List<T> _activeItems;
2833

29-
/// <summary>
30-
/// The function that generates a new object.
31-
/// </summary>
32-
private Generator _generator;
33-
3434
/// <summary>
3535
/// The maximum number of objects that can be generated.
3636
/// </summary>
@@ -119,18 +119,8 @@ public ObjectPool(Generator generator, int initialCapacity, int maxCapacity, boo
119119
/// </summary>
120120
public void Dispose()
121121
{
122-
if (_pool != null)
123-
{
124-
_pool.Clear();
125-
_pool = null;
126-
}
127-
128-
if (_activeItems != null)
129-
{
130-
_activeItems.Clear();
131-
_activeItems = null;
132-
}
133-
122+
_pool.Clear();
123+
_activeItems.Clear();
134124
_generator = null;
135125
}
136126

@@ -141,33 +131,19 @@ public void Dispose()
141131
/// </summary>
142132
public void Dispose(Action<T> cleanup)
143133
{
144-
if (_pool != null)
134+
if (cleanup != null)
145135
{
146-
if (cleanup != null)
147-
{
148-
while (_pool.Count > 0) {
149-
cleanup(_pool.Dequeue());
150-
}
136+
while (_pool.Count > 0) {
137+
cleanup(_pool.Dequeue());
151138
}
152139

153-
_pool.Clear();
154-
_pool = null;
155-
}
156-
157-
if (_activeItems != null)
158-
{
159-
if (cleanup != null)
160-
{
161-
foreach (T item in _activeItems) {
162-
cleanup(item);
163-
}
140+
foreach (T item in _activeItems) {
141+
cleanup(item);
164142
}
165-
166-
_activeItems.Clear();
167-
_activeItems = null;
168143
}
169144

170-
_generator = null;
145+
_pool.Clear();
146+
_activeItems.Clear();
171147
}
172148

173149
/// <summary>
@@ -178,7 +154,7 @@ public void Dispose(Action<T> cleanup)
178154
/// </summary>
179155
public T Retrieve()
180156
{
181-
T item;
157+
T item = null;
182158

183159
if (_activeItems.Count >= this.maxCapacity)
184160
{
@@ -187,17 +163,15 @@ public T Retrieve()
187163
item = _activeItems[0];
188164
_activeItems.RemoveAt(0);
189165
}
190-
else
191-
{
192-
item = null;
193-
}
194166
}
195167
else
196168
{
197169
if (_pool.Count > 0) {
198-
return _pool.Dequeue() ?? _generator.Invoke();
199-
} else {
200-
return _generator.Invoke();
170+
item = _pool.Dequeue();
171+
}
172+
173+
if (item == null && _generator != null) {
174+
item = _generator.Invoke();
201175
}
202176
}
203177

Runtime/SingletonBehavior.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ namespace Zigurous.DataStructures
44
{
55
public abstract class SingletonBehavior<T> : MonoBehaviour where T : Component
66
{
7+
private static volatile T _instance;
78
private static object _lock = new object();
89
private static bool _isUnloading = false;
9-
private static volatile T _instance;
10+
11+
/// <summary>
12+
/// Returns true if the Singleton has been initialized and an instance
13+
/// is available to use.
14+
/// </summary>
15+
public static bool HasInstance => _instance != null;
1016

1117
/// <summary>
1218
/// The current instance of the class. The instance will be created if
@@ -42,13 +48,7 @@ public static T Instance
4248
}
4349
}
4450

45-
/// <summary>
46-
/// Returns true if the Singleton has been initialized and an instance
47-
/// is available to use.
48-
/// </summary>
49-
public static bool HasInstance => _instance != null;
50-
51-
protected SingletonBehavior() {} // Prevent outside instantiation
51+
protected SingletonBehavior() {}
5252

5353
protected virtual void Awake()
5454
{

0 commit comments

Comments
 (0)