diff options
author | vnugent <public@vaughnnugent.com> | 2023-01-08 16:01:54 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-01-08 16:01:54 -0500 |
commit | de94d788e9a47432a7630a8215896b8dd3628599 (patch) | |
tree | 666dec06eef861d101cb6948aff52a3d354c8d73 /lib/Utils/src/VnDisposeable.cs | |
parent | be6dc557a3b819248b014992eb96c1cb21f8112b (diff) |
Reorder + analyzer cleanup
Diffstat (limited to 'lib/Utils/src/VnDisposeable.cs')
-rw-r--r-- | lib/Utils/src/VnDisposeable.cs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/lib/Utils/src/VnDisposeable.cs b/lib/Utils/src/VnDisposeable.cs new file mode 100644 index 0000000..4230a13 --- /dev/null +++ b/lib/Utils/src/VnDisposeable.cs @@ -0,0 +1,85 @@ +/* +* Copyright (c) 2022 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Utils +* File: VnDisposeable.cs +* +* VnDisposeable.cs is part of VNLib.Utils which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Utils 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 2 of the License, +* or (at your option) any later version. +* +* VNLib.Utils is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.Utils. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.Runtime.CompilerServices; + +namespace VNLib.Utils +{ + /// <summary> + /// Provides a base class with abstract methods for for disposable objects, with disposed check method + /// </summary> + public abstract class VnDisposeable : IDisposable + { + ///<inheritdoc/> + protected bool Disposed { get; private set; } + + /// <summary> + /// When overriden in a child class, is responsible for freeing resources + /// </summary> + protected abstract void Free(); + + /// <summary> + /// Checks if the current object has been disposed. Method will be inlined where possible + /// </summary> + /// <exception cref="ObjectDisposedException"></exception> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected virtual void Check() + { + if (Disposed) + { + throw new ObjectDisposedException("Object has been disposed"); + } + } + + /// <summary> + /// Sets the internal state to diposed without calling <see cref="Free"/> operation. + /// Usefull if another code-path performs the free operation independant of a dispose opreation. + /// </summary> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + protected void SetDisposedState() => Disposed = true; + ///<inheritdoc/> + protected virtual void Dispose(bool disposing) + { + if (!Disposed) + { + if (disposing) + { + //Call free method + Free(); + } + Disposed = true; + } + } + //Finalizer is not needed here + + ///<inheritdoc/> + public void Dispose() + { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } +}
\ No newline at end of file |