Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/Binderm_FS16/src/libraries/util/Singleton.h @ 12412

Last change on this file since 12412 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: 4.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    @defgroup SingletonScope Singletons and Scope
31    @ingroup Util
32*/
33
34/**
35    @file
36    @ingroup SingletonScope
37    @brief Definition of the Singleton template that is used as base class for classes that allow only one instance.
38
39    @anchor SingletonExample
40
41    Classes that inherit from orxonox::Singleton follow the singleton pattern and thus
42    allow only one instance of the class to exist. This istance is stored in a static
43    variable called @c singletonPtr_s. orxonox::Singleton will access this variable, but
44    it must be implemented in the deriving class.
45
46    Example:
47    @code
48    class TestSingleton : public Singleton<TestSingleton>   // inherit from Singleton, pass the own class as template argument
49    {
50        friend class Singleton<TestSingleton>;              // friend declaration so Singleton can access singletonPtr_s
51
52        public:
53            TestSingleton();                                // public constructor because we may want to manage this singleton
54                                                            //     with an orxonox::ScopedSingletonWrapper
55            virtual ~TestSingleton();                       // public destructor
56
57            void testFunction();                            // put your functions here
58
59        private:
60            int testValue_;                                 // put your variables here
61
62            static TestSingleton* singletonPtr_s;           // static singleton instance pointer, used by the Singleton template
63    };
64    @endcode
65
66    And don't forget to initialize the static singleton pointer in the source (*.cc) %file:
67    @code
68    TestSingleton* TestSingleton::singletonPtr_s = nullptr;
69    @endcode
70
71    If a class inherits from orxonox::Singleton, it also inherits its functions. The most important
72    function is orxonox::Singleton::getInstance() which returns a reference to the only instance
73    of the singleton.
74
75    Example:
76    @code
77    TestSingleton::TestSingleton()                          // implement the constructor
78    {
79        this->testValue_ = 15;
80    }
81
82    void TestSingleton::testFunction()                      // implement testFunction
83    {
84        orxout() << "My value is " << this->testValue_ << endl;
85    }
86
87    TestSingleton::getInstance().testFunction();            // prints "My value is 15"
88    @endcode
89*/
90
91#ifndef __Util_Singleton_H__
92#define __Util_Singleton_H__
93
94#include "UtilPrereqs.h"
95
96#include <cstring>
97#include <typeinfo>
98
99#include "OrxAssert.h"
100
101namespace orxonox
102{
103    /**
104    @brief
105        Base for singleton classes.
106
107        Usage:
108        Inherit publicly from Singleton<MyClass> and provide access to MyClass::singletonPtr_s.
109        This can easily be done with a friend declaration.
110
111        See @ref SingletonExample "this example" for an exemplary implementation.
112    */
113    template <class T>
114    class Singleton
115    {
116    public:
117        //! Returns a reference to the singleton instance
118        static T& getInstance()
119        {
120            OrxVerify(T::singletonPtr_s != nullptr, "T=" << typeid(T).name());
121            return *T::singletonPtr_s;
122        }
123
124        //! Tells whether the singleton has been created
125        static bool exists()
126        {
127            return (T::singletonPtr_s != nullptr);
128        }
129
130    protected:
131        //! Constructor sets the singleton instance pointer
132        Singleton()
133        {
134            OrxVerify(T::singletonPtr_s == nullptr, "T=" << typeid(T).name());
135            T::singletonPtr_s = static_cast<T*>(this);
136        }
137
138        //! Destructor resets the singleton instance pointer
139        virtual ~Singleton()
140        {
141            OrxVerify(T::singletonPtr_s != nullptr, "T=" << typeid(T).name());
142            T::singletonPtr_s = nullptr;
143        }
144
145    private:
146        // non-copyable:
147        Singleton(const Singleton&) = delete;
148        Singleton& operator=(const Singleton&) = delete;
149    };
150}
151
152#endif /* __Util_Singleton_H__ */
Note: See TracBrowser for help on using the repository browser.