Skip to content

Commit

Permalink
Merge pull request #4162 from N-Dekker/SetGlobalInstance-returns-true
Browse files Browse the repository at this point in the history
Document and use the fact that `SingletonIndex::SetGlobalInstance` always returns true
  • Loading branch information
thewtex authored Sep 5, 2023
2 parents 79dd54c + 6ec6328 commit 7fb08b8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
17 changes: 7 additions & 10 deletions Modules/Core/Common/include/itkSingleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -70,7 +70,8 @@ class ITKCommon_EXPORT SingletonIndex
std::function<void(void *)> func,
std::function<void()> deleteFunc)
{
return this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc);
this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc);
return true;
}

/** Set/Get the pointer to GlobalSingleton.
Expand All @@ -90,9 +91,9 @@ class ITKCommon_EXPORT SingletonIndex
// work, and could use some type of Holder<T> 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<void(void *)> func,
Expand All @@ -119,11 +120,7 @@ Singleton(const char * globalName, std::function<void(void *)> func, std::functi
if (instance == nullptr)
{
instance = new T;
if (!SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, func, deleteFunc))
{
delete instance;
instance = nullptr;
}
SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, func, deleteFunc);
}
return instance;
}
Expand Down
8 changes: 3 additions & 5 deletions Modules/Core/Common/src/itkSingleton.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(void *)> func,
std::function<void()> 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 *
Expand Down

0 comments on commit 7fb08b8

Please sign in to comment.