Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/libraries/src/util/ScopedSingleton.h @ 5640

Last change on this file since 5640 was 5640, checked in by landauf, 15 years ago

Added a new utility "ScopedSingleton". A ScopedSingleton has a scope which is represented by a Scope template. ScopedSingletons are allowed to exist (= get created automatically after the first call to getInstance()) as soon as at least one instance of the corresponding Scope exists. If all those instances of Scope are deleted, the corresponding ScopedSingletons are destroyed too.

This allows us to create Singletons (for example the QuestManager) which may exist in a plugin and still get created and destroyed in parallel with GSLevel (or whatever Scope you like). All we have to do is to create and destroy one instance of Scope in GSLevel (or whoever defines the Scope). Therefore we don't have to link the singleton into GSLevel which is quite handy if we want to build independent plugins.

Btw, this can be expanded to other constructs, not just singletons. Just inherit from ScopeListener to get the full load of features.

(Note to Reto: I had to add a "ScopeManager" which ensures static linkage of the scope counters and listeners. Otherwise we'd end up with different counts in each library which is quite bad. Maybe you have a better idea? Or maybe you want to make the ScopeManager a singleton too? ;))

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
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
37namespace orxonox
38{
39    /**
40    @brief
41        Base for scoped singleton classes.
42        A Scoped singleton creates itself if the scope is active and getInstance() is called.
43        Destroys itself if the scope is deactivated.
44
45        Usage:
46        Inherit publicly from ScopedSingleton<MyClass, scope> and provide access to
47        MyClass::singletonPtr_s.
48        This can easily be done with a friend declaration.
49
50        See @ref UtilPrereqs.h for a list of scopes (ScopeID::Value).
51    */
52    template <class T, ScopeID::Value scope>
53    class ScopedSingleton : public ScopeListener
54    {
55        public:
56            //! Returns a reference to the singleton instance
57            static T& getInstance()
58            {
59                assert(Scope<scope>::isActive());
60
61                if (!T::singletonPtr_s && Scope<scope>::isActive())
62                    T::singletonPtr_s = new T();
63
64                return *T::singletonPtr_s;
65            }
66
67        protected:
68            //! Constructor sets the singleton instance pointer
69            ScopedSingleton() : ScopeListener(scope)
70            {
71                assert(T::singletonPtr_s == 0);
72                T::singletonPtr_s = static_cast<T*>(this);
73            }
74
75            //! Constructor resets the singleton instance pointer
76            ~ScopedSingleton()
77            {
78                assert(T::singletonPtr_s != 0);
79                T::singletonPtr_s = 0;
80            }
81
82        private:
83            //! Called if the Scope of this Singleton gets active (no instance should be active then)
84            void activated()
85            {
86                // The ScopedSingleton shouldn't be active bevor the scope is activated -> always assertion failed
87                assert(T::singletonPtr_s == 0 && false);
88            }
89
90            //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
91            void deactivated()
92            {
93                if (T::singletonPtr_s)
94                {
95                    delete T::singletonPtr_s;
96                    T::singletonPtr_s = 0;
97                }
98            }
99    };
100}
101
102#endif /* __Util_ScopedSingleton_H__ */
Note: See TracBrowser for help on using the repository browser.