Merge pull request #1 from CYBUTEK/master
[VesselSimulator.git] / KerbalEngineer / Helpers / Pool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections.Generic;
 
namespace KerbalEngineer
{
    /// <summary>
    ///     Pool of object
    /// </summary>
    public class Pool<T> {
        
        private readonly Stack<T> values = new Stack<T>();
 
        private readonly CreateDelegate<T> create;
        private readonly ResetDelegate<T> reset;
 
        public delegate R CreateDelegate<out R>();
        public delegate void ResetDelegate<in T1>(T1 a);
        
        /// <summary>
        ///     Creates an empty pool with the specified object creation and reset delegates.
        /// </summary>
        public Pool(CreateDelegate<T> create, ResetDelegate<T> reset) {
            this.create = create;
            this.reset = reset;
        }
 
        /// <summary>
        ///     Borrows an object from the pool.
        /// </summary>
        public T Borrow() {
            lock (values) {
                return values.Count > 0 ? values.Pop() : create();
            }
        }
        
        /// <summary>
        ///     Release an object, reset it and returns it to the pool.
        /// </summary>
        public void Release(T value) {
            reset(value);
            lock (values) {
                values.Push(value);
            }
        }
        
        /// <summary>
        ///     Current size of the pool.
        /// </summary>
        public int Count()
        {
            return values.Count;
        }
    }
}