OpenLexocad  27.1
base_singleton.h File Reference

Go to the source code of this file.

Macros

#define DECLARE_SINGLETON(CLASSNAME)
 
#define DEFINE_SINGLETON(CLASSNAME)
 

Detailed Description

Singleton template macro definition. Use this macros to define singleton classes.

Author
Tomáš "Shamot" Burian

Macro Definition Documentation

◆ DECLARE_SINGLETON

#define DECLARE_SINGLETON (   CLASSNAME)
Value:
\
public: \
static CLASSNAME* instance(); \
static void destroy(); \
\
protected: \
CLASSNAME(); \
~CLASSNAME(); \
\
private: \
static CLASSNAME* _instance;

Singleton class declaration. Must be in a header file. Example:

class ClassX { DECLARE_SINGLETON(ClassX); };

◆ DEFINE_SINGLETON

#define DEFINE_SINGLETON (   CLASSNAME)
Value:
CLASSNAME* CLASSNAME::_instance = 0; \
\
CLASSNAME* CLASSNAME::instance() \
{ \
if (!_instance) \
{ \
_instance = new CLASSNAME; \
} \
return _instance; \
} \
\
void CLASSNAME::destroy() \
{ \
delete _instance; \
_instance = NULL; \
}

Singleton class definition. Must be in a cpp file. Example:

DEFINE_SINGLETON(ClassX);

Continue to define class constructor and destructor:

ClassX::ClassX() {};

ClassX::~ClassX() {};