public class LinkedPoolean<T> where T : PooleanNode<T>, new() { public T Head; public LinkedPoolean() { Head = Activator.CreateInstance<T>(); } public void Get(out T Vessel) { if (Head != null) { Vessel = Head; Head = Head.Next; } else { Vessel = Activator.CreateInstance<T>(); } } public void ReturnPoolable(T Return) { Return.Next = Head; Head = Return; } }
The Key change here is the way we access the "Activator", you see, there's a function to create an instance that takes a Generic parameter T, and there's another version of the function that takes an argument "Type" and returns an "Object", the object returned by the second function is sadly not the right type, so in order to use it I used to cast it, this would create a hit on the performance. The function that takes a parameter of type T returns an object of type T, making casting unnecessary.
This improvement was only possible though if the PooleanNode knew which "kind of poolean node" it was, new Poolean node class:
public class PooleanNode<T> where T: PooleanNode<T> { public T Next; }
This in turn makes the poolean node type safe, before hand it was potentially possible to subscribe a poolean node into a pool of another type, it was kind of weird at the beginning to see a class expecting itself to be the template, but it is kind of cool the way it works.
Thanks for reading~
No comments:
Post a Comment