Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/singleton/ScopedSingletonWrapper.h @ 10514

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

don't store scope in ScopeListener - instead register the listener in ScopeManager for a specific scope

  • Property svn:eol-style set to native
File size: 6.1 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 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @ingroup SingletonScope
32    @brief Definition of orxonox::ScopedSingletonWrapper and orxonox::ClassScopedSingletonWrapper.
33
34    ScopedSingletonWrapper is used to create and destroy Singletons that belong to
35    a given Scope. For each one of these singletons, the macro ManageScopedSingleton()
36    has to be called to register the wrapper with orxonox::ScopeManager.
37
38    See @ref SingletonExample "this code" for an example.
39
40    @see orxonox::Singleton
41    @see orxonox::Scope
42*/
43
44#ifndef __ScopedSingletonWrapper_H__
45#define __ScopedSingletonWrapper_H__
46
47#include "core/CorePrereqs.h"
48
49#include <cassert>
50#include "util/Exception.h"
51#include "util/Singleton.h"
52#include "Scope.h"
53
54namespace orxonox
55{
56    /**
57        @brief Base class of ClassScopedSingletonWrapper.
58    */
59    class _CoreExport ScopedSingletonWrapper : public ScopeListener
60    {
61        public:
62            /// Constructor: Initializes all the values
63            ScopedSingletonWrapper(const std::string& className)
64                : className_(className)
65            { }
66            virtual ~ScopedSingletonWrapper() { }
67
68        protected:
69            const std::string className_;   ///< The name of the scoped singleton class that is managed by this object
70    };
71
72    /**
73        @anchor ClassScopedSingletonWrapper
74
75        @brief Manages a scoped singleton
76        @param T The managed singleton class
77        @param allowedToFail If true, a specialization of this template is used, that uses try-catch blocks to handle possible failures.
78
79        This class inherits from ScopeListener and is registered for a scope in ScopeManager and thus its functions
80        activated() and deactivated() are called whenever the Scope changes its state.
81
82        If the Scope is activated, a new instance of @a T (which must be a singleton) is created.
83        If the Scope is deactivated, the singleton is destroyed.
84
85        @see Singleton
86    */
87    template <class T, bool allowedToFail>
88    class ClassScopedSingletonWrapper : public ScopedSingletonWrapper
89    {
90    public:
91        ClassScopedSingletonWrapper(const std::string& className)
92            : ScopedSingletonWrapper(className)
93            , singletonPtr_(NULL)
94        {
95        }
96
97        ~ClassScopedSingletonWrapper()
98        {
99        }
100
101        //! Called if the Scope of the Singleton gets active (creates the instance)
102        void activated()
103        {
104            assert(singletonPtr_ == NULL);
105            singletonPtr_ = new T();
106        }
107
108        //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
109        void deactivated()
110        {
111            assert(singletonPtr_ != NULL);
112            this->destroy(singletonPtr_);
113            singletonPtr_ = NULL;
114        }
115
116        //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy()
117        void destroy(Destroyable*)
118        {
119            singletonPtr_->destroy();
120        }
121        //! Destroys the singleton instance - overloaded for all other pointers, calls delete
122        void destroy(void*)
123        {
124            delete singletonPtr_;
125        }
126
127    private:
128        T* singletonPtr_;   ///< Unique instance of the singleton class @a T
129    };
130
131    /**
132        @brief This class partially spezializes ClassScopedSingletonWrapper for classes @a T that are allowed to fail.
133        @param T The managed singleton class
134
135        Because @a T could fail when being created, this partial spezialization of ClassScopedSingletonWrapper
136        uses a try-catch block to handle exceptions.
137
138        See @ref ClassScopedSingletonWrapper for a full documentation of the basis template.
139    */
140    template <class T>
141    class ClassScopedSingletonWrapper<T, true> : public ScopedSingletonWrapper
142    {
143    public:
144        ClassScopedSingletonWrapper(const std::string& className)
145            : ScopedSingletonWrapper(className)
146            , singletonPtr_(NULL)
147        {
148        }
149
150        ~ClassScopedSingletonWrapper()
151        {
152        }
153
154        //! Called if the Scope of the Singleton gets active (creates the instance)
155        void activated()
156        {
157            assert(singletonPtr_ == NULL);
158            try
159                { singletonPtr_ = new T(); }
160            catch (const InitialisationAbortedException& ex)
161                { orxout(internal_error) << ex.getDescription() << endl; }
162            catch (...)
163                { orxout(internal_error) << "Singleton creation failed: " << Exception::handleMessage() << endl; }
164        }
165
166        //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
167        void deactivated()
168        {
169            if (singletonPtr_ != NULL)
170            {
171                this->destroy(singletonPtr_);
172                singletonPtr_ = NULL;
173            }
174        }
175
176        //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy()
177        void destroy(Destroyable*)
178        {
179            singletonPtr_->destroy();
180        }
181        //! Destroys the singleton instance - overloaded for void*, calls delete
182        void destroy(void*)
183        {
184            delete singletonPtr_;
185        }
186
187    private:
188        T* singletonPtr_;   ///< Unique instance of the singleton class @a T
189    };
190}
191
192#endif /* __ScopedSingletonWrapper_H__ */
Note: See TracBrowser for help on using the repository browser.