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 committed Aug 20, 2023
1 parent c3ae515 commit 8fc9b00
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
Original file line number Diff line number Diff line change
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)
{
return this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc);
return this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
}

/** Set/Get the pointer to GlobalSingleton.
Expand All @@ -93,33 +98,30 @@ class ITKCommon_EXPORT SingletonIndex
// If globalName is already registered than false is return,
// otherwise global is added to the singleton index under globalName
bool
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;
if (!SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, func, deleteFunc))
if (!SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, {}, deleteFunc))
{
delete instance;
instance = nullptr;
Expand Down
6 changes: 1 addition & 5 deletions Modules/Core/Common/include/itkSingletonMacro.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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,13 +98,10 @@ SingletonIndex::GetGlobalInstancePrivate(const char * globalName)
// If globalName is already registered remove it from map,
// otherwise global is added to the singleton index under globalName
bool
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.erase(globalName);
m_GlobalObjects.insert(std::make_pair(globalName, std::make_tuple(global, func, deleteFunc)));
m_GlobalObjects.insert(std::make_pair(globalName, std::make_tuple(global, deleteFunc)));
return true;
}

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

Expand Down

0 comments on commit 8fc9b00

Please sign in to comment.