/// <summary> /// Sets the minimum number of hard references to be contained in the specified object pool. /// </summary> /// <param name="minSize">The minimum number of hard references to be contained in the specified object pool.</param> public static void SetMinimumSize <T>(int minSize) where T : class { long typePointer = ObjectPoolMgr.GetTypePointer <T>(); if (!ObjectPoolMgr.Pools.ContainsKey(typePointer)) { return; } ((ObjectPool <T>)ObjectPoolMgr.Pools[typePointer]).MinimumSize = minSize; }
/// <summary>Gets information about the specified object pool.</summary> /// <returns>An object of type <see cref="T:WCell.Util.ObjectPools.ObjectPoolInfo" /> if the function succeeded, otherwise an object with all values equal to 0 is returned.</returns> public static ObjectPoolInfo GetPoolInfo <T>() where T : class { long typePointer = ObjectPoolMgr.GetTypePointer <T>(); IObjectPool objectPool; if (ObjectPoolMgr.Pools.TryGetValue(typePointer, out objectPool)) { return(((ObjectPool <T>)objectPool).Info); } return(new ObjectPoolInfo(0, 0)); }
/// <summary>Obtains an object from the specified object pool.</summary> /// <returns>If a lock could not be aquired on the object pool null is returned. Otherwise a hard reference to the object requested is returned.</returns> public static T ObtainObject <T>() where T : class { long typePointer = ObjectPoolMgr.GetTypePointer <T>(); IObjectPool objectPool; if (ObjectPoolMgr.Pools.TryGetValue(typePointer, out objectPool)) { return(((ObjectPool <T>)objectPool).Obtain()); } return(default(T)); }
/// <summary>Releases an object back into the object pool.</summary> /// <param name="obj">The object to be released.</param> public static void ReleaseObject <T>(T obj) where T : class { long typePointer = ObjectPoolMgr.GetTypePointer <T>(); IObjectPool objectPool; if (!ObjectPoolMgr.Pools.TryGetValue(typePointer, out objectPool)) { return; } objectPool.Recycle((object)obj); }
/// <summary>Registers an object pool with the specified type.</summary> /// <param name="func">A pointer to a function that creates new objects.</param> /// <returns>True if the type already exists or was registered successfully. False if locking the internal pool list timed out.</returns> /// <remarks>The function waits 3000 milliseconds to aquire the lock of the internal pool list.</remarks> public static bool RegisterType <T>(Func <T> func) where T : class { long typePointer = ObjectPoolMgr.GetTypePointer <T>(); lock (typeof(ObjectPoolMgr)) { if (!ObjectPoolMgr.Pools.ContainsKey(typePointer)) { ObjectPoolMgr.Pools.Add(typePointer, (IObjectPool) new ObjectPool <T>(func)); return(true); } } return(false); }
/// <summary>Returns true if the specified type is registered.</summary> /// <typeparam name="T">The type to check registration with.</typeparam> /// <returns>True if the specified type is registered.</returns> public static bool ContainsType <T>() { return(ObjectPoolMgr.Pools.ContainsKey(ObjectPoolMgr.GetTypePointer <T>())); }