ITK  6.0.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  [[maybe_unused]] static auto * staticGlobals = Get##VarName##Pointer(); \
31  } \
32  ITK_MACROEND_NOOP_STATEMENT
33 
34 #define itkGetGlobalDeclarationMacro(Type, VarName) static Type * Get##VarName##Pointer()
35 
36 #define itkGetGlobalSimpleMacro(Class, Type, Name) itkGetGlobalInitializeMacro(Class, Type, Name, Class, (void)0)
37 
38 #define itkGetGlobalValueMacro(Class, Type, Name, Value) \
39  itkGetGlobalInitializeMacro(Class, Type, Name, Name, *m_##Name = Value)
40 
41 #define itkGetGlobalInitializeMacro(Class, Type, VarName, SingletonName, Init) \
42  Type * Class::Get##VarName##Pointer() \
43  { \
44  if (m_##VarName == nullptr) \
45  { \
46  const auto deleteLambda = []() { \
47  delete m_##VarName; \
48  m_##VarName = nullptr; \
49  }; \
50  auto * old_instance = SingletonIndex::GetInstance()->GetGlobalInstance<Type>(#SingletonName); \
51  m_##VarName = Singleton<Type>(#SingletonName, deleteLambda); \
52  if (old_instance == nullptr) \
53  { \
54  Init; \
55  } \
56  } \
57  return m_##VarName; \
58  } \
59  ITK_MACROEND_NOOP_STATEMENT
60 
61 #endif