< Summary

Class:Imagini.Resource
Assembly:Imagini.Core
File(s):/home/razer/vscode-projects/project-grove/imagini/Imagini.Core/Internal/Resource.cs
Covered lines:27
Uncovered lines:2
Coverable lines:29
Total lines:70
Line coverage:93.1% (27 of 29)
Covered branches:4
Total branches:6
Branch coverage:66.6% (4 of 6)

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor()20100%100%
.cctor()10100%100%
Destroy()20100%50%
CheckIfNotDisposed()2066.66%50%
NotDisposed(...)10100%100%
NotDisposed(...)10100%100%
Register(...)10100%100%
Unregister(...)100%100%

File(s)

/home/razer/vscode-projects/project-grove/imagini/Imagini.Core/Internal/Resource.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Runtime.CompilerServices;
 4using static Imagini.Logger;
 5
 6namespace Imagini
 7{
 8    /// <summary>
 9    /// Represents a resource which can be disposed.
 10    /// </summary>
 11    public abstract class Resource
 12    {
 30613        private List<Resource> _children = new List<Resource>();
 14        private readonly string _resourceName;
 15
 116        private static Dictionary<Type, int> s_count = new Dictionary<Type, int>();
 83217        internal int ResourceID { get; private set; }
 18
 30619        internal Resource()
 20        {
 30621            var type = this.GetType();
 30622            _resourceName = type.Name;
 30623            if (!s_count.ContainsKey(type))
 24            {
 725                s_count.Add(type, 0);
 26            }
 30627            ResourceID = s_count[type];
 30628            s_count[type]++;
 30629            Log.Debug("Created {name} ID {id}", _resourceName, ResourceID);
 30630        }
 31
 32
 33        /// <summary>
 34        /// Indicates if this resource is disposed or not.
 35        /// </summary>
 106436        public bool IsDisposed { get; private set; }
 37        internal virtual void Destroy()
 38        {
 22039            if (IsDisposed) return;
 25940            _children.ForEach(c => c.Destroy());
 22041            _children = null;
 22042            IsDisposed = true;
 22043            Log.Debug("Destroyed {name} ID {guid}", _resourceName, ResourceID);
 22044        }
 45
 46        protected void CheckIfNotDisposed()
 47        {
 34148            if (IsDisposed)
 049                throw new ObjectDisposedException(_resourceName);
 34150        }
 51
 52        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 53        protected T NotDisposed<T>(Func<T> val)
 54        {
 1555            CheckIfNotDisposed();
 1556            return val();
 57        }
 58
 59        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 60        protected void NotDisposed(Action action)
 61        {
 23762            CheckIfNotDisposed();
 23763            action();
 23764        }
 65
 3966        internal void Register(Resource child) => _children.Add(child);
 67
 068        internal void Unregister(Resource child) => _children.Remove(child);
 69    }
 70}