diff --git a/Modules/Core/Common/include/itkSingleton.h b/Modules/Core/Common/include/itkSingleton.h index 3fe961648de..5f2ce22cca9 100644 --- a/Modules/Core/Common/include/itkSingleton.h +++ b/Modules/Core/Common/include/itkSingleton.h @@ -59,7 +59,7 @@ class ITKCommon_EXPORT SingletonIndex } - // returns true if the globalName has not been registered yet. + // Returns true. // // It is assumed that the global will remain valid until the start // of globals being destroyed. @@ -70,7 +70,8 @@ class ITKCommon_EXPORT SingletonIndex std::function func, std::function deleteFunc) { - return this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc); + this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc); + return true; } /** Set/Get the pointer to GlobalSingleton. @@ -90,9 +91,9 @@ class ITKCommon_EXPORT SingletonIndex // work, and could use some type of Holder class for intrinsic types void * GetGlobalInstancePrivate(const char * globalName); - // If globalName is already registered than false is return, - // otherwise global is added to the singleton index under globalName - bool + + // global is added or set to the singleton index under globalName + void SetGlobalInstancePrivate(const char * globalName, void * global, std::function func, @@ -119,11 +120,7 @@ Singleton(const char * globalName, std::function func, std::functi if (instance == nullptr) { instance = new T; - if (!SingletonIndex::GetInstance()->SetGlobalInstance(globalName, instance, func, deleteFunc)) - { - delete instance; - instance = nullptr; - } + SingletonIndex::GetInstance()->SetGlobalInstance(globalName, instance, func, deleteFunc); } return instance; } diff --git a/Modules/Core/Common/src/itkSingleton.cxx b/Modules/Core/Common/src/itkSingleton.cxx index d76b1e88051..1607971baa1 100644 --- a/Modules/Core/Common/src/itkSingleton.cxx +++ b/Modules/Core/Common/src/itkSingleton.cxx @@ -96,17 +96,15 @@ SingletonIndex::GetGlobalInstancePrivate(const char * globalName) return std::get<0>(it->second); } -// If globalName is already registered remove it from map, +// If globalName is already registered, set its global as specified, // otherwise global is added to the singleton index under globalName -bool +void SingletonIndex::SetGlobalInstancePrivate(const char * globalName, void * global, std::function func, std::function deleteFunc) { - m_GlobalObjects.erase(globalName); - m_GlobalObjects.insert(std::make_pair(globalName, std::make_tuple(global, func, deleteFunc))); - return true; + m_GlobalObjects.insert_or_assign(globalName, std::make_tuple(global, func, deleteFunc)); } SingletonIndex *