Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Singleton.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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 
27 #include "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
34  template<typename T>
35  class SingletonStorage;
36 
37  /**
38  * @ingroup core
39  * @brief A singleton that wraps a pointer provided by a storage.
40  *
41  * This class should be used with a global variable. It acts as an access to
42  * a singleton which storage is provided by a gf::SingletonStorage<T>.
43  *
44  * ~~~{.cc}
45  * gf::Singleton<Foo> gFoo;
46  *
47  * int main() {
48  * gf::SingletonStorage<Foo> fooStorage(gFoo);
49  *
50  * gFoo().bar();
51  * }
52  * ~~~
53  *
54  * @sa gf::SingletonStorage
55  */
56  template<typename T>
57  class Singleton {
58  public:
59  /**
60  * @brief Default constructor.
61  *
62  * It initialize the underlying pointer to `nullptr`. The actual
63  * initialization is done when the storage is allocated.
64  */
66  : m_single(nullptr)
67  {
68  }
69 
70  /**
71  * @brief Deleted copy constructor.
72  */
73  Singleton(const Singleton&) = delete;
74 
75  /**
76  * @brief Deleted move constructor.
77  */
78  Singleton(Singleton&&) = delete;
79  /**
80  * @brief Deleted copy assignment.
81  */
82  Singleton& operator=(const Singleton&) = delete;
83 
84  /**
85  * @brief Deleted move assignment.
86  */
87  Singleton& operator=(Singleton&&) = delete;
88 
89  /**
90  * @brief Access the singleton.
91  *
92  * The access of the singleton must be done after a storage has been
93  * provided. Otherwise, the behaviour is undefined (and may result in a
94  * crash).
95  *
96  * @returns a reference to the stored singleton
97  */
98  T& operator()() {
99  assert(m_single);
100  return *m_single;
101  }
102 
103  /**
104  * @brief Reset the singleton
105  *
106  * After a call to this function, the singleton must be accessed anymore.
107  */
108  void reset() noexcept {
109  m_single = nullptr;
110  }
111 
112  /**
113  * @brief Check if the singleton has been initialized
114  *
115  * @return True if the singleton is valid
116  */
117  bool isValid() const noexcept {
118  return m_single != nullptr;
119  }
120 
121  private:
122  friend class SingletonStorage<T>;
123 
124  T *m_single;
125  };
126 
127  /**
128  * @ingroup core
129  * @brief A storage for a singleton.
130  *
131  * This class is a wrapper around an object of class `T` that should be
132  * unique in the system. It works in cooperation with a global variable of
133  * type gf::Singleton<T>. The global variable is for the access of the object
134  * while this class is for the storage of the object. The global variable is
135  * initialized at the storage construction.
136  *
137  * ~~~{.cc}
138  * gf::Singleton<Foo> gFoo;
139  *
140  * int main() {
141  * gf::SingletonStorage<Foo> fooStorage(gFoo);
142  *
143  * gFoo().bar();
144  * }
145  * ~~~
146  *
147  * @sa gf::Singleton
148  */
149  template<typename T>
150  class SingletonStorage {
151  public:
152  /**
153  * @brief Construct a storage for a singleton.
154  *
155  *
156  * @param ref a reference to a singleton (a global variable)
157  * @param args arguments for the constructor of class `T`
158  */
159  template<typename ... Args>
160  SingletonStorage(Singleton<T>& ref, Args&&... args)
161  : m_storage(std::forward<Args>(args)...) {
162  assert(ref.m_single == nullptr);
163  ref.m_single = &m_storage;
164  }
165 
166  private:
167  T m_storage;
168  };
169 
170 #ifndef DOXYGEN_SHOULD_SKIP_THIS
171 }
172 #endif
173 }
174 
175 #endif // GF_SINGLETON_H
void reset() noexcept
Reset the singleton.
Definition: Singleton.h:108
friend class RenderTarget
Definition: Shader.h:387
Singleton & operator=(Singleton &&)=delete
Deleted move assignment.
Singleton(Singleton &&)=delete
Deleted move constructor.
Definition: Action.h:34
T & operator()()
Access the singleton.
Definition: Singleton.h:98
Singleton()
Default constructor.
Definition: Singleton.h:65
Singleton(const Singleton &)=delete
Deleted copy constructor.
bool isValid() const noexcept
Check if the singleton has been initialized.
Definition: Singleton.h:117
Singleton & operator=(const Singleton &)=delete
Deleted copy assignment.
SingletonStorage(Singleton< T > &ref, Args &&...args)
Construct a storage for a singleton.
Definition: Singleton.h:160
A singleton that wraps a pointer provided by a storage.
Definition: Singleton.h:57