7 #include "MaterialId.h"
26 , m_allocatedIdentifierCount(0)
27 , m_releaseCallback(
this, &MaterialIdGenerator::ReleaseIdentifier)
31 TMaterialId GetNextId()
33 return TMaterialId(Eegeo_NEW(
TMaterialIdWrapper)(AllocateNextIdentifier(), m_releaseCallback));
37 void ReleaseIdentifier(
const TMaterialIdWrapper::TValueType& identifier)
39 Eegeo_ASSERT(m_allocatedIdentifierCount > 0,
40 "Releasing material but no materials allocated. Indicates possible double release of identifier.\n");
42 --m_allocatedIdentifierCount;
43 size_t valuesErased(m_identifiersUsed.erase(identifier));
45 Eegeo_ASSERT(valuesErased == 1,
46 "Error: Consistency error in tracking material identifiers. Untracked identifier released.\n");
49 TMaterialIdWrapper::TValueType AllocateNextIdentifier()
51 Eegeo_ASSERT(m_allocatedIdentifierCount < SortKey::MaxMaterialValue,
52 "Too many materials allocated, out of space to identify. Indicates possible identifier leak.\n");
54 ++m_allocatedIdentifierCount;
55 TMaterialIdWrapper::TValueType identifierToUse(m_nextIdentifier);
56 std::pair<TIdentifiersUsed::iterator, bool> insertionResult(m_identifiersUsed.insert(identifierToUse));
57 Eegeo_ASSERT(insertionResult.second,
"Error: reused material identifier in MaterialIdGenerator.\n");
58 m_nextIdentifier = FindNextUnusedIdentifierAfter(identifierToUse);
60 return identifierToUse;
63 TMaterialIdWrapper::TValueType FindNextUnusedIdentifierAfter(TMaterialIdWrapper::TValueType identifier)
67 identifier = (identifier + 1) % SortKey::MaxMaterialValue;
69 while(m_identifiersUsed.find(identifier) != m_identifiersUsed.end());
74 TMaterialIdWrapper::TValueType m_nextIdentifier;
75 int m_allocatedIdentifierCount;
78 typedef std::set<TMaterialIdWrapper::TValueType> TIdentifiersUsed;
79 TIdentifiersUsed m_identifiersUsed;