1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef __Util_ScopedSingleton_H__ |
---|
30 | #define __Util_ScopedSingleton_H__ |
---|
31 | |
---|
32 | #include "UtilPrereqs.h" |
---|
33 | #include <cassert> |
---|
34 | |
---|
35 | #include "Scope.h" |
---|
36 | |
---|
37 | namespace orxonox |
---|
38 | { |
---|
39 | class Clock; |
---|
40 | /** |
---|
41 | @brief |
---|
42 | Base for scoped singleton classes. |
---|
43 | A Scoped singleton creates itself if the scope is active and getInstance() is called. |
---|
44 | Destroys itself if the scope is deactivated. |
---|
45 | |
---|
46 | Usage: |
---|
47 | Inherit publicly from ScopedSingleton<MyClass, scope> and provide access to |
---|
48 | MyClass::singletonPtr_s. |
---|
49 | This can easily be done with a friend declaration. |
---|
50 | |
---|
51 | See @ref UtilPrereqs.h for a list of scopes (ScopeID::Value). |
---|
52 | */ |
---|
53 | template <class T, ScopeID::Value scope> |
---|
54 | class ScopedSingleton : public ScopeListener |
---|
55 | { |
---|
56 | public: |
---|
57 | //! Returns a reference to the singleton instance |
---|
58 | static T& getInstance() |
---|
59 | { |
---|
60 | assert(Scope<scope>::isActive()); |
---|
61 | |
---|
62 | if (!T::singletonPtr_s) |
---|
63 | T::singletonPtr_s = new T(); |
---|
64 | |
---|
65 | return *T::singletonPtr_s; |
---|
66 | } |
---|
67 | |
---|
68 | //! Update method for singletons like the ingame console |
---|
69 | virtual void updated(const Clock& time) { static_cast<T*>(this)->update(time); } |
---|
70 | //! Empty update method for the static polymorphism |
---|
71 | void update(const Clock& time) { } |
---|
72 | |
---|
73 | protected: |
---|
74 | //! Constructor sets the singleton instance pointer |
---|
75 | ScopedSingleton() : ScopeListener(scope) |
---|
76 | { |
---|
77 | assert(T::singletonPtr_s == 0); |
---|
78 | T::singletonPtr_s = static_cast<T*>(this); |
---|
79 | } |
---|
80 | |
---|
81 | //! Constructor resets the singleton instance pointer |
---|
82 | ~ScopedSingleton() |
---|
83 | { |
---|
84 | assert(T::singletonPtr_s != 0); |
---|
85 | T::singletonPtr_s = 0; |
---|
86 | } |
---|
87 | |
---|
88 | private: |
---|
89 | //! Called if the Scope of this Singleton gets active (no instance should be active then) |
---|
90 | void activated() |
---|
91 | { |
---|
92 | // The ScopedSingleton shouldn't be active bevor the scope is activated -> always assertion failed |
---|
93 | assert(false); |
---|
94 | } |
---|
95 | |
---|
96 | //! Called if the Scope of this Singleton gets deactivated (destroys the instance) |
---|
97 | void deactivated() |
---|
98 | { |
---|
99 | if (T::singletonPtr_s) |
---|
100 | { |
---|
101 | T::singletonPtr_s->destroy(); |
---|
102 | T::singletonPtr_s = 0; |
---|
103 | } |
---|
104 | } |
---|
105 | }; |
---|
106 | } |
---|
107 | |
---|
108 | #endif /* __Util_ScopedSingleton_H__ */ |
---|