Skip to content

Commit 7b22228

Browse files
committed
Feat: Add custom object pool policies for Stack and Queue types
1 parent ca729e4 commit 7b22228

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.ObjectPool;
5+
6+
namespace CodeOfChaos.Extensions.ObjectPool;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class QueuePoolPolicy<T, TItem> : IPooledObjectPolicy<T>
11+
where T : Queue<TItem>, new()
12+
{
13+
public T Create() => new();
14+
public bool Return(T obj) {
15+
obj.Clear();
16+
return obj.Count == 0;
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.ObjectPool;
5+
6+
namespace CodeOfChaos.Extensions.ObjectPool;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class ResettableQueuePoolPolicy<T, TItem> : IPooledObjectPolicy<T>
11+
where T : Queue<TItem>, new()
12+
where TItem : IResettable
13+
{
14+
public T Create() => new();
15+
public bool Return(T obj) {
16+
while (obj.TryDequeue(out TItem? item)) {
17+
item.TryReset();
18+
}
19+
obj.Clear();
20+
return obj.Count == 0;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.ObjectPool;
5+
6+
namespace CodeOfChaos.Extensions.ObjectPool;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class ResettableStackPoolPolicy<T, TItem> : IPooledObjectPolicy<T>
11+
where T : Stack<TItem>, new()
12+
where TItem : IResettable
13+
{
14+
public T Create() => new();
15+
public bool Return(T obj) {
16+
while (obj.TryPop(out TItem? item)) {
17+
item.TryReset();
18+
}
19+
obj.Clear();
20+
return obj.Count == 0;
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
using Microsoft.Extensions.ObjectPool;
5+
6+
namespace CodeOfChaos.Extensions.ObjectPool;
7+
// ---------------------------------------------------------------------------------------------------------------------
8+
// Code
9+
// ---------------------------------------------------------------------------------------------------------------------
10+
public class StackPoolPolicy<T, TItem> : IPooledObjectPolicy<T>
11+
where T : Stack<TItem>, new()
12+
{
13+
public T Create() => new();
14+
public bool Return(T obj) {
15+
obj.Clear();
16+
return obj.Count == 0;
17+
}
18+
}

0 commit comments

Comments
 (0)