#define DECLARE_SINGLETON(classname) \
public: \
static classname *Instance(bool create_if_needed = true) { \
classname **instance = GetInstancePtrAddr(); \
if (!*instance && create_if_needed) { \
static std::once_flag flag; \
std::call_once(flag, \
[&] { *instance = new (std::nothrow) classname(); \
std::atexit(CleanUp);}); \
} \
return *instance; \
} \
\
static void CleanUp() { \
classname **instance = GetInstancePtrAddr(); \
if (*instance != nullptr) { \
LOGD("CleanUp singleton %s\n", #classname); \
CallShutdown(*instance); \
delete *instance; \
*instance = nullptr; \
} \
} \
\
private: \
static classname **GetInstancePtrAddr() { \
static classname *instance = nullptr; \
return &instance; \
} \
classname(); \
DISALLOW_COPY_AND_ASSIGN(classname)
|