Skip to content

Instantly share code, notes, and snippets.

@H2NCH2COOH
Last active April 7, 2025 01:09
Show Gist options
  • Save H2NCH2COOH/93f1e8e3583e34023d77c8a385e4077b to your computer and use it in GitHub Desktop.
Save H2NCH2COOH/93f1e8e3583e34023d77c8a385e4077b to your computer and use it in GitHub Desktop.
Worst Singleton.h
#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