ITK  5.4.0
Insight Toolkit
itkSingletonMacro.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
25 #ifndef itkSingletonMacro_h
26 #define itkSingletonMacro_h
27 
28 #define itkInitGlobalsMacro(VarName) \
29  { \
30  static auto * staticGlobals = Get##VarName##Pointer(); \
31  (void)staticGlobals; \
32  } \
33  ITK_MACROEND_NOOP_STATEMENT
34 
35 #define itkGetGlobalDeclarationMacro(Type, VarName) static Type * Get##VarName##Pointer()
36 
37 #define itkGetGlobalSimpleMacro(Class, Type, Name) itkGetGlobalInitializeMacro(Class, Type, Name, Class, (void)0)
38 
39 #define itkGetGlobalValueMacro(Class, Type, Name, Value) \
40  itkGetGlobalInitializeMacro(Class, Type, Name, Name, *m_##Name = Value)
41 
42 #define itkGetGlobalInitializeMacro(Class, Type, VarName, SingletonName, Init) \
43  Type * Class::Get##VarName##Pointer() \
44  { \
45  if (m_##VarName == nullptr) \
46  { \
47  const auto deleteLambda = []() { \
48  delete m_##VarName; \
49  m_##VarName = nullptr; \
50  }; \
51  auto * old_instance = SingletonIndex::GetInstance()->GetGlobalInstance<Type>(#SingletonName); \
52  m_##VarName = Singleton<Type>(#SingletonName, deleteLambda); \
53  if (old_instance == nullptr) \
54  { \
55  Init; \
56  } \
57  } \
58  return m_##VarName; \
59  } \
60  ITK_MACROEND_NOOP_STATEMENT
61 
62 #endif