49 lines
923 B
C++
49 lines
923 B
C++
#pragma once
|
|
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
|
|
#ifndef __forceinline
|
|
#define __forceinline inline __attribute__((always_inline))
|
|
#endif
|
|
|
|
template <typename T>
|
|
class CSingleton
|
|
{
|
|
static CSingleton<T> * ms_singleton;
|
|
|
|
public:
|
|
CSingleton()
|
|
{
|
|
assert(!ms_singleton);
|
|
// Register the actual base subobject. Convert to T only after its
|
|
// construction starts, using the compiler's multiple-inheritance adjustment.
|
|
ms_singleton = this;
|
|
}
|
|
|
|
virtual ~CSingleton()
|
|
{
|
|
assert(ms_singleton);
|
|
ms_singleton = nullptr;
|
|
}
|
|
|
|
__forceinline static T & Instance()
|
|
{
|
|
assert(ms_singleton);
|
|
return *static_cast<T*>(ms_singleton);
|
|
}
|
|
|
|
__forceinline static T * InstancePtr()
|
|
{
|
|
return static_cast<T*>(ms_singleton);
|
|
}
|
|
|
|
__forceinline static T & instance()
|
|
{
|
|
assert(ms_singleton);
|
|
return *static_cast<T*>(ms_singleton);
|
|
}
|
|
};
|
|
|
|
template <typename T> CSingleton<T> * CSingleton<T>::ms_singleton = nullptr;
|