Skip to content

Commit

Permalink
STYLE: Remove unused parameter from SetGlobalInstance and Singleton
Browse files Browse the repository at this point in the history
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.

Follow-up to:

pull request InsightSoftwareConsortium#4164
commit 6d1c4c7
"STYLE: SingletonIndex does not need to store the unused `func` parameter"

pull request InsightSoftwareConsortium#4162
commit 6ec6328
"STYLE: Let `Singleton` assume that SetGlobalInstance always returns true"
  • Loading branch information
N-Dekker committed Sep 7, 2023
1 parent 4b6ff31 commit 7a1f9ac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
32 changes: 26 additions & 6 deletions Modules/Core/Common/include/itkSingleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,29 @@ class ITKCommon_EXPORT SingletonIndex
}


// Returns true.
//
// It is assumed that the global will remain valid until the start
// of globals being destroyed.
template <typename T>
bool
void
SetGlobalInstance(const char * globalName, T * global, std::function<void()> deleteFunc)
{
this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
}

#ifndef ITK_LEGACY_REMOVE
template <typename T>
[[deprecated("Prefer calling the SetGlobalInstance(globalName, global, deleteFunc) overload (without the unused func "
"parameter)!")]] bool
SetGlobalInstance(const char * globalName,
T * global,
std::function<void(void *)> itkNotUsed(func),
std::function<void()> deleteFunc)
{
this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
this->SetGlobalInstance(globalName, global, deleteFunc);
// Just returns true for backward compatibility (legacy only).
return true;
}
#endif

/** Set/Get the pointer to GlobalSingleton.
* Note that SetGlobalSingleton is not concurrent thread safe. */
Expand Down Expand Up @@ -114,18 +123,29 @@ class ITKCommon_EXPORT SingletonIndex
// A wrapper for a global variable registered in the singleton index.
template <typename T>
T *
Singleton(const char * globalName, std::function<void(void *)> itkNotUsed(func), std::function<void()> deleteFunc)
Singleton(const char * globalName, std::function<void()> deleteFunc)
{
static SingletonIndex * singletonIndex = SingletonIndex::GetInstance();
Unused(singletonIndex);
T * instance = SingletonIndex::GetInstance()->GetGlobalInstance<T>(globalName);
if (instance == nullptr)
{
instance = new T;
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, {}, deleteFunc);
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, deleteFunc);
}
return instance;
}


#ifndef ITK_LEGACY_REMOVE
template <typename T>
[[deprecated("Prefer calling the Singleton(globalName, deleteFunc) overload (without the unused func parameter)!")]] T *
Singleton(const char * globalName, std::function<void(void *)> itkNotUsed(func), std::function<void()> deleteFunc)
{
return Singleton<T>(globalName, deleteFunc);
}
#endif

} // end namespace itk

#endif
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkSingletonMacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
m_##VarName = nullptr; \
}; \
auto * old_instance = SingletonIndex::GetInstance()->GetGlobalInstance<Type>(#SingletonName); \
m_##VarName = Singleton<Type>(#SingletonName, {}, deleteLambda); \
m_##VarName = Singleton<Type>(#SingletonName, deleteLambda); \
if (old_instance == nullptr) \
{ \
Init; \
Expand Down
3 changes: 1 addition & 2 deletions Modules/Core/Common/src/itkObjectFactoryBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ auto
ObjectFactoryBase::GetPimplGlobalsPointer() -> ObjectFactoryBasePrivate *
{
const auto deleteLambda = []() { m_PimplGlobals->UnRegister(); };
ObjectFactoryBasePrivate * globalInstance =
Singleton<ObjectFactoryBasePrivate>("ObjectFactoryBase", SynchronizeObjectFactoryBase, deleteLambda);
ObjectFactoryBasePrivate * globalInstance = Singleton<ObjectFactoryBasePrivate>("ObjectFactoryBase", deleteLambda);
if (globalInstance != m_PimplGlobals)
{
SynchronizeObjectFactoryBase(globalInstance);
Expand Down

0 comments on commit 7a1f9ac

Please sign in to comment.