Skip to content

Commit

Permalink
STYLE: Replace tuple with private GlobalObject struct, in SingletonIndex
Browse files Browse the repository at this point in the history
A simple `struct` appears more readable than an `std::tuple`, in this case,
especially because the struct has a name for each data element (`Instance` and
`DeleteFunc`), whereas each tuple data element is only accessed by its number.
  • Loading branch information
N-Dekker authored and dzenanz committed Jan 25, 2024
1 parent f58ccd1 commit 902d1f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 11 additions & 4 deletions Modules/Core/Common/include/itkSingleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ITKCommon_EXPORT SingletonIndex
void
SetGlobalInstance(const char * globalName, T * global, std::function<void()> deleteFunc)
{
this->SetGlobalInstancePrivate(globalName, global, std::move(deleteFunc));
this->SetGlobalInstancePrivate(globalName, GlobalObject{ global, std::move(deleteFunc) });
}

#ifndef ITK_FUTURE_LEGACY_REMOVE
Expand All @@ -99,6 +99,13 @@ class ITKCommon_EXPORT SingletonIndex
~SingletonIndex();

private:
// Internal struct to store the instance pointer and the delete function object of a global object.
struct GlobalObject
{
void * Instance{};
std::function<void()> DeleteFunc{};
};

// may return nullptr if string is not registered already
//
// access something like a std::map<std::string, void *> or
Expand All @@ -110,14 +117,14 @@ 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()> deleteFunc);
SetGlobalInstancePrivate(const char * globalName, GlobalObject globalObject);

/** 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().
* */
std::map<std::string, std::tuple<void *, std::function<void()>>> m_GlobalObjects;
static Self * m_Instance;
std::map<std::string, GlobalObject> m_GlobalObjects;
static Self * m_Instance;
// static SingletonIndexPrivate * m_GlobalSingleton;
};

Expand Down
8 changes: 4 additions & 4 deletions Modules/Core/Common/src/itkSingleton.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ SingletonIndex::GetGlobalInstancePrivate(const char * globalName)
{
return nullptr;
}
return std::get<0>(it->second);
return it->second.Instance;
}

// 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()> deleteFunc)
SingletonIndex::SetGlobalInstancePrivate(const char * globalName, GlobalObject globalObject)
{
m_GlobalObjects.insert_or_assign(globalName, std::make_tuple(global, std::move(deleteFunc)));
m_GlobalObjects.insert_or_assign(globalName, std::move(globalObject));
}

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

Expand Down

0 comments on commit 902d1f2

Please sign in to comment.