Skip to content

Commit 8598841

Browse files
N-Dekkerdzenanz
authored andcommitted
STYLE: Remove unused parameter from SetGlobalInstance and Singleton
Added overloads of `SetGlobalInstance` and `Singleton` without the unused `func` parameter. Let the new `SetGlobalInstance` overload just return `void`, instead of `bool`. Deprecated the original overloads, and made them `ITK_FUTURE_LEGACY_REMOVE`. Follow-up to: pull request #4164 commit 6d1c4c7 "STYLE: SingletonIndex does not need to store the unused `func` parameter" pull request #4162 commit 6ec6328 "STYLE: Let `Singleton` assume that SetGlobalInstance always returns true"
1 parent c1e15a3 commit 8598841

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

Modules/Core/Common/include/itkSingleton.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,29 @@ class ITKCommon_EXPORT SingletonIndex
6464
}
6565

6666

67-
// Returns true.
68-
//
6967
// It is assumed that the global will remain valid until the start
7068
// of globals being destroyed.
7169
template <typename T>
72-
bool
70+
void
71+
SetGlobalInstance(const char * globalName, T * global, std::function<void()> deleteFunc)
72+
{
73+
this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
74+
}
75+
76+
#ifndef ITK_FUTURE_LEGACY_REMOVE
77+
template <typename T>
78+
[[deprecated("Prefer calling the SetGlobalInstance(globalName, global, deleteFunc) overload (without the unused func "
79+
"parameter)!")]] bool
7380
SetGlobalInstance(const char * globalName,
7481
T * global,
7582
std::function<void(void *)> itkNotUsed(func),
7683
std::function<void()> deleteFunc)
7784
{
78-
this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
85+
this->SetGlobalInstance(globalName, global, deleteFunc);
86+
// Just returns true for backward compatibility (legacy only).
7987
return true;
8088
}
89+
#endif
8190

8291
/** Set/Get the pointer to GlobalSingleton.
8392
* Note that SetGlobalSingleton is not concurrent thread safe. */
@@ -114,18 +123,29 @@ class ITKCommon_EXPORT SingletonIndex
114123
// A wrapper for a global variable registered in the singleton index.
115124
template <typename T>
116125
T *
117-
Singleton(const char * globalName, std::function<void(void *)> itkNotUsed(func), std::function<void()> deleteFunc)
126+
Singleton(const char * globalName, std::function<void()> deleteFunc)
118127
{
119128
static SingletonIndex * singletonIndex = SingletonIndex::GetInstance();
120129
Unused(singletonIndex);
121130
T * instance = SingletonIndex::GetInstance()->GetGlobalInstance<T>(globalName);
122131
if (instance == nullptr)
123132
{
124133
instance = new T;
125-
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, {}, deleteFunc);
134+
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, deleteFunc);
126135
}
127136
return instance;
128137
}
138+
139+
140+
#ifndef ITK_FUTURE_LEGACY_REMOVE
141+
template <typename T>
142+
[[deprecated("Prefer calling the Singleton(globalName, deleteFunc) overload (without the unused func parameter)!")]] T *
143+
Singleton(const char * globalName, std::function<void(void *)> itkNotUsed(func), std::function<void()> deleteFunc)
144+
{
145+
return Singleton<T>(globalName, deleteFunc);
146+
}
147+
#endif
148+
129149
} // end namespace itk
130150

131151
#endif

Modules/Core/Common/include/itkSingletonMacro.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
m_##VarName = nullptr; \
5050
}; \
5151
auto * old_instance = SingletonIndex::GetInstance()->GetGlobalInstance<Type>(#SingletonName); \
52-
m_##VarName = Singleton<Type>(#SingletonName, {}, deleteLambda); \
52+
m_##VarName = Singleton<Type>(#SingletonName, deleteLambda); \
5353
if (old_instance == nullptr) \
5454
{ \
5555
Init; \

Modules/Core/Common/src/itkObjectFactoryBase.cxx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ auto
130130
ObjectFactoryBase::GetPimplGlobalsPointer() -> ObjectFactoryBasePrivate *
131131
{
132132
const auto deleteLambda = []() { m_PimplGlobals->UnRegister(); };
133-
ObjectFactoryBasePrivate * globalInstance =
134-
Singleton<ObjectFactoryBasePrivate>("ObjectFactoryBase", SynchronizeObjectFactoryBase, deleteLambda);
133+
ObjectFactoryBasePrivate * globalInstance = Singleton<ObjectFactoryBasePrivate>("ObjectFactoryBase", deleteLambda);
135134
if (globalInstance != m_PimplGlobals)
136135
{
137136
SynchronizeObjectFactoryBase(globalInstance);

0 commit comments

Comments
 (0)