Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Singleton.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21#ifndef GF_SINGLETON_H
22#define GF_SINGLETON_H
23
24#include <cassert>
25#include <utility>
26
27namespace gf {
28#ifndef DOXYGEN_SHOULD_SKIP_THIS
29inline namespace v1 {
30#endif
31
32 template<typename T>
33 class SingletonStorage;
34
54 template<typename T>
55 class Singleton {
56 public:
64 : m_single(nullptr)
65 {
66 }
67
71 Singleton(const Singleton&) = delete;
72
76 Singleton(Singleton&&) = delete;
80 Singleton& operator=(const Singleton&) = delete;
81
86
97 assert(m_single);
98 return *m_single;
99 }
100
106 void reset() noexcept {
107 m_single = nullptr;
108 }
109
115 bool isValid() const noexcept {
116 return m_single != nullptr;
117 }
118
119 private:
120 friend class SingletonStorage<T>;
121
122 T *m_single;
123 };
124
147 template<typename T>
149 public:
157 template<typename ... Args>
158 SingletonStorage(Singleton<T>& ref, Args&&... args)
159 : m_storage(std::forward<Args>(args)...) {
160 assert(ref.m_single == nullptr);
161 ref.m_single = &m_storage;
162 }
163
164 private:
165 T m_storage;
166 };
167
168#ifndef DOXYGEN_SHOULD_SKIP_THIS
169}
170#endif
171}
172
173#endif // GF_SINGLETON_H
A singleton that wraps a pointer provided by a storage.
Definition: Singleton.h:55
Singleton & operator=(const Singleton &)=delete
Deleted copy assignment.
Singleton & operator=(Singleton &&)=delete
Deleted move assignment.
Singleton(const Singleton &)=delete
Deleted copy constructor.
T & operator()()
Access the singleton.
Definition: Singleton.h:96
Singleton()
Default constructor.
Definition: Singleton.h:63
void reset() noexcept
Reset the singleton.
Definition: Singleton.h:106
bool isValid() const noexcept
Check if the singleton has been initialized.
Definition: Singleton.h:115
Singleton(Singleton &&)=delete
Deleted move constructor.
A storage for a singleton.
Definition: Singleton.h:148
SingletonStorage(Singleton< T > &ref, Args &&... args)
Construct a storage for a singleton.
Definition: Singleton.h:158
The namespace for gf classes.