Crazy Eddie's GUI System  0.8.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Singleton.h
1 /***********************************************************************
2  created: 22/2/2004
3  author: Paul D Turner
4 
5  purpose: Singleton Base Class
6 *************************************************************************/
7 /***************************************************************************
8  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  ***************************************************************************/
29 /*************************************************************************
30 
31  The code in this file is taken from article 1.3 in the the book:
32  Game Programming Gems from Charles River Media
33 
34 *************************************************************************/
35 #ifndef _CEGUISingleton_h_
36 #define _CEGUISingleton_h_
37 
38 #include "CEGUI/Base.h"
39 #include <cassert>
40 
41 // Start of CEGUI namespace section
42 namespace CEGUI
43 {
44 /* Copyright (C) Scott Bilas, 2000.
45  * All rights reserved worldwide.
46  *
47  * This software is provided "as is" without express or implied
48  * warranties. You may freely copy and compile this source into
49  * applications you distribute provided that the copyright text
50  * below is included in the resulting source code, for example:
51  * "Portions Copyright (C) Scott Bilas, 2000"
52  */
53 
54 
55 template <typename T> class CEGUIEXPORT Singleton
56 {
57 protected:
58 // TODO: Come up with something better than this!
59 // TODO:
60 // TODO: This super-nasty piece of nastiness was put in for continued
61 // TODO: compatability with MSVC++ and MinGW - the latter apparently
62 // TODO: needs this.
63  static
64 #ifdef __MINGW32__
65  CEGUIEXPORT
66 #endif
67  T* ms_Singleton;
68 
69 public:
70  Singleton( void )
71  {
72  assert( !ms_Singleton );
73  ms_Singleton = static_cast<T*>(this);
74  }
75  ~Singleton( void )
76  { assert( ms_Singleton ); ms_Singleton = 0; }
77  static T& getSingleton( void )
78  { assert( ms_Singleton ); return ( *ms_Singleton ); }
79  static T* getSingletonPtr( void )
80  { return ( ms_Singleton ); }
81 
82 private:
83  Singleton& operator=(const Singleton&) { return this; }
84  Singleton(const Singleton&) {}
85 };
86 
87 } // End of CEGUI namespace section
88 
89 
90 
91 #endif // end of guard _CEGUISingleton_h_