/**************************************************************************** * Copyright (c) 2017 ~ 2021.4 liangxie * * http://qframework.io * https://github.com/liangxiegame/QFramework * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ****************************************************************************/ using System.Collections.Generic; using UnityEngine; namespace QFramework { public interface IRefCounter { int RefCount { get; } void Retain(object refOwner = null); void Release(object refOwner = null); } public class SimpleRC : IRefCounter { public SimpleRC() { RefCount = 0; } public int RefCount { get; private set; } public void Retain(object refOwner = null) { ++RefCount; } public void Release(object refOwner = null) { --RefCount; if (RefCount == 0) { OnZeroRef(); } } protected virtual void OnZeroRef() { } } public sealed class SafeARC : IRefCounter { public int RefCount { get { return mOwners.Count; } } public HashSet Owners { get { return mOwners; } } readonly HashSet mOwners = new HashSet(); public void Retain(object refOwner) { if (!Owners.Add(refOwner)) { Debug.LogError("ObjectIsAlreadyRetainedByOwnerException"); } } public void Release(object refOwner) { if (!Owners.Remove(refOwner)) { Debug.LogError("ObjectIsNotRetainedByOwnerExceptionWithHint"); } } } public sealed class UnsafeARC : SimpleRC { } }