Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 4.4 KB
RevLine 
[5640]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
[7401]29/**
30@file
31@ingroup SingletonScope
[10461]32@brief Declaration of the classes that are needed to use Scopes: orxonox::Scope and orxonox::ScopeListener.
[7401]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.
[10461]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.
[7401]41
42Scopes are usually used to control the creation and destruction of Singletons.
43
44@see orxonox::Singleton
45*/
46
[10407]47#ifndef __Core_Scope_H__
48#define __Core_Scope_H__
[5640]49
[10407]50#include "core/CorePrereqs.h"
[5929]51
[5640]52#include <cassert>
[5929]53#include <map>
[5640]54#include <set>
[7266]55#include <loki/ScopeGuard.h>
[5929]56
[10407]57#include "util/Output.h"
[10461]58#include "ScopeManager.h"
[5640]59
60namespace orxonox
61{
62    /**
[7401]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.
[5640]67    */
[10407]68    class _CoreExport ScopeListener
[5640]69    {
[10513]70        friend class ScopeManager;
[5640]71
72        protected:
[10514]73            ScopeListener() : bActivated_(false) { }
[11071]74            virtual ~ScopeListener() = default;
[5640]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:
[5929]82            bool bActivated_;
[5640]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.
[7401]90
91        @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
[5640]92    */
93    template <ScopeID::Value scope>
94    class Scope
95    {
96        public:
[10463]97            //! Constructor: activate the listeners.
[5640]98            Scope()
99            {
[8858]100                orxout(internal_status) << "creating scope... (" << scope << ")" << endl;
101
[10463]102                Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateScope);
103                ScopeManager::getInstance().addScope(scope);
104                deactivator.Dismiss();
[8858]105
106                orxout(internal_status) << "created scope (" << scope << ")" << endl;
[5640]107            }
108
[10463]109            //! Destructor: deactivate the listeners.
[5640]110            ~Scope()
111            {
[8858]112                orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
113
[10463]114                this->deactivateScope();
[5640]115
[8858]116                orxout(internal_status) << "destroyed scope (" << scope << ")" << endl;
[5929]117            }
118
[7401]119            //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails.
[10463]120            void deactivateScope()
[5929]121            {
[10463]122                ScopeManager::getInstance().removeScope(scope);
[5640]123            }
124
125            //! Returns true if the scope is active.
126            static bool isActive()
127            {
[10463]128                return ScopeManager::getInstance().isActive(scope);
[5640]129            }
130    };
131}
132
[10407]133#endif /* __Core_Scope_H__ */
Note: See TracBrowser for help on using the repository browser.