-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGenericPooler.cs
More file actions
123 lines (91 loc) · 3.33 KB
/
Copy pathGenericPooler.cs
File metadata and controls
123 lines (91 loc) · 3.33 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
namespace Patterns
{
/// <summary>
/// Class able to manage any pool able object.
/// </summary>
/// <typeparam name="T"></typeparam>
public class GenericPooler<T> where T : class, IPoolableObject, new()
{
//--------------------------------------------------------------------------------------------------------------
#region Constructor
/// <summary>
/// Constructor, you must have to specify the starting size of the pool
/// </summary>
/// <param name="startingSize"></param>
public GenericPooler(int startingSize)
{
StartSize = startingSize;
//pool start size
for (var i = 0; i < StartSize; ++i)
{
var obj = new T();
freeObjects.Add(obj);
}
}
#endregion
//--------------------------------------------------------------------------------------------------------------
#region Exceptions
public class GenericPoolerArgumentException : ArgumentException
{
public GenericPoolerArgumentException(string message) : base(message)
{
}
}
#endregion
//--------------------------------------------------------------------------------------------------------------
#region Fields
readonly List<T> busyObjects = new List<T>();
readonly List<T> freeObjects = new List<T>();
#endregion
//--------------------------------------------------------------------------------------------------------------
#region Utility
public int StartSize { get; }
public int SizeFreeObjects => freeObjects.Count;
public int SizeBusyObjects => busyObjects.Count;
public Type PoolType => typeof(T);
#endregion
//--------------------------------------------------------------------------------------------------------------
#region Operations
/// <summary>
/// Get an object of the type T
/// </summary>
/// <returns></returns>
public T Get()
{
T pooled = null;
if (SizeFreeObjects > 0)
{
//pool the last object
pooled = freeObjects[SizeFreeObjects - 1];
freeObjects.Remove(pooled);
}
else
{
//if can't pool create a new object
pooled = new T();
}
//add to the busy list
busyObjects.Add(pooled);
return pooled;
}
/// <summary>
/// Release an object of the type T
/// </summary>
/// <param name="released"></param>
public void Release(T released)
{
if (released == null)
throw new GenericPoolerArgumentException("Can't Release a null object");
//reset object
released.Restart();
//add back to the freelist
freeObjects.Add(released);
//remove from busy list
busyObjects.Remove(released);
}
#endregion
//--------------------------------------------------------------------------------------------------------------
}
}