source: GTP/trunk/App/Demos/Geom/OgreStuff/include/OgreSingleton.h @ 1812

Revision 1812, 2.6 KB checked in by gumbau, 18 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25/* Original version Copyright (C) Scott Bilas, 2000.
26 * All rights reserved worldwide.
27 *
28 * This software is provided "as is" without express or implied
29 * warranties. You may freely copy and compile this source into
30 * applications you distribute provided that the copyright text
31 * below is included in the resulting source code, for example:
32 * "Portions Copyright (C) Scott Bilas, 2000"
33 */
34#ifndef _SINGLETON_H__
35#define _SINGLETON_H__
36
37// Added by Steve Streeting for Ogre
38#include "OgrePrerequisites.h"
39
40#if OGRE_COMPILER == OGRE_COMPILER_MSVC
41// Turn off warnings generated by this singleton implementation
42#   pragma warning (disable : 4311)
43#   pragma warning (disable : 4312)
44#endif
45
46namespace Ogre {
47// End SJS additions
48    /** Template class for creating single-instance global classes.
49    */
50    template <typename T> class Singleton
51    {
52    protected:
53
54        static T* ms_Singleton;
55
56    public:
57        Singleton( void )
58        {
59            assert( !ms_Singleton );
60#if defined( _MSC_VER ) && _MSC_VER < 1200       
61            int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
62            ms_Singleton = (T*)((int)this + offset);
63#else
64            ms_Singleton = static_cast< T* >( this );
65#endif
66        }
67        ~Singleton( void )
68            {  assert( ms_Singleton );  ms_Singleton = 0;  }
69        static T& getSingleton( void )
70                {       assert( ms_Singleton );  return ( *ms_Singleton ); }
71        static T* getSingletonPtr( void )
72                { return ms_Singleton; }
73    };
74}
75#endif
Note: See TracBrowser for help on using the repository browser.