Пример #1
0
        /// <summary>
        /// Gets an object from the pool, or creates a new one if no pool items are available.
        /// </summary>
        /// <returns>An available object from the pool, or a new one if no pool items are available.</returns>
        /// <remarks>
        /// If type of <typeparamref name="T"/> implements <see cref="ISupportLifecycle"/>, the pool will attach
        /// to the item's <see cref="ISupportLifecycle.Disposed"/> event such that the object can be automatically
        /// restored to the pool upon <see cref="IDisposable.Dispose"/>. It will be up to class implementors to
        /// make sure <see cref="ISupportLifecycle.Initialize"/> makes the class ready for use as this method will
        /// always be called for an object being taken from the pool.
        /// </remarks>
        public T TakeObject()
        {
            T    item;
            bool newItem = false;

            // Attempt to provide user with a queued item
            if (!m_objectPool.TryDequeue(out item))
            {
                // No items are available, create a new item for the object pool
                item = FastObjectFactory <T> .CreateObjectFunction();

                newItem = true;
            }

            // Automatically handle class life cycle if item implements support for this
            ISupportLifecycle lifecycleItem = item as ISupportLifecycle;

            if (lifecycleItem != null)
            {
                // Attach to dispose event so item can be automatically returned to the pool
                if (newItem)
                {
                    lifecycleItem.Disposed += LifecycleItem_Disposed;
                }

                // Initialize or reinitialize (i.e., un-dispose) the item
                lifecycleItem.Initialize();
            }

            return(item);
        }
Пример #2
0
        /// <summary>
        /// Allocates the pool to the desired <paramref name="size"/>.
        /// </summary>
        /// <param name="size">Desired pool size.</param>
        /// <exception cref="ArgumentOutOfRangeException">Pool <paramref name="size"/> must at least be one.</exception>
        public void SetPoolSize(int size)
        {
            if (size < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(size), "pool size must at least be one");
            }

            for (int i = 0; i < size - m_objectPool.Count; i++)
            {
                T item = FastObjectFactory <T> .CreateObjectFunction();

                // Automatically handle life cycle if item implements support for this
                ISupportLifecycle lifecycleItem = item as ISupportLifecycle;

                // Attach to dispose event so item can be automatically returned to the pool
                if (lifecycleItem != null)
                {
                    lifecycleItem.Disposed += LifecycleItem_Disposed;
                }

                m_objectPool.Enqueue(item);
            }
        }