Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STYLE: Replace tuple with private GlobalObject struct, in SingletonIndex #4424

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
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