Skip to content

Commit

Permalink
Check existing resource ids from OPC package before generating a reso…
Browse files Browse the repository at this point in the history
…urce id #380 #355 #183
  • Loading branch information
vijaiaeroastro committed Aug 5, 2024
1 parent 2601a62 commit 6aa56ab
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions Source/Model/Classes/NMR_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,19 +391,30 @@ 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;
}

void CModel::updateUniqueResourceID(UniqueResourceID nOldID, UniqueResourceID nNewID)
ModelResourceID CModel::generateResourceID()
{
// Build a map with existing resource IDs
std::map<ModelResourceID, bool> existingResourceIDs;
for (const auto& resource : m_Resources) {
existingResourceIDs[resource->getPackageResourceID()->getModelResourceID()] = true;
}

// Determine the initial resource ID
ModelResourceID currentResourceID = 1;
auto iIterator = m_ResourceMap.rbegin();
if (iIterator != m_ResourceMap.rend()) {
currentResourceID = iIterator->first + 1;
}

// Ensure the generated ID is not in the existing resource IDs map
if (!existingResourceIDs.empty() && existingResourceIDs.find(currentResourceID) != existingResourceIDs.end()) {
currentResourceID = existingResourceIDs.rbegin()->first + 1;
}

return currentResourceID;
}

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

Please sign in to comment.