48 lines
810 B
C++
48 lines
810 B
C++
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
|
|
#ifndef __forceinline
|
|
#define __forceinline inline __attribute__((always_inline))
|
|
#endif
|
|
|
|
template <typename T>
|
|
class CSingleton
|
|
{
|
|
static T * ms_singleton;
|
|
|
|
public:
|
|
CSingleton()
|
|
{
|
|
assert(!ms_singleton);
|
|
intptr_t offset = (intptr_t) (T*) 1 - (intptr_t) (CSingleton <T>*) (T*) 1;
|
|
ms_singleton = (T*) ((intptr_t) this + offset);
|
|
}
|
|
|
|
virtual ~CSingleton()
|
|
{
|
|
assert(ms_singleton);
|
|
ms_singleton = nullptr;
|
|
}
|
|
|
|
__forceinline static T & Instance()
|
|
{
|
|
assert(ms_singleton);
|
|
return (*ms_singleton);
|
|
}
|
|
|
|
__forceinline static T * InstancePtr()
|
|
{
|
|
return (ms_singleton);
|
|
}
|
|
|
|
__forceinline static T & instance()
|
|
{
|
|
assert(ms_singleton);
|
|
return (*ms_singleton);
|
|
}
|
|
};
|
|
|
|
template <typename T> T * CSingleton<T>::ms_singleton = nullptr;
|