Skip to content

Commit

Permalink
[CModel] Refactor resource ID generation to ensure uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
3dJan committed Sep 17, 2024
1 parent daf8c64 commit 6f6e534
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions Source/Model/Classes/NMR_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,22 @@ namespace NMR {
// Retrieve a unique Resource ID
ModelResourceID CModel::generateResourceID()
{
// TODO: is this truly safe?
auto iIterator = m_ResourceMap.rbegin();
if (iIterator != m_ResourceMap.rend())
return iIterator->first + 1;
else
return 1;
ModelResourceID highestID = 0;

if (m_ResourceMap.empty())
{
return 1;
}

highestID = std::max_element(m_ResourceMap.begin(), m_ResourceMap.end(),
[](const auto &a, const auto &b) {
return a.first < b.first;
})->first;

return highestID + 1;
}

void CModel::updateUniqueResourceID(UniqueResourceID nOldID, UniqueResourceID nNewID)
void CModel::updateUniqueResourceID(UniqueResourceID nOldID, UniqueResourceID nNewID)
{
if (m_ResourceMap.find(nNewID) != m_ResourceMap.end()) {
throw CNMRException(NMR_ERROR_DUPLICATEMODELRESOURCE);
Expand Down

0 comments on commit 6f6e534

Please sign in to comment.