Skip to content

Commit

Permalink
STYLE: SingletonIndex does not need to store the unused func parameter
Browse files Browse the repository at this point in the history
No longer stored the `func` parameter from `SingletonIndex::SetGlobalInstance`
and from `Singleton(globalName, func, deleteFunc)`, as it was never actually
retrieved from `m_GlobalObjects` afterwards.

Deprecated the `SingletonIndex::SingletonData` type alias, as it is no longer
used internally anymore, and it should not be public either.
  • Loading branch information
N-Dekker authored and dzenanz committed Sep 6, 2023
1 parent 1801209 commit 6d1c4c7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
24 changes: 13 additions & 11 deletions Modules/Core/Common/include/itkSingleton.h
Expand Up @@ -47,7 +47,12 @@ class ITKCommon_EXPORT SingletonIndex
public:
/** Standard class types. */
using Self = SingletonIndex;
using SingletonData = std::map<std::string, std::tuple<void *, std::function<void(void *)>, std::function<void()>>>;

#ifndef ITK_LEGACY_REMOVE
using SingletonData [[deprecated("The internal representation of the singleton data is private, and may not "
"correspond with SingletonData anymore.")]] =
std::map<std::string, std::tuple<void *, std::function<void(void *)>, std::function<void()>>>;
#endif

// obtain a global registered in the singleton index under the
// globalName, if unknown then nullptr will be returned.
Expand All @@ -67,10 +72,10 @@ class ITKCommon_EXPORT SingletonIndex
bool
SetGlobalInstance(const char * globalName,
T * global,
std::function<void(void *)> func,
std::function<void(void *)> itkNotUsed(func),
std::function<void()> deleteFunc)
{
this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc);
this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
return true;
}

Expand All @@ -94,33 +99,30 @@ class ITKCommon_EXPORT SingletonIndex

// global is added or set to the singleton index under globalName
void
SetGlobalInstancePrivate(const char * globalName,
void * global,
std::function<void(void *)> func,
std::function<void()> deleteFunc);
SetGlobalInstancePrivate(const char * globalName, void * global, std::function<void()> deleteFunc);

/** The static GlobalSingleton. This is initialized to nullptr as the first
* stage of static initialization. It is then populated on the first call to
* itk::Singleton::Modified() but it can be overridden with SetGlobalSingleton().
* */
SingletonData m_GlobalObjects;
static Self * m_Instance;
std::map<std::string, std::tuple<void *, std::function<void()>>> m_GlobalObjects;
static Self * m_Instance;
// static SingletonIndexPrivate * m_GlobalSingleton;
};


// A wrapper for a global variable registered in the singleton index.
template <typename T>
T *
Singleton(const char * globalName, std::function<void(void *)> func, std::function<void()> deleteFunc)
Singleton(const char * globalName, std::function<void(void *)> itkNotUsed(func), 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, func, deleteFunc);
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, {}, deleteFunc);
}
return instance;
}
Expand Down
6 changes: 1 addition & 5 deletions Modules/Core/Common/include/itkSingletonMacro.h
Expand Up @@ -44,16 +44,12 @@
{ \
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); \
m_##VarName = Singleton<Type>(#SingletonName, {}, deleteLambda); \
if (old_instance == nullptr) \
{ \
Init; \
Expand Down
12 changes: 4 additions & 8 deletions Modules/Core/Common/src/itkSingleton.cxx
Expand Up @@ -87,8 +87,7 @@ namespace itk
void *
SingletonIndex::GetGlobalInstancePrivate(const char * globalName)
{
SingletonData::iterator it;
it = m_GlobalObjects.find(globalName);
const auto it = m_GlobalObjects.find(globalName);
if (it == m_GlobalObjects.end())
{
return nullptr;
Expand All @@ -99,12 +98,9 @@ SingletonIndex::GetGlobalInstancePrivate(const char * globalName)
// If globalName is already registered, set its global as specified,
// otherwise global is added to the singleton index under globalName
void
SingletonIndex::SetGlobalInstancePrivate(const char * globalName,
void * global,
std::function<void(void *)> func,
std::function<void()> deleteFunc)
SingletonIndex::SetGlobalInstancePrivate(const char * globalName, void * global, std::function<void()> deleteFunc)
{
m_GlobalObjects.insert_or_assign(globalName, std::make_tuple(global, func, deleteFunc));
m_GlobalObjects.insert_or_assign(globalName, std::make_tuple(global, deleteFunc));
}

SingletonIndex *
Expand All @@ -128,7 +124,7 @@ SingletonIndex::~SingletonIndex()
{
for (auto & pair : m_GlobalObjects)
{
std::get<2>(pair.second)();
std::get<1>(pair.second)();
}
}

Expand Down

0 comments on commit 6d1c4c7

Please sign in to comment.