Slicer  5.1
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
vtkSingleton.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Library: CTK
4 
5  Copyright (c) 2010 Kitware Inc.
6 
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10 
11  https://www.commontk.org/LICENSE
12 
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18 
19 =========================================================================*/
20 
21 #ifndef __vtkSingleton_h
22 #define __vtkSingleton_h
23 
33 
34 //-----------------------------------------------------------------------------
37 #define VTK_SINGLETON_DECLARE(NAME) \
38 typedef NAME Self; \
39 static NAME* Instance; \
40 static void classInitialize(); \
41 static void classFinalize(); \
42 friend class NAME##Initialize;
43 
44 //-----------------------------------------------------------------------------
53 #define VTK_SINGLETON_DECLARE_INITIALIZER(EXPORT_DIRECTIVE,NAME) \
54 class EXPORT_DIRECTIVE NAME##Initialize \
55 { \
56 public: \
57  typedef NAME##Initialize Self; \
58  \
59  NAME##Initialize(); \
60  ~NAME##Initialize(); \
61 private: \
62  static unsigned int Count; \
63 }; \
64  \
65 static NAME##Initialize NAME##Initializer;
66 
67 
68 //-----------------------------------------------------------------------------
77 #define VTK_SINGLETON_INITIALIZER_CXX(NAME) \
78 NAME##Initialize::NAME##Initialize() \
79 { \
80  if(++Self::Count == 1) \
81  { NAME::classInitialize(); } \
82 } \
83  \
84 NAME##Initialize::~NAME##Initialize() \
85 { \
86  if(--Self::Count == 0) \
87  { NAME::classFinalize(); } \
88 } \
89  \
90 unsigned int NAME##Initialize::Count; \
91 NAME* NAME::Instance;
92 
93 // TODO Would be great to have this macro working. that would avoid
94 // to duplicate the following lines over and over
95 //#define VTK_SINGLETON_GETINSTANCE_CXX(NAME)
96 /* Up the reference count so it behaves like New */
97 //NAME* NAME::New()
98 //{
99 // NAME* instance = Self::GetInstance();
100 // instance->Register(0);
101 // return instance;
102 //}
103 //NAME* NAME::GetInstance()
104 //{
105 // if(!Self::Instance)
106 // {
107  /* Try the factory first */
108 // Self::Instance = (NAME*)vtkObjectFactory::CreateInstance(#NAME);
109 //
110  /* if the factory did not provide one, then create it here*/
111 // if(!Self::Instance)
112 // {
113  /* if the factory failed to create the object, */
114  /* then destroy it now, as vtkDebugLeaks::ConstructClass */
115  /* was called with "NAME", and not the real name of the class */
116 //#ifdef VTK_DEBUG_LEAKS
117 // vtkDebugLeaks::DestructClass(#NAME);
118 //#endif
119 // Self::Instance = new NAME;
120 // }
121 // }
122 // return Self::Instance;
123 //}
124 
125 //----------------------------------------------------------------------------
129 #define VTK_SINGLETON_CXX(NAME) \
130 void NAME::classInitialize() \
131 { \
132  Self::Instance = NAME::GetInstance(); \
133 } \
134  \
135 void NAME::classFinalize() \
136 { \
137  Self::Instance->Delete(); \
138  Self::Instance = 0; \
139 } \
140  \
141 VTK_SINGLETON_INITIALIZER_CXX(NAME)
142 
144 
145 #endif //__vtkSingleton_h