ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkSingletonMacro.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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) \
35 static Type * Get##VarName##Pointer();
36 
37 #define itkGetGlobalSimpleMacro(Class, Type, Name) \
38  itkGetGlobalInitializeMacro(Class, Type, Name, Class, (void)0)
39 
40 #define itkGetGlobalValueMacro(Class, Type, Name, Value) \
41  itkGetGlobalInitializeMacro(Class, Type, Name, Name, *m_##Name = Value)
42 
43 #define itkGetGlobalInitializeMacro(Class, Type, VarName, SingletonName, Init) \
44 Type * Class::Get##VarName##Pointer() \
45 { \
46  if( m_##VarName == nullptr ) \
47  { \
48  static auto setLambda = [](void * a) \
49  { \
50  delete m_##VarName; \
51  m_##VarName = static_cast<Type*>(a); \
52  }; \
53  static auto deleteLambda = []() \
54  { \
55  delete m_##VarName; \
56  m_##VarName = nullptr; \
57  }; \
58  auto* old_instance = SingletonIndex::GetInstance()->GetGlobalInstance<Type>(#SingletonName); \
59  m_##VarName = Singleton<Type>( #SingletonName , setLambda, deleteLambda); \
60  if( old_instance == nullptr) \
61  { \
62  Init; \
63  }\
64  } \
65  return m_##VarName; \
66 }
67 
68 #endif