ITK  5.3.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  * http://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  static auto setLambda = [](void * a) { \
48  delete m_##VarName; \
49  m_##VarName = static_cast<Type *>(a); \
50  }; \
51  static auto deleteLambda = []() { \
52  delete m_##VarName; \
53  m_##VarName = nullptr; \
54  }; \
55  auto * old_instance = SingletonIndex::GetInstance()->GetGlobalInstance<Type>(#SingletonName); \
56  m_##VarName = Singleton<Type>(#SingletonName, setLambda, deleteLambda); \
57  if (old_instance == nullptr) \
58  { \
59  Init; \
60  } \
61  } \
62  return m_##VarName; \
63  } \
64  ITK_MACROEND_NOOP_STATEMENT
65 
66 #endif