Last active
April 7, 2025 01:09
-
-
Save H2NCH2COOH/93f1e8e3583e34023d77c8a385e4077b to your computer and use it in GitHub Desktop.
Worst Singleton.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _SINGLETON_H__ | |
#define _SINGLETON_H__ | |
#include <assert.h> | |
template <typename T> class Singleton | |
{ | |
protected: | |
static T* ms_Singleton; | |
public: | |
Singleton( void ) | |
{ | |
assert( !ms_Singleton ); | |
#if defined( _MSC_VER ) && _MSC_VER < 1200 | |
int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1; | |
ms_Singleton = (T*)((int)this + offset); | |
#else | |
ms_Singleton = static_cast< T* >( this ); | |
#endif | |
} | |
~Singleton( void ) | |
{ assert( ms_Singleton ); ms_Singleton = 0; } | |
static T& getSingleton( void ) | |
{ assert( ms_Singleton ); return ( *ms_Singleton ); } | |
static T* getSingletonPtr( void ) | |
{ return ms_Singleton; } | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment