Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/singleton/Scope.h @ 11118

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

merged branch cpp11_v3 back to trunk

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