Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_FUTURE_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_FUTURE_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