--- libgig/trunk/src/gig.cpp 2021/06/17 12:04:54 3939 +++ libgig/trunk/src/gig.cpp 2021/06/19 09:10:33 3952 @@ -4801,8 +4801,8 @@ ScriptGroup::~ScriptGroup() { if (pScripts) { - std::list::iterator iter = pScripts->begin(); - std::list::iterator end = pScripts->end(); + std::vector::iterator iter = pScripts->begin(); + std::vector::iterator end = pScripts->end(); while (iter != end) { delete *iter; ++iter; @@ -4840,7 +4840,7 @@ // now store the name of this group as chunk as subchunk of the list chunk ::SaveString(CHUNK_ID_LSNM, NULL, pList, Name, String("Unnamed Group"), true, 64); - for (std::list::iterator it = pScripts->begin(); + for (std::vector::iterator it = pScripts->begin(); it != pScripts->end(); ++it) { (*it)->UpdateChunks(pProgress); @@ -4855,12 +4855,10 @@ * @param index - number of the sought script (0..n) * @returns sought script or NULL if there's no such script */ - Script* ScriptGroup::GetScript(uint index) { + Script* ScriptGroup::GetScript(size_t index) { if (!pScripts) LoadScripts(); - std::list::iterator it = pScripts->begin(); - for (uint i = 0; it != pScripts->end(); ++i, ++it) - if (i == index) return *it; - return NULL; + if (index >= pScripts->size()) return NULL; + return (*pScripts)[index]; } /** @brief Add new instrument script. @@ -4893,7 +4891,7 @@ */ void ScriptGroup::DeleteScript(Script* pScript) { if (!pScripts) LoadScripts(); - std::list::iterator iter = + std::vector::iterator iter = find(pScripts->begin(), pScripts->end(), pScript); if (iter == pScripts->end()) throw gig::Exception("Could not delete script, could not find given script"); @@ -4906,7 +4904,7 @@ void ScriptGroup::LoadScripts() { if (pScripts) return; - pScripts = new std::list; + pScripts = new std::vector; if (!pList) return; size_t i = 0; @@ -5394,7 +5392,8 @@ * @param dst - destination instrument at which this instrument will be * moved to, or pass NULL for moving to end of list * @throw gig::Exception if this instrument and target instrument are not - * part of the same file + * part of the same file, as well as on unexpected + * internal error */ void Instrument::MoveTo(Instrument* dst) { if (dst && GetParent() != dst->GetParent()) @@ -5411,11 +5410,17 @@ File::InstrumentList::iterator itFrom = std::find(list.begin(), list.end(), static_cast(this)); + if (itFrom == list.end()) + throw Exception( + "gig::Instrument::MoveTo(): unexpected missing membership " + "of this instrument." + ); + list.erase(itFrom); File::InstrumentList::iterator itTo = std::find(list.begin(), list.end(), static_cast(dst)); - list.splice(itTo, list, itFrom); + list.insert(itTo, this); } // move the instrument's actual list RIFF chunk appropriately @@ -5496,7 +5501,7 @@ File* pFile = (File*) GetParent(); for (uint k = 0; k < scriptPoolFileOffsets.size(); ++k) { uint32_t soughtOffset = scriptPoolFileOffsets[k].fileOffset; - for (uint i = 0; pFile->GetScriptGroup(i); ++i) { + for (size_t i = 0; pFile->GetScriptGroup(i); ++i) { ScriptGroup* group = pFile->GetScriptGroup(i); for (uint s = 0; group->GetScript(s); ++s) { Script* script = group->GetScript(s); @@ -5534,7 +5539,7 @@ * @param index - instrument script slot index * @returns script or NULL if index is out of bounds */ - Script* Instrument::GetScriptOfSlot(uint index) { + Script* Instrument::GetScriptOfSlot(size_t index) { LoadScripts(); if (index >= pScriptRefs->size()) return NULL; return pScriptRefs->at(index).script; @@ -6298,8 +6303,8 @@ delete pGroups; } if (pScriptGroups) { - std::list::iterator iter = pScriptGroups->begin(); - std::list::iterator end = pScriptGroups->end(); + std::vector::iterator iter = pScriptGroups->begin(); + std::vector::iterator end = pScriptGroups->end(); while (iter != end) { delete *iter; ++iter; @@ -6405,10 +6410,11 @@ pSample->DeleteChunks(); delete pSample; - SampleList::iterator tmp = SamplesIterator; // remove all references to the sample - for (Instrument* instrument = GetFirstInstrument() ; instrument ; - instrument = GetNextInstrument()) { + size_t iIns = 0; + for (Instrument* instrument = GetInstrument(iIns); instrument; + instrument = GetInstrument(++iIns)) + { size_t iRgn = 0; for (Region* region = instrument->GetRegionAt(iRgn); region; region = instrument->GetRegionAt(++iRgn)) @@ -6421,7 +6427,6 @@ } } } - SamplesIterator = tmp; // restore iterator } void File::LoadSamples() { @@ -6539,6 +6544,13 @@ __notify_progress(pProgress, 1.0); // notify done } + /** + * Returns a pointer to the first Instrument object of the file, + * NULL otherwise. + * + * @deprecated This method is not reentrant-safe, use GetInstrument() + * instead. + */ Instrument* File::GetFirstInstrument() { if (!pInstruments) LoadInstruments(); if (!pInstruments) return NULL; @@ -6546,6 +6558,13 @@ return static_cast( (InstrumentsIterator != pInstruments->end()) ? *InstrumentsIterator : NULL ); } + /** + * Returns a pointer to the next Instrument object of the file, + * NULL otherwise. + * + * @deprecated This method is not reentrant-safe, use GetInstrument() + * instead. + */ Instrument* File::GetNextInstrument() { if (!pInstruments) return NULL; InstrumentsIterator++; @@ -6573,7 +6592,7 @@ * @param pProgress - optional: callback function for progress notification * @returns sought instrument or NULL if there's no such instrument */ - Instrument* File::GetInstrument(uint index, progress_t* pProgress) { + Instrument* File::GetInstrument(size_t index, progress_t* pProgress) { if (!pInstruments) { // TODO: hack - we simply load ALL samples here, it would have been done in the Region constructor anyway (ATM) @@ -6604,12 +6623,8 @@ } } if (!pInstruments) return NULL; - InstrumentsIterator = pInstruments->begin(); - for (uint i = 0; InstrumentsIterator != pInstruments->end(); i++) { - if (i == index) return static_cast( *InstrumentsIterator ); - InstrumentsIterator++; - } - return NULL; + if (index >= pInstruments->size()) return NULL; + return static_cast( (*pInstruments)[index] ); } /** @brief Add a new instrument definition. @@ -6696,7 +6711,7 @@ } // clone script groups and their scripts - for (int iGroup = 0; pFile->GetScriptGroup(iGroup); ++iGroup) { + for (size_t iGroup = 0; pFile->GetScriptGroup(iGroup); ++iGroup) { ScriptGroup* sg = pFile->GetScriptGroup(iGroup); ScriptGroup* dg = AddScriptGroup(); dg->Name = "COPY" + ToString(iCallCount) + "_" + sg->Name; @@ -7088,12 +7103,10 @@ * @param index - number of the sought group (0..n) * @returns sought script group or NULL if there's no such group */ - ScriptGroup* File::GetScriptGroup(uint index) { + ScriptGroup* File::GetScriptGroup(size_t index) { if (!pScriptGroups) LoadScriptGroups(); - std::list::iterator it = pScriptGroups->begin(); - for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) - if (i == index) return *it; - return NULL; + if (index >= pScriptGroups->size()) return NULL; + return (*pScriptGroups)[index]; } /** @brief Get instrument script group (by name). @@ -7106,9 +7119,10 @@ */ ScriptGroup* File::GetScriptGroup(const String& name) { if (!pScriptGroups) LoadScriptGroups(); - std::list::iterator it = pScriptGroups->begin(); - for (uint i = 0; it != pScriptGroups->end(); ++i, ++it) - if ((*it)->Name == name) return *it; + for (size_t i = 0; i < pScriptGroups->size(); ++i) { + ScriptGroup* pGroup = (*pScriptGroups)[i]; + if (pGroup->Name == name) return pGroup; + } return NULL; } @@ -7141,7 +7155,7 @@ */ void File::DeleteScriptGroup(ScriptGroup* pScriptGroup) { if (!pScriptGroups) LoadScriptGroups(); - std::list::iterator iter = + std::vector::iterator iter = find(pScriptGroups->begin(), pScriptGroups->end(), pScriptGroup); if (iter == pScriptGroups->end()) throw gig::Exception("Could not delete script group, could not find given script group"); @@ -7156,7 +7170,7 @@ void File::LoadScriptGroups() { if (pScriptGroups) return; - pScriptGroups = new std::list; + pScriptGroups = new std::vector; RIFF::List* lstLS = pRIFF->GetSubList(LIST_TYPE_3LS); if (lstLS) { size_t i = 0; @@ -7201,7 +7215,7 @@ // of the respective instrument script chunk as reference. if (pScriptGroups) { // Update instrument script (group) chunks. - for (std::list::iterator it = pScriptGroups->begin(); + for (std::vector::iterator it = pScriptGroups->begin(); it != pScriptGroups->end(); ++it) { (*it)->UpdateChunks(pProgress); @@ -7308,8 +7322,10 @@ memset(&pData[48], 0, sublen - 48); - for (Instrument* instrument = GetFirstInstrument() ; instrument ; - instrument = GetNextInstrument()) { + size_t iIns = 0; + for (Instrument* instrument = GetInstrument(iIns); instrument; + instrument = GetInstrument(++iIns)) + { int nbusedsamples = 0; int nbusedchannels = 0; int nbdimregions = 0; @@ -7414,8 +7430,9 @@ void File::UpdateFileOffsets() { DLS::File::UpdateFileOffsets(); - for (Instrument* instrument = GetFirstInstrument(); instrument; - instrument = GetNextInstrument()) + size_t i = 0; + for (Instrument* instrument = GetInstrument(i); instrument; + instrument = GetInstrument(++i)) { instrument->UpdateScriptFileOffsets(); }