OpenLexocad  27.1
base_singleton.h
Go to the documentation of this file.
1 
10 #ifndef SINGLETON__H
11 #define SINGLETON__H
12 
13 
14 //----------------------------------------------------------------------------
15 // DECLARATIONs
16 //----------------------------------------------------------------------------
17 
28 #define DECLARE_SINGLETON(CLASSNAME) \
29 \
30 public: \
31  static CLASSNAME* instance(); \
32  static void destroy(); \
33 \
34 protected: \
35  CLASSNAME(); \
36  ~CLASSNAME(); \
37 \
38 private: \
39  static CLASSNAME* _instance;
40 
41 
42 
57 #define DEFINE_SINGLETON(CLASSNAME) \
58  CLASSNAME* CLASSNAME::_instance = 0; \
59 \
60  CLASSNAME* CLASSNAME::instance() \
61  { \
62  if (!_instance) \
63  { \
64  _instance = new CLASSNAME; \
65  } \
66  return _instance; \
67  } \
68 \
69  void CLASSNAME::destroy() \
70  { \
71  delete _instance; \
72  _instance = NULL; \
73  }
74 
75 #endif