-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathObjectPoolcs.cs
More file actions
94 lines (74 loc) · 2.17 KB
/
ObjectPoolcs.cs
File metadata and controls
94 lines (74 loc) · 2.17 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ObjectPool_Queue<T> where T : class, new()
{
private Queue<T> m_objectQueue;
private Action<T> m_ResetAction;
private Action<T> m_InitAction;
public ObjectPool_Queue(Action<T>
ResetAction = null, Action<T> OnetimeInitAction = null)
{
m_objectQueue = new Queue<T>();
m_ResetAction = ResetAction;
m_InitAction = OnetimeInitAction;
}
public T New()
{
if (m_objectQueue.Count > 0)
{
T t = m_objectQueue.Dequeue();
if (null != m_ResetAction )
m_ResetAction(t);
if (null != m_InitAction)
m_InitAction(t);
return t;
}
else
{
return null;
}
}
public void Store(T obj)
{
m_objectQueue.Enqueue(obj);
}
}
public class ObjectPool_Dict<Key, Value>
where Key : struct
where Value : class, new()
{
private Dictionary<Key, ObjectPool_Queue<Value>> m_objectDic;
private readonly Action<Value> m_ResetAction;
private readonly Action<Value> m_InitAction;
public ObjectPool_Dict(Action<Value>
ResetAction = null, Action<Value> OnetimeInitAction = null)
{
//m_MaxTIme = initialBufferSize;
m_objectDic = new Dictionary<Key, ObjectPool_Queue<Value>>();
m_ResetAction = ResetAction;
m_InitAction = OnetimeInitAction;
}
public Value this[Key key]
{
get
{
if (!m_objectDic.ContainsKey(key)) return null;
var objectPool_Queue = m_objectDic[key];
if (null == objectPool_Queue)
objectPool_Queue = new ObjectPool_Queue<Value>(m_ResetAction, m_InitAction);
var t = objectPool_Queue.New();
if (null == t) return t;
return t;
}
set
{
if (!m_objectDic.ContainsKey(key)|| null == m_objectDic[key])
{
m_objectDic[key] = new ObjectPool_Queue<Value>(m_ResetAction, m_InitAction);
}
m_objectDic[key].Store(value);
}
}
}