From 782373cb0d8c6e84145a427f0459fc70bda22214 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Wed, 24 Jan 2024 15:38:47 +0100 Subject: [PATCH] STYLE: Replace tuple with private GlobalObject struct, in SingletonIndex 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. --- Modules/Core/Common/include/itkSingleton.h | 15 +++++++++++---- Modules/Core/Common/src/itkSingleton.cxx | 8 ++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Modules/Core/Common/include/itkSingleton.h b/Modules/Core/Common/include/itkSingleton.h index 67bcf570655..a0847f87323 100644 --- a/Modules/Core/Common/include/itkSingleton.h +++ b/Modules/Core/Common/include/itkSingleton.h @@ -72,7 +72,7 @@ class ITKCommon_EXPORT SingletonIndex void SetGlobalInstance(const char * globalName, T * global, std::function deleteFunc) { - this->SetGlobalInstancePrivate(globalName, global, std::move(deleteFunc)); + this->SetGlobalInstancePrivate(globalName, GlobalObject{ global, std::move(deleteFunc) }); } #ifndef ITK_FUTURE_LEGACY_REMOVE @@ -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 DeleteFunc{}; + }; + // may return nullptr if string is not registered already // // access something like a std::map or @@ -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 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>> m_GlobalObjects; - static Self * m_Instance; + std::map m_GlobalObjects; + static Self * m_Instance; // static SingletonIndexPrivate * m_GlobalSingleton; }; diff --git a/Modules/Core/Common/src/itkSingleton.cxx b/Modules/Core/Common/src/itkSingleton.cxx index 83b4740da70..fab30b04d96 100644 --- a/Modules/Core/Common/src/itkSingleton.cxx +++ b/Modules/Core/Common/src/itkSingleton.cxx @@ -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 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 * @@ -118,7 +118,7 @@ SingletonIndex::~SingletonIndex() { for (auto & pair : m_GlobalObjects) { - std::get<1>(pair.second)(); + pair.second.DeleteFunc(); } }