Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/ScopedSingleton.h @ 5732

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

merged libraries branch back to trunk

  • 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.