A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3
4namespace Unity.VisualScripting
5{
6 public static class ManualPool<T> where T : class
7 {
8 private static readonly object @lock = new object();
9 private static readonly Stack<T> free = new Stack<T>();
10 private static readonly HashSet<T> busy = new HashSet<T>();
11
12 public static T New(Func<T> constructor)
13 {
14 lock (@lock)
15 {
16 if (free.Count == 0)
17 {
18 free.Push(constructor());
19 }
20
21 var item = free.Pop();
22
23 busy.Add(item);
24
25 return item;
26 }
27 }
28
29 public static void Free(T item)
30 {
31 lock (@lock)
32 {
33 if (!busy.Contains(item))
34 {
35 throw new ArgumentException("The item to free is not in use by the pool.", nameof(item));
36 }
37
38 busy.Remove(item);
39
40 free.Push(item);
41 }
42 }
43 }
44}