Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/singleton/Scope.h @ 10463

Last change on this file since 10463 was 10463, checked in by landauf, 9 years ago

removed unnecessary instance counts. creation of scopes is strictly controlled (in Core.cc)

  • 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30@file
31@ingroup SingletonScope
32@brief Declaration of the classes that are needed to use Scopes: orxonox::Scope and orxonox::ScopeListener.
33
34@anchor Scope
35
36A virtual scope can be represented by an instance of class orxonox::Scope. orxonox::Scope<@a scope> is a template
37an its template argument defines the name of the virtual scope. See orxonox::ScopeID for an enumeration of the
38available values for @a scope. The orxonox::Scope object for a given @a scope can be activated or deactivated.
39Instances of orxonox::ScopeListener can register in orxonox::ScopeMAnager for a given @a scope and will get a
40notification if the corresponding orxonox::Scope object changes its state.
41
42Scopes are usually used to control the creation and destruction of Singletons.
43
44@see orxonox::Singleton
45*/
46
47#ifndef __Core_Scope_H__
48#define __Core_Scope_H__
49
50#include "core/CorePrereqs.h"
51
52#include <cassert>
53#include <map>
54#include <set>
55#include <loki/ScopeGuard.h>
56
57#include "util/Output.h"
58#include "ScopeManager.h"
59
60namespace orxonox
61{
62    /**
63        @brief ScopeListeners register themselves in the corresponding Scope and wait for notifications.
64        Notifications are sent if a Scope is activated or deactivated.
65
66        @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
67    */
68    class _CoreExport ScopeListener
69    {
70        template <ScopeID::Value scope>
71        friend class Scope;
72
73        protected:
74            ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false) { }
75            virtual ~ScopeListener() { }
76
77            //! Gets called if the scope is activated
78            virtual void activated() = 0;
79            //! Gets called if the scope is deactivated
80            virtual void deactivated() = 0;
81
82        public:
83            inline ScopeID::Value getScope() const
84                { return this->scope_; }
85
86        private:
87            ScopeID::Value scope_; //!< Store the scope to unregister on destruction
88            bool bActivated_;
89    };
90
91    /**
92        @brief A scope for a given template argument is either active or not.
93
94        Objects inheriting from a ScopeListener are registered in a list (different for each scope).
95        If the scope gets activated or deactivated, all objects in this list are notified.
96
97        @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
98    */
99    template <ScopeID::Value scope>
100    class Scope
101    {
102        public:
103            //! Constructor: activate the listeners.
104            Scope()
105            {
106                orxout(internal_status) << "creating scope... (" << scope << ")" << endl;
107
108                Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateScope);
109                ScopeManager::getInstance().addScope(scope);
110                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
111                {
112                    (*it)->activated();
113                    (*(it++))->bActivated_ = true;
114                }
115                deactivator.Dismiss();
116
117                orxout(internal_status) << "created scope (" << scope << ")" << endl;
118            }
119
120            //! Destructor: deactivate the listeners.
121            ~Scope()
122            {
123                orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
124
125                this->deactivateScope();
126
127                orxout(internal_status) << "destroyed scope (" << scope << ")" << endl;
128            }
129
130            //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails.
131            void deactivateScope()
132            {
133                ScopeManager::getInstance().removeScope(scope);
134                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getInstance().getListeners(scope).begin(); it != ScopeManager::getInstance().getListeners(scope).end(); )
135                {
136                    if ((*it)->bActivated_)
137                    {
138                        try
139                            { (*it)->deactivated(); }
140                        catch (...)
141                            { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; }
142                        (*(it++))->bActivated_ = false;
143                    }
144                    else
145                        ++it;
146                }
147            }
148
149            //! Returns true if the scope is active.
150            static bool isActive()
151            {
152                return ScopeManager::getInstance().isActive(scope);
153            }
154    };
155}
156
157#endif /* __Core_Scope_H__ */
Note: See TracBrowser for help on using the repository browser.