-
-
Notifications
You must be signed in to change notification settings - Fork 657
/
itkSingletonMacro.h
66 lines (59 loc) · 3.94 KB
/
itkSingletonMacro.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
/**
* itkSingletonMacro.h defines macros that are used to declare and define
* global variables across ITK with a global map that is used to synchronize
* this variables across multiple instantiations of ITK if necessary.
* Note: this is rarely necessary.
*/
#ifndef itkSingletonMacro_h
#define itkSingletonMacro_h
#define itkInitGlobalsMacro(VarName) \
{ \
static auto * staticGlobals = Get##VarName##Pointer(); \
(void)staticGlobals; \
} \
ITK_MACROEND_NOOP_STATEMENT
#define itkGetGlobalDeclarationMacro(Type, VarName) static Type * Get##VarName##Pointer()
#define itkGetGlobalSimpleMacro(Class, Type, Name) itkGetGlobalInitializeMacro(Class, Type, Name, Class, (void)0)
#define itkGetGlobalValueMacro(Class, Type, Name, Value) \
itkGetGlobalInitializeMacro(Class, Type, Name, Name, *m_##Name = Value)
#define itkGetGlobalInitializeMacro(Class, Type, VarName, SingletonName, Init) \
Type * Class::Get##VarName##Pointer() \
{ \
if (m_##VarName == nullptr) \
{ \
const auto setLambda = [](void * a) { \
delete m_##VarName; \
m_##VarName = static_cast<Type *>(a); \
}; \
const auto deleteLambda = []() { \
delete m_##VarName; \
m_##VarName = nullptr; \
}; \
auto * old_instance = SingletonIndex::GetInstance()->GetGlobalInstance<Type>(#SingletonName); \
m_##VarName = Singleton<Type>(#SingletonName, setLambda, deleteLambda); \
if (old_instance == nullptr) \
{ \
Init; \
} \
} \
return m_##VarName; \
} \
ITK_MACROEND_NOOP_STATEMENT
#endif