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