From f765becf067dc1017634c04621af20da41181186 Mon Sep 17 00:00:00 2001 From: "Nuno \"Omega Leo\" Diogo" Date: Fri, 17 Jan 2025 15:05:52 +0000 Subject: [PATCH] added an instacedobject class --- OmegaLeo.HelperLib/Models/InstancedObject.cs | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 OmegaLeo.HelperLib/Models/InstancedObject.cs diff --git a/OmegaLeo.HelperLib/Models/InstancedObject.cs b/OmegaLeo.HelperLib/Models/InstancedObject.cs new file mode 100644 index 0000000..1c156e2 --- /dev/null +++ b/OmegaLeo.HelperLib/Models/InstancedObject.cs @@ -0,0 +1,31 @@ +using System; + +namespace GameDevLibrary.Models +{ + public abstract class InstancedObject where T : class + { + public static T Instance { get; private set; } + + protected InstancedObject() + { + if (Instance != null) + { + throw new InvalidOperationException($"An instance of {typeof(T).Name} already exists."); + } + + Instance = this as T; + if (Instance == null) + { + throw new InvalidOperationException($"Failed to cast {GetType().Name} to {typeof(T).Name}."); + } + } + + ~InstancedObject() + { + if (Instance == this) + { + Instance = null; + } + } + } +} \ No newline at end of file