Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

refactoring: moved code from Scope.h into ScopeManager

  • Property svn:eol-style set to native
File size: 4.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        friend class ScopeManager;
71
72        protected:
73            ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false) { }
74            virtual ~ScopeListener() { }
75
76            //! Gets called if the scope is activated
77            virtual void activated() = 0;
78            //! Gets called if the scope is deactivated
79            virtual void deactivated() = 0;
80
81        public:
82            inline ScopeID::Value getScope() const
83                { return this->scope_; }
84
85        private:
86            ScopeID::Value scope_; //!< Store the scope to unregister on destruction
87            bool bActivated_;
88    };
89
90    /**
91        @brief A scope for a given template argument is either active or not.
92
93        Objects inheriting from a ScopeListener are registered in a list (different for each scope).
94        If the scope gets activated or deactivated, all objects in this list are notified.
95
96        @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
97    */
98    template <ScopeID::Value scope>
99    class Scope
100    {
101        public:
102            //! Constructor: activate the listeners.
103            Scope()
104            {
105                orxout(internal_status) << "creating scope... (" << scope << ")" << endl;
106
107                Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateScope);
108                ScopeManager::getInstance().addScope(scope);
109                deactivator.Dismiss();
110
111                orxout(internal_status) << "created scope (" << scope << ")" << endl;
112            }
113
114            //! Destructor: deactivate the listeners.
115            ~Scope()
116            {
117                orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
118
119                this->deactivateScope();
120
121                orxout(internal_status) << "destroyed scope (" << scope << ")" << endl;
122            }
123
124            //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails.
125            void deactivateScope()
126            {
127                ScopeManager::getInstance().removeScope(scope);
128            }
129
130            //! Returns true if the scope is active.
131            static bool isActive()
132            {
133                return ScopeManager::getInstance().isActive(scope);
134            }
135    };
136}
137
138#endif /* __Core_Scope_H__ */
Note: See TracBrowser for help on using the repository browser.