Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

wrap ScopedSingletonWrapper in a StaticallyInitializedScopedSingletonWrapper. doesn't do a lot at the moment.

  • Property svn:eol-style set to native
File size: 6.8 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
60    {
61        public:
62            /// Constructor: Initializes all the values
63            ScopedSingletonWrapper(const std::string& className, ScopeID::Value scope)
64                : className_(className)
65                , scope_(scope)
66            { }
67            virtual ~ScopedSingletonWrapper() { }
68
69        protected:
70            const std::string className_;   ///< The name of the scoped singleton class that is managed by this object
71            const ScopeID::Value scope_;    ///< The scope of the singleton that is managed by this object
72    };
73
74    /**
75        @anchor ClassScopedSingletonWrapper
76
77        @brief Manages a scoped singleton for a given scope.
78        @param T The managed singleton class
79        @param scope The scope in which the singleton @a T should be active
80        @param allowedToFail If true, a specialization of this template is used, that uses try-catch blocks to handle possible failures.
81
82        This class inherits from ScopeListener for the given scope and thus its functions
83        activated() and deactivated() are called whenever the Scope changes its state.
84
85        If the Scope is activated, a new instance of @a T (which must be a singleton) is created.
86        If the Scope is deactivated, the singleton is destroyed.
87
88        @see Singleton
89    */
90    template <class T, ScopeID::Value scope, bool allowedToFail>
91    class ClassScopedSingletonWrapper : public ScopedSingletonWrapper, public ScopeListener
92    {
93    public:
94        //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonWrapper and ScopeListener
95        ClassScopedSingletonWrapper(const std::string& className)
96            : ScopedSingletonWrapper(className, scope)
97            , ScopeListener(scope)
98            , singletonPtr_(NULL)
99        {
100        }
101
102        ~ClassScopedSingletonWrapper()
103        {
104        }
105
106        //! Called if the Scope of the Singleton gets active (creates the instance)
107        void activated()
108        {
109            assert(singletonPtr_ == NULL);
110            singletonPtr_ = new T();
111        }
112
113        //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
114        void deactivated()
115        {
116            assert(singletonPtr_ != NULL);
117            this->destroy(singletonPtr_);
118            singletonPtr_ = NULL;
119        }
120
121        //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy()
122        void destroy(Destroyable*)
123        {
124            singletonPtr_->destroy();
125        }
126        //! Destroys the singleton instance - overloaded for all other pointers, calls delete
127        void destroy(void*)
128        {
129            delete singletonPtr_;
130        }
131
132    private:
133        T* singletonPtr_;   ///< Unique instance of the singleton class @a T
134    };
135
136    /**
137        @brief This class partially spezializes ClassScopedSingletonWrapper for classes @a T that are allowed to fail.
138        @param T The managed singleton class
139        @param scope The scope in which the singleton @a T should be active
140
141        Because @a T could fail when being created, this partial spezialization of ClassScopedSingletonWrapper
142        uses a try-catch block to handle exceptions.
143
144        See @ref ClassScopedSingletonWrapper for a full documentation of the basis template.
145    */
146    template <class T, ScopeID::Value scope>
147    class ClassScopedSingletonWrapper<T, scope, true> : public ScopedSingletonWrapper, public ScopeListener
148    {
149    public:
150        //! Constructor: Initializes the singleton pointer and passes the scope to ScopedSingletonWrapper and ScopeListener
151        ClassScopedSingletonWrapper(const std::string& className)
152            : ScopedSingletonWrapper(className, scope)
153            , ScopeListener(scope)
154            , singletonPtr_(NULL)
155        {
156        }
157
158        ~ClassScopedSingletonWrapper()
159        {
160        }
161
162        //! Called if the Scope of the Singleton gets active (creates the instance)
163        void activated()
164        {
165            assert(singletonPtr_ == NULL);
166            try
167                { singletonPtr_ = new T(); }
168            catch (const InitialisationAbortedException& ex)
169                { orxout(internal_error) << ex.getDescription() << endl; }
170            catch (...)
171                { orxout(internal_error) << "Singleton creation failed: " << Exception::handleMessage() << endl; }
172        }
173
174        //! Called if the Scope of this Singleton gets deactivated (destroys the instance)
175        void deactivated()
176        {
177            if (singletonPtr_ != NULL)
178            {
179                this->destroy(singletonPtr_);
180                singletonPtr_ = NULL;
181            }
182        }
183
184        //! Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy()
185        void destroy(Destroyable*)
186        {
187            singletonPtr_->destroy();
188        }
189        //! Destroys the singleton instance - overloaded for void*, calls delete
190        void destroy(void*)
191        {
192            delete singletonPtr_;
193        }
194
195    private:
196        T* singletonPtr_;   ///< Unique instance of the singleton class @a T
197    };
198}
199
200#endif /* __ScopedSingletonWrapper_H__ */
Note: See TracBrowser for help on using the repository browser.