Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

renamed ScopedSingletonManager to ScopedSingletonWrapper. removed static maps.

  • Property svn:eol-style set to native
File size: 7.9 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
32@brief Declaration of the classes that are needed to use Scopes:
33orxonox::Scope, orxonox::ScopeListener, and orxonox::ScopeManager.
34
35@anchor Scope
36
37A virtual scope can be represented by an instance of class orxonox::Scope. orxonox::Scope<@a scope> is a template
38an its template argument defines the name of the virtual scope. See orxonox::ScopeID for an enumeration of the
39available values for @a scope. The orxonox::Scope object for a given @a scope can be activated or deactivated.
40Instances of orxonox::ScopeListener can register for a given @a scope and will get a notification if the
41corresponding orxonox::Scope object changes its state.
42
43To avoid multiple instances of orxonox::Scope<@a scope> in different libraries, each instance of orxonox::Scope
[10407]44registers in orxonox::ScopeManager, where they are linked statically in the core library.
[7401]45
46Scopes are usually used to control the creation and destruction of Singletons.
47
48@see orxonox::Singleton
49*/
50
[10407]51#ifndef __Core_Scope_H__
52#define __Core_Scope_H__
[5640]53
[10407]54#include "core/CorePrereqs.h"
[5929]55
[5640]56#include <cassert>
[5929]57#include <map>
[5640]58#include <set>
[7266]59#include <loki/ScopeGuard.h>
[5929]60
[10407]61#include "util/Output.h"
[5640]62
63namespace orxonox
64{
65    /**
[7401]66        @brief The ScopeManager stores the variables of the Scope templates in a statically linked context.
67
[10407]68        If all Scope objects are managed by this class, they are statically linked in the core library.
[7401]69        Without this, a new instance of Scope<T> for each T would be created in every library of Orxonox,
70        which is of course not the desired behavior.
71
72        @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
[5640]73    */
[10407]74    class _CoreExport ScopeManager
[5640]75    {
76        template <ScopeID::Value scope>
77        friend class Scope;
78        friend class ScopeListener;
79
80        private:
[10412]81            static std::map<ScopeID::Value, int>& getInstanceCounts();                  //!< Counts the number of active instances (>0 means active) for a scope
82            static std::map<ScopeID::Value, std::set<ScopeListener*> >& getListeners(); //!< Stores all listeners for a scope
[5640]83    };
84
85    /**
[7401]86        @brief ScopeListeners register themselves in the corresponding Scope and wait for notifications.
87        Notifications are sent if a Scope is activated or deactivated.
88
89        @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
[5640]90    */
[10407]91    class _CoreExport ScopeListener
[5640]92    {
93        template <ScopeID::Value scope>
94        friend class Scope;
95
96        protected:
97            //! Constructor: Registers the instance.
[5929]98            ScopeListener(ScopeID::Value scope) : scope_(scope), bActivated_(false)
[10412]99                { ScopeManager::getListeners()[this->scope_].insert(this); }
[5640]100            //! Destructor: Unregisters the instance.
101            virtual ~ScopeListener()
[10412]102                { ScopeManager::getListeners()[this->scope_].erase(this); }
[5640]103
104            //! Gets called if the scope is activated
105            virtual void activated() = 0;
106            //! Gets called if the scope is deactivated
107            virtual void deactivated() = 0;
108
109        private:
110            ScopeID::Value scope_; //!< Store the scope to unregister on destruction
[5929]111            bool bActivated_;
[5640]112    };
113
114    /**
115        @brief A scope for a given template argument is either active or not.
116
117        Objects inheriting from a ScopeListener are registered in a list (different for each scope).
118        If the scope gets activated or deactivated, all objects in this list are notified.
[7401]119
120        @see See @ref Scope "this description" for details about the interrelationship of Scope, ScopeListener, and ScopeManager.
[5640]121    */
122    template <ScopeID::Value scope>
123    class Scope
124    {
125        public:
126            //! Constructor: Increases the instance counter and activates the scope if the count went from 0 to 1. Counts >1 don't change anything.
127            Scope()
128            {
[8858]129                orxout(internal_status) << "creating scope... (" << scope << ")" << endl;
130
[5929]131                try
[5640]132                {
[10412]133                    ScopeManager::getInstanceCounts()[scope]++;
134                    assert(ScopeManager::getInstanceCounts()[scope] > 0);
135                    if (ScopeManager::getInstanceCounts()[scope] == 1)
[5929]136                    {
137                        Loki::ScopeGuard deactivator = Loki::MakeObjGuard(*this, &Scope::deactivateListeners);
[10412]138                        for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
[5929]139                        {
140                            (*it)->activated();
141                            (*(it++))->bActivated_ = true;
142                        }
143                        deactivator.Dismiss();
144                    }
[5640]145                }
[5929]146                catch (...)
147                {
[10412]148                    ScopeManager::getInstanceCounts()[scope]--;
[5929]149                    throw;
150                }
[8858]151
152                orxout(internal_status) << "created scope (" << scope << ")" << endl;
[5640]153            }
154
155            //! Destructor: Decreases the instance counter and deactivates the scope if the count went from 1 to 0. Counts >0 don't change anything.
156            ~Scope()
157            {
[8858]158                orxout(internal_status) << "destroying scope... (" << scope << ")" << endl;
159
[10412]160                ScopeManager::getInstanceCounts()[scope]--;
[5640]161
162                // This shouldn't happen but just to be sure: check if the count is positive
[10412]163                assert(ScopeManager::getInstanceCounts()[scope] >= 0);
164                if (ScopeManager::getInstanceCounts()[scope] < 0)
165                    ScopeManager::getInstanceCounts()[scope] = 0;
[5640]166
[10412]167                if (ScopeManager::getInstanceCounts()[scope] == 0)
[5929]168                    this->deactivateListeners();
[8858]169
170                orxout(internal_status) << "destroyed scope (" << scope << ")" << endl;
[5929]171            }
172
[7401]173            //! Deactivates the listeners of this scope in case the scope is destroyed or the construction fails.
[5929]174            void deactivateListeners()
175            {
[10412]176                for (typename std::set<ScopeListener*>::iterator it = ScopeManager::getListeners()[scope].begin(); it != ScopeManager::getListeners()[scope].end(); )
[5640]177                {
[5929]178                    if ((*it)->bActivated_)
179                    {
180                        try
181                            { (*it)->deactivated(); }
182                        catch (...)
[8858]183                            { orxout(internal_warning) << "ScopeListener::deactivated() failed! This MUST NOT happen, fix it!" << endl; }
[5929]184                        (*(it++))->bActivated_ = false;
185                    }
186                    else
187                        ++it;
[5640]188                }
189            }
190
191            //! Returns true if the scope is active.
192            static bool isActive()
193            {
[10412]194                return (ScopeManager::getInstanceCounts()[scope] > 0);
[5640]195            }
196    };
197}
198
[10407]199#endif /* __Core_Scope_H__ */
Note: See TracBrowser for help on using the repository browser.