OgreSingleton.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. /* Original version Copyright (C) Scott Bilas, 2000.
  25. * All rights reserved worldwide.
  26. *
  27. * This software is provided "as is" without express or implied
  28. * warranties. You may freely copy and compile this source into
  29. * applications you distribute provided that the copyright text
  30. * below is included in the resulting source code, for example:
  31. * "Portions Copyright (C) Scott Bilas, 2000"
  32. */
  33. #ifndef _SINGLETON_H__
  34. #define _SINGLETON_H__
  35. // Added by Steve Streeting for Ogre
  36. #include "OgrePrerequisites.h"
  37. #include "OgreHeaderPrefix.h"
  38. #if OGRE_COMPILER == OGRE_COMPILER_MSVC
  39. // Turn off warnings generated by this singleton implementation
  40. # pragma warning (disable : 4311)
  41. # pragma warning (disable : 4312)
  42. #endif
  43. #if defined ( OGRE_GCC_VISIBILITY )
  44. # pragma GCC visibility push(default)
  45. #endif
  46. namespace Ogre {
  47. /** \addtogroup Core
  48. * @{
  49. */
  50. /** \addtogroup General
  51. * @{
  52. */
  53. // End SJS additions
  54. /** Template class for creating single-instance global classes.
  55. */
  56. template <typename T> class Singleton
  57. {
  58. private:
  59. /** \brief Explicit private copy constructor. This is a forbidden operation.*/
  60. Singleton(const Singleton<T> &);
  61. /** \brief Private operator= . This is a forbidden operation. */
  62. Singleton& operator=(const Singleton<T> &);
  63. protected:
  64. static T* ms_Singleton;
  65. public:
  66. Singleton( void )
  67. {
  68. assert( !ms_Singleton );
  69. #if defined( _MSC_VER ) && _MSC_VER < 1200
  70. int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
  71. ms_Singleton = (T*)((int)this + offset);
  72. #else
  73. ms_Singleton = static_cast< T* >( this );
  74. #endif
  75. }
  76. ~Singleton( void )
  77. { assert( ms_Singleton ); ms_Singleton = 0; }
  78. static T& getSingleton( void )
  79. { assert( ms_Singleton ); return ( *ms_Singleton ); }
  80. static T* getSingletonPtr( void )
  81. { return ms_Singleton; }
  82. };
  83. /** @} */
  84. /** @} */
  85. }
  86. #if defined ( OGRE_GCC_VISIBILITY )
  87. # pragma GCC visibility pop
  88. #endif
  89. #include "OgreHeaderSuffix.h"
  90. #endif