Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/singleton/ScopedSingletonIncludes.h @ 11863

Last change on this file since 11863 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: 3.9 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 the ManageScopedSingleton macro.
33*/
34
35#ifndef __ScopedSingletonIncludes_H__
36#define __ScopedSingletonIncludes_H__
37
38#include "core/CorePrereqs.h"
39
40#include "ScopedSingletonWrapper.h"
41#include "core/module/StaticallyInitializedInstance.h"
42
43/**
44    @brief Creates an orxonox::ScopedSingletonWrapper for an orxonox::Singleton and registers it with orxonox::ScopeManager.
45    @param className The name of the singleton class
46    @param scope The scope in which the singleton should exist
47    @param allowedToFail If true, the singleton is allowed to fail and thus a try-catch block is used when creating the singleton.
48
49    If this macro is called for a singleton, it is wrapped in a ScopedSingletonWrapper and registered with ScopeManager
50    and will thus be created if its scope becomes active and destroyed if is deactivated.
51
52
53    Usually a singleton gets created automatically when it is first used, but it will never
54    be destroyed (unless the singleton explicitly deletes itself). To allow controlled
55    construction and destruction, the singleton can be put within a virtual scope. This is
56    done by registering the singleton class with orxonox::ScopeManager. To
57    do so, the ManageScopedSingleton() macro has to be called:
58
59    @code
60    ManageScopedSingleton(TestSingleton, ScopeID::Graphics, false); // muste be called in a source (*.cc) file
61    @endcode
62
63    @b Important: If you call ManageScopedSingleton(), you don't have to initialize singletonPtr_s anymore,
64    because that's already done by the macro.
65
66    Now the singleton TestSingleton gets automatically created if the scope Graphics becomes
67    active and also gets destroyed if the scope is deactivated.
68
69    Note that not all singletons must register with a scope, but it's recommended.
70
71*/
72#define ManageScopedSingleton(className, scope, allowedToFail) \
73    className* className::singletonPtr_s = nullptr; \
74    static ScopedSingletonWrapper& className##ScopedSingletonWrapper \
75        = (new orxonox::SI_SSW(new ClassScopedSingletonWrapper<className, allowedToFail>(#className), scope))->getWrapper()
76
77namespace orxonox
78{
79    class _CoreExport StaticallyInitializedScopedSingletonWrapper : public StaticallyInitializedInstance
80    {
81        public:
82            StaticallyInitializedScopedSingletonWrapper(ScopedSingletonWrapper* wrapper, ScopeID::Value scope)
83                : StaticallyInitializedInstance(StaticInitialization::SCOPED_SINGLETON_WRAPPER)
84                , wrapper_(wrapper)
85                , scope_(scope)
86            {}
87            ~StaticallyInitializedScopedSingletonWrapper() { delete wrapper_; }
88
89            virtual void load() override;
90            virtual void unload() override;
91
92            inline ScopedSingletonWrapper& getWrapper()
93                { return *this->wrapper_; }
94
95        private:
96            ScopedSingletonWrapper* wrapper_;
97            ScopeID::Value scope_;
98    };
99
100    typedef StaticallyInitializedScopedSingletonWrapper SI_SSW;
101}
102
103#endif /* __ScopedSingletonIncludes_H__ */
Note: See TracBrowser for help on using the repository browser.