[2250] Fix wrong code in [2247]
[mangos-git.git] / src / shared / Singleton.h
blob7a4da0657dd6df5540380e136384a4d4232feeca
1 /* Singleton.h
3 * Copyright (C) 2004 Wow Daemon
4 * Copyright (C) 2005 MaNGOS <https://opensvn.csie.org/traccgi/MaNGOS/trac.cgi/>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef MANGOSSERVER_SINGLETON_H
22 #define MANGOSSERVER_SINGLETON_H
24 #include "Errors.h"
26 /// Should be placed in the appropriate .cpp file somewhere
27 #define initialiseSingleton( type ) \
28 template <> type * Singleton < type > :: mSingleton = 0
30 /// To be used as a replacement for initialiseSingleton( )
31 /// Creates a file-scoped Singleton object, to be retrieved with getSingleton
32 #define createFileSingleton( type ) \
33 initialiseSingleton( type ); \
34 type the##type
36 template < class type > class Singleton
38 public:
39 /// Constructor
40 Singleton( )
42 WPAssert( mSingleton == 0 );
43 mSingleton = static_cast<type *>(this);
45 /// Destructor
46 ~Singleton( )
48 mSingleton = 0;
51 /// Retrieve the singleton object, if you hit this assert this singleton object doesn't exist yet
52 static type & getSingleton( ) { WPAssert( mSingleton ); return *mSingleton; }
54 /// Retrieve a pointer to the singleton object
55 static type * getSingletonPtr( ) { return mSingleton; }
57 protected:
58 /// Singleton pointer, must be set to 0 prior to creating the object
59 static type * mSingleton;
61 #endif