/src/ogre/OgreMain/include/OgreSingleton.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | /* Original version Copyright (C) Scott Bilas, 2000. |
29 | | * All rights reserved worldwide. |
30 | | * |
31 | | * This software is provided "as is" without express or implied |
32 | | * warranties. You may freely copy and compile this source into |
33 | | * applications you distribute provided that the copyright text |
34 | | * below is included in the resulting source code, for example: |
35 | | * "Portions Copyright (C) Scott Bilas, 2000" |
36 | | */ |
37 | | #ifndef _SINGLETON_H__ |
38 | | #define _SINGLETON_H__ |
39 | | |
40 | | // Added by Steve Streeting for Ogre |
41 | | #include "OgrePrerequisites.h" |
42 | | #include "OgreException.h" |
43 | | |
44 | | #if OGRE_COMPILER == OGRE_COMPILER_MSVC |
45 | | # pragma warning (push) |
46 | | # pragma warning ( disable: 4661) |
47 | | #endif |
48 | | |
49 | | namespace Ogre { |
50 | | /** \addtogroup Core |
51 | | * @{ |
52 | | */ |
53 | | /** \addtogroup General |
54 | | * @{ |
55 | | */ |
56 | | |
57 | | // End SJS additions |
58 | | /** Template class for creating single-instance global classes. |
59 | | * |
60 | | * This implementation @cite bilas2000automatic slightly derives from the textbook pattern, by requiring |
61 | | * manual instantiation, instead of implicitly doing it in #getSingleton. This is useful for classes that |
62 | | * want to do some involved initialization, which should be done at a well defined time-point or need some |
63 | | * additional parameters in their constructor. |
64 | | * |
65 | | * It also allows you to manage the singleton lifetime through RAII. |
66 | | * |
67 | | * @note Be aware that #getSingleton will fail before the global instance is created. (check via |
68 | | * #getSingletonPtr) |
69 | | */ |
70 | | template <typename T> class Singleton |
71 | | { |
72 | | private: |
73 | | /** @brief Explicit private copy constructor. This is a forbidden operation.*/ |
74 | | Singleton(const Singleton<T>&); |
75 | | |
76 | | /** @brief Private operator= . This is a forbidden operation. */ |
77 | | Singleton& operator=(const Singleton<T>&); |
78 | | |
79 | | protected: |
80 | | static T* msSingleton; |
81 | | |
82 | | public: |
83 | | #if defined(__has_attribute) |
84 | | # if __has_attribute(no_sanitize) |
85 | | // The `static_cast` happens so early in the construction of the inheriting |
86 | | // classes that the `this` pointer is still detected as the super class |
87 | | // pointer. Therefore, disabling vptr checks. |
88 | | __attribute__((no_sanitize("vptr"))) |
89 | | # endif |
90 | | #endif |
91 | | Singleton(void) |
92 | 0 | { |
93 | 0 | OgreAssert(!msSingleton, "There can be only one singleton"); |
94 | 0 | msSingleton = static_cast<T*>(this); |
95 | 0 | } Unexecuted instantiation: Ogre::Singleton<Ogre::TerrainGlobalOptions>::Singleton() Unexecuted instantiation: Ogre::Singleton<Ogre::Quake3ShaderManager>::Singleton() |
96 | | ~Singleton(void) |
97 | 0 | { |
98 | 0 | assert(msSingleton); |
99 | 0 | msSingleton = 0; |
100 | 0 | } Unexecuted instantiation: Ogre::Singleton<Ogre::TerrainGlobalOptions>::~Singleton() Unexecuted instantiation: Ogre::Singleton<Ogre::Quake3ShaderManager>::~Singleton() |
101 | | /// Get the singleton instance |
102 | | static T& getSingleton(void) |
103 | | { |
104 | | assert(msSingleton); |
105 | | return (*msSingleton); |
106 | | } |
107 | | /// @copydoc getSingleton |
108 | | static T* getSingletonPtr(void) { return msSingleton; } |
109 | | }; |
110 | | /** @} */ |
111 | | /** @} */ |
112 | | |
113 | | } |
114 | | #if OGRE_COMPILER == OGRE_COMPILER_MSVC |
115 | | # pragma warning (pop) |
116 | | #endif |
117 | | |
118 | | #endif |