Nui Engine
A game engine framework
Loading...
Searching...
No Matches
Singleton.h
1#pragma once
2
3namespace Nui
4{
9 template <typename T>
10 class Singleton final
11 {
12 public:
17 static T& Get() noexcept
18 {
19 return *GetPtr();
20 }
21
25 static void Destroy() noexcept
26 {
27 T* ptr = GetPtr();
28 if (ptr)
29 {
30 delete ptr;
31 ptr = nullptr;
32 }
33 }
34
35 Singleton(Singleton&&) = delete;
36 Singleton(const Singleton&) = delete;
37 Singleton& operator=(Singleton&&) = delete;
38 Singleton& operator=(Singleton&) = delete;
39
40 private:
41
46 static T* GetPtr() noexcept
47 {
48 static Singleton<T> s_instance;
49 return s_instance.m_obj;
50 }
51
52 private:
53 Singleton() = default;
54
58 T* m_obj = new T();
59 };
60}
Template class for creating a singleton instance of a class.
Definition Singleton.h:11
static void Destroy() noexcept
Destroy the singleton instance.
Definition Singleton.h:25
static T & Get() noexcept
Get the singleton instance.
Definition Singleton.h:17