Slicer  5.1
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
Macros
Singleton definition and declaration helpers

Macros

#define VTK_SINGLETON_CXX(NAME)
 
#define VTK_SINGLETON_DECLARE(NAME)
 
#define VTK_SINGLETON_DECLARE_INITIALIZER(EXPORT_DIRECTIVE, NAME)
 
#define VTK_SINGLETON_INITIALIZER_CXX(NAME)
 

Detailed Description

See https://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12 and https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter

Inspired from VTK/Utilities/kwsys/SystemTools class

Macro Definition Documentation

◆ VTK_SINGLETON_CXX

#define VTK_SINGLETON_CXX (   NAME)
Value:
void NAME::classInitialize() \
{ \
Self::Instance = NAME::GetInstance(); \
} \
\
void NAME::classFinalize() \
{ \
Self::Instance->Delete(); \
Self::Instance = 0; \
} \
#define VTK_SINGLETON_INITIALIZER_CXX(NAME)
Definition: vtkSingleton.h:77

This should be added at the end of the CXX file

Definition at line 129 of file vtkSingleton.h.

◆ VTK_SINGLETON_DECLARE

#define VTK_SINGLETON_DECLARE (   NAME)
Value:
typedef NAME Self; \
static NAME* Instance; \
static void classInitialize(); \
static void classFinalize(); \
friend class NAME##Initialize;

Should be included as a class protected member

Definition at line 37 of file vtkSingleton.h.

◆ VTK_SINGLETON_DECLARE_INITIALIZER

#define VTK_SINGLETON_DECLARE_INITIALIZER (   EXPORT_DIRECTIVE,
  NAME 
)
Value:
class EXPORT_DIRECTIVE NAME##Initialize \
{ \
public: \
typedef NAME##Initialize Self; \
\
NAME##Initialize(); \
~NAME##Initialize(); \
private: \
static unsigned int Count; \
}; \
\
static NAME##Initialize NAME##Initializer;

Help macro allowing to declare the utility class to make sure NAME is initialized before it is used.

Should be added at the bottom of the header file, after the class declaration

The instance (NAME##Initializer) will show up in any translation unit that uses NAME. It will make sure NAME is initialized before it is used.

Definition at line 53 of file vtkSingleton.h.

◆ VTK_SINGLETON_INITIALIZER_CXX

#define VTK_SINGLETON_INITIALIZER_CXX (   NAME)
Value:
NAME##Initialize::NAME##Initialize() \
{ \
if(++Self::Count == 1) \
{ NAME::classInitialize(); } \
} \
\
NAME##Initialize::~NAME##Initialize() \
{ \
if(--Self::Count == 0) \
{ NAME::classFinalize(); } \
} \
\
unsigned int NAME##Initialize::Count; \
NAME* NAME::Instance;

Implementation of NAME##Initialize class.

Macro used by VTK_SINGLETON_DEFINE. See below.

Note
NAME##Initialize::Count and NAME::Instance Must NOT be initialized. Default initialization to zero is necessary.

Definition at line 77 of file vtkSingleton.h.