/************************************************************************* * ModernUO * * Copyright 2019-2020 - ModernUO Development Team * * Email: hi@modernuo.com * * File: RefPool.cs * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * *************************************************************************/ using System; using System.Collections.Generic; namespace Server.Utilities { /// A resource reference object that can be disposed. /// /// Disposing the reference is expected to return itself back into the /// original pool that created it. /// public interface IRef : IDisposable { } /// /// Base implementation of the interface. /// /// /// New implementations of should either derive from, or mirror /// the functionality of this base implementation. /// /// public abstract class BaseRef : IRef where TDerived : IRef { private readonly RefPool m_Pool; public BaseRef(RefPool pool) => m_Pool = pool; public void Dispose() { OnDispose(); m_Pool.Return((TDerived)(object)this); } protected abstract void OnDispose(); } /// /// A resource reference pool that manages a collection of reusable resources. /// /// The resource type the pool will contain. public class RefPool where TRef : IRef { public delegate TRef Generator(RefPool targetPool); public const int DEFAULT_RESOURCE_RETENTION = 10; private readonly Generator m_Generator; private readonly Stack m_Resources = new(); private int m_MaxRefrenceRetention; /// The generator function for creating new resources. /// /// An amount of resources that should be pre-generated during initialization of the resource /// pool. /// public RefPool(Generator generator, int preGenerateCount = 0, int maxRefrenceRetention = DEFAULT_RESOURCE_RETENTION) { if (generator == null) { throw new ArgumentNullException(nameof(generator)); } if (preGenerateCount > maxRefrenceRetention) { throw new IndexOutOfRangeException( $"{nameof(preGenerateCount)} greater than {nameof(maxRefrenceRetention)}" ); } m_Generator = generator; m_MaxRefrenceRetention = maxRefrenceRetention; while (--preGenerateCount >= 0) { m_Resources.Push(generator(this)); } } /// /// The maximum number of unused resources to hold in the pool. /// public int MaxRefrenceRetention { get => m_MaxRefrenceRetention; set { m_MaxRefrenceRetention = value; while (m_Resources.Count > value) { m_Resources.Pop(); } } } /// /// Retrieves a resource reference that is managed by this . If the pool is has unused /// resources, /// it will remove one from the pool and return it; otherwise, a new resource will be generated. /// /// Unused resource, or a new resource if no unused resources available. public TRef Get() => m_Resources.TryPop(out var item) ? item : m_Generator(this); /// /// Returns a resource reference to the pool of unused resources. /// /// Resource to be returned. public void Return(TRef queueRef) { if (m_Resources.Count < MaxRefrenceRetention) { m_Resources.Push(queueRef); } } } public class QueueRef : Queue, IRef { /// /// Generator function for creating instances of the resource. /// public static RefPool>.Generator Generate = targetPool => new QueueRef(targetPool); private readonly RefPool> m_Pool; private QueueRef(RefPool> pool) => m_Pool = pool; /// Clears the queue and returns this resource to its parent resource pool. public void Dispose() { Clear(); m_Pool.Return(this); } } public class StackRef : Stack, IRef { /// /// Generator function for creating instances of the resource. /// public static RefPool>.Generator Generate = targetPool => new StackRef(targetPool); private readonly RefPool> m_Pool; private StackRef(RefPool> pool) => m_Pool = pool; /// Clears the stack and returns this resource to its parent resource pool. public void Dispose() { Clear(); m_Pool.Return(this); } } }