Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/ScopedSingletonManager.h @ 6067

Last change on this file since 6067 was 6035, checked in by rgrieder, 15 years ago

Fixed program termination segfault in the trunk

  • Property svn:eol-style set to native
File size: 5.6 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 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef __ScopedSingletonManager_H__
30#define __ScopedSingletonManager_H__
31
32#include "CorePrereqs.h"
33
34#include <cassert>
35#include <map>
36#include "util/Exception.h"
37#include "util/Scope.h"
38#include "util/Singleton.h"
39
40#define ManageScopedSingleton(className, scope, allowedToFail) \
41    static ClassScopedSingletonManager<className, scope, allowedToFail> className##ScopedSingletonManager(#className)
42
43namespace orxonox
44{
45    class _CoreExport ScopedSingletonManager
46    {
47        public:
48            ScopedSingletonManager(const std::string& className, ScopeID::Value scope)
49                : className_(className)
50                , scope_(scope)
51            { }
52            virtual ~ScopedSingletonManager() { }
53            static void addManager(ScopedSingletonManager* manager);
54
55            template<ScopeID::Value scope>
56            static void update(const Clock& time)
57            {
58                assert(Scope<scope>::isActive());
59                for (ManagerMultiMap::iterator it = getManagersByScope().lower_bound(scope); it != getManagersByScope().upper_bound(scope); ++it)
60                    it->second->update(time);
61            }
62            virtual void update(const Clock& time) = 0;
63
64            static std::map<std::string, ScopedSingletonManager*>& getManagers();
65            typedef std::multimap<ScopeID::Value, ScopedSingletonManager*> ManagerMultiMap;
66            static ManagerMultiMap& getManagersByScope();
67
68        protected:
69            const std::string className_;
70            const ScopeID::Value scope_;
71    };
72
73    template <class T, ScopeID::Value scope, bool allowedToFail>
74    class ClassScopedSingletonManager : public ScopedSingletonManager, public ScopeListener
75    {
76    public:
77        ClassScopedSingletonManager(const std::string& className)
78            : ScopedSingletonManager(className, scope)
79            , ScopeListener(scope)
80            , singletonPtr_(NULL)
81        {
82            ScopedSingletonManager::addManager(this);
83        }
84
85        ~ClassScopedSingletonManager()
86        {
87        }
88
89        //! Called if the Scope of the Singleton gets active (creates the instance)
90        void activated()
91        {
92            assert(singletonPtr_ == NULL);
93            singletonPtr_ = new T();
94        }
95
96        //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
97        void deactivated()
98        {
99            assert(singletonPtr_ != NULL);
100            this->destroy(singletonPtr_);
101            singletonPtr_ = NULL;
102        }
103
104        void destroy(OrxonoxClass*)
105        {
106            singletonPtr_->destroy();
107        }
108        void destroy(void*)
109        {
110            delete singletonPtr_;
111        }
112
113        //! Called every frame by the ScopedSingletonManager
114        void update(const Clock& time)
115        {
116            assert(Scope<scope>::isActive());
117            // assuming T inherits Singleton<T>
118            singletonPtr_->updateSingleton(time);
119        }
120
121    private:
122        T* singletonPtr_;
123    };
124
125    template <class T, ScopeID::Value scope>
126    class ClassScopedSingletonManager<T, scope, true> : public ScopedSingletonManager, public ScopeListener
127    {
128    public:
129        ClassScopedSingletonManager(const std::string& className)
130            : ScopedSingletonManager(className, scope)
131            , ScopeListener(scope)
132            , singletonPtr_(NULL)
133        {
134            ScopedSingletonManager::addManager(this);
135        }
136
137        ~ClassScopedSingletonManager()
138        {
139        }
140
141        //! Called if the Scope of the Singleton gets active (creates the instance)
142        void activated()
143        {
144            assert(singletonPtr_ == NULL);
145            try
146                { singletonPtr_ = new T(); }
147            catch (...)
148                { COUT(1) << "Singleton creation failed: " << Exception::handleMessage() << std::endl; }
149        }
150
151        //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
152        void deactivated()
153        {
154            if (singletonPtr_ != NULL)
155            {
156                this->destroy(singletonPtr_);
157                singletonPtr_ = NULL;
158            }
159        }
160
161        void destroy(OrxonoxClass* ptr)
162        {
163            singletonPtr_->destroy();
164        }
165        void destroy(void* ptr)
166        {
167            delete singletonPtr_;
168        }
169
170        //! Called every frame by the ScopedSingletonManager
171        void update(const Clock& time)
172        {
173            assert(Scope<scope>::isActive());
174            // assuming T inherits Singleton<T>
175            if (singletonPtr_ != NULL)
176                singletonPtr_->updateSingleton(time);
177        }
178
179    private:
180        T* singletonPtr_;
181    };
182}
183
184#endif /* __ScopedSingletonManager_H__ */
Note: See TracBrowser for help on using the repository browser.