Opentk/Source/Compatibility/ObjectPool.cs
the_fiddler 244084c99e Moved GdiPlus wrappers to OpenTK.Compatibility.
Moved TextPrinter to OpenTK.Compatibility.
2009-08-17 10:29:51 +00:00

42 lines
874 B
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK
{
class ObjectPool<T> where T : IPoolable<T>, new()
{
Queue<T> pool = new Queue<T>();
public ObjectPool()
{ }
public T Acquire()
{
T item;
if (pool.Count > 0)
{
item = pool.Dequeue();
item.OnAcquire();
}
else
{
item = new T();
item.Owner = this;
item.OnAcquire();
}
return item;
}
public void Release(T item)
{
if (item == null)
throw new ArgumentNullException("item");
item.OnRelease();
pool.Enqueue(item);
}
}
}