--- libgig/trunk/src/gig.cpp 2021/06/18 14:56:53 3943 +++ libgig/trunk/src/gig.cpp 2021/07/16 16:53:13 3974 @@ -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; @@ -5310,6 +5308,7 @@ * @param pos - position of sought Region in region list * @returns pointer address to requested region or @c NULL if @a pos is * out of bounds + * @see CountRegions() */ Region* Instrument::GetRegionAt(size_t pos) { if (!pRegions) return NULL; @@ -5503,7 +5502,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); @@ -5541,7 +5540,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; @@ -5602,7 +5601,7 @@ * @param index1 - index of the first script slot to swap * @param index2 - index of the second script slot to swap */ - void Instrument::SwapScriptSlots(uint index1, uint index2) { + void Instrument::SwapScriptSlots(size_t index1, size_t index2) { LoadScripts(); if (index1 >= pScriptRefs->size() || index2 >= pScriptRefs->size()) return; @@ -5617,7 +5616,7 @@ * * @param index - index of script slot to remove */ - void Instrument::RemoveScriptSlot(uint index) { + void Instrument::RemoveScriptSlot(size_t index) { LoadScripts(); if (index >= pScriptRefs->size()) return; pScriptRefs->erase( pScriptRefs->begin() + index ); @@ -5658,8 +5657,8 @@ * GigaStudio 4 software. It will currently only work with LinuxSampler and * gigedit. */ - uint Instrument::ScriptSlotCount() const { - return uint(pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size()); + size_t Instrument::ScriptSlotCount() const { + return pScriptRefs ? pScriptRefs->size() : scriptPoolFileOffsets.size(); } /** @brief Whether script execution shall be skipped. @@ -5678,7 +5677,7 @@ * @param index - index of the script slot on this instrument * @see Script::Bypass */ - bool Instrument::IsScriptSlotBypassed(uint index) { + bool Instrument::IsScriptSlotBypassed(size_t index) { if (index >= ScriptSlotCount()) return false; return pScriptRefs ? pScriptRefs->at(index).bypass : scriptPoolFileOffsets.at(index).bypass; @@ -5698,7 +5697,7 @@ * @param bBypass - if true, the script slot will be skipped by the sampler * @see Script::Bypass */ - void Instrument::SetScriptSlotBypassed(uint index, bool bBypass) { + void Instrument::SetScriptSlotBypassed(size_t index, bool bBypass) { if (index >= ScriptSlotCount()) return; if (pScriptRefs) pScriptRefs->at(index).bypass = bBypass; @@ -5718,8 +5717,8 @@ * the @c Script identified by passed @p uuid. */ bool Instrument::ReferencesScriptWithUuid(const _UUID& uuid) { - const uint nSlots = ScriptSlotCount(); - for (uint iSlot = 0; iSlot < nSlots; ++iSlot) + const size_t nSlots = ScriptSlotCount(); + for (size_t iSlot = 0; iSlot < nSlots; ++iSlot) if (_UUIDFromCArray(&GetScriptOfSlot(iSlot)->Uuid[0]) == uuid) return true; return false; @@ -5744,7 +5743,7 @@ * @param slot - script slot index of the variable to be retrieved * @param variable - name of the 'patch' variable in that script */ - bool Instrument::IsScriptPatchVariableSet(int slot, String variable) { + bool Instrument::IsScriptPatchVariableSet(size_t slot, String variable) { if (variable.empty()) return false; Script* script = GetScriptOfSlot(slot); if (!script) return false; @@ -5778,7 +5777,7 @@ * * @param slot - script slot index of the variable to be retrieved */ - std::map Instrument::GetScriptPatchVariables(int slot) { + std::map Instrument::GetScriptPatchVariables(size_t slot) { Script* script = GetScriptOfSlot(slot); if (!script) return std::map(); const _UUID uuid = _UUIDFromCArray(&script->Uuid[0]); @@ -5810,7 +5809,7 @@ * @param slot - script slot index of the variable to be retrieved * @param variable - name of the 'patch' variable in that script */ - String Instrument::GetScriptPatchVariable(int slot, String variable) { + String Instrument::GetScriptPatchVariable(size_t slot, String variable) { std::map vars = GetScriptPatchVariables(slot); return (vars.count(variable)) ? vars.find(variable)->second : ""; } @@ -5837,7 +5836,7 @@ * @throws gig::Exception if given script @p slot index is invalid or given * @p variable name is empty */ - void Instrument::SetScriptPatchVariable(int slot, String variable, String value) { + void Instrument::SetScriptPatchVariable(size_t slot, String variable, String value) { if (variable.empty()) throw Exception("Variable name must not be empty"); Script* script = GetScriptOfSlot(slot); @@ -5878,7 +5877,7 @@ * @param slot - script slot index of the variable to be unset * @param variable - name of the 'patch' variable in that script */ - void Instrument::UnsetScriptPatchVariable(int slot, String variable) { + void Instrument::UnsetScriptPatchVariable(ssize_t slot, String variable) { Script* script = GetScriptOfSlot(slot); // option 1: unset a particular variable of one particular script slot @@ -6305,8 +6304,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; @@ -6390,7 +6389,9 @@ wave->AddSubChunk(CHUNK_ID_FMT, 16); wave->AddSubList(LIST_TYPE_INFO); + const size_t idxIt = SamplesIterator - pSamples->begin(); pSamples->push_back(pSample); + SamplesIterator = pSamples->begin() + std::min(idxIt, pSamples->size()); // avoid iterator invalidation return pSample; } @@ -6407,15 +6408,17 @@ if (!pSamples || !pSamples->size()) throw gig::Exception("Could not delete sample as there are no samples"); SampleList::iterator iter = find(pSamples->begin(), pSamples->end(), (DLS::Sample*) pSample); if (iter == pSamples->end()) throw gig::Exception("Could not delete sample, could not find given sample"); - if (SamplesIterator != pSamples->end() && *SamplesIterator == pSample) ++SamplesIterator; // avoid iterator invalidation + const size_t idxIt = SamplesIterator - pSamples->begin(); pSamples->erase(iter); + SamplesIterator = pSamples->begin() + std::min(idxIt, pSamples->size()); // avoid iterator invalidation 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)) @@ -6428,7 +6431,6 @@ } } } - SamplesIterator = tmp; // restore iterator } void File::LoadSamples() { @@ -6515,16 +6517,17 @@ ExtensionFiles.push_back(pExtFile); } - // load samples from extension files (if required) + // load all samples (both from this/main .gig file as well as from + // extension files if required) for (int i = 0; i < poolFiles.size(); i++) { RIFF::File* file = poolFiles[i]; RIFF::List* wvpl = file->GetSubList(LIST_TYPE_WVPL); if (wvpl) { file_offset_t wvplFileOffset = wvpl->GetFilePos() - wvpl->GetPos(); // should be zero, but just to be sure - size_t i = 0; - for (RIFF::List* wave = wvpl->GetSubListAt(i); wave; - wave = wvpl->GetSubListAt(++i)) + size_t iWaveCk = 0; + for (RIFF::List* wave = wvpl->GetSubListAt(iWaveCk); wave; + wave = wvpl->GetSubListAt(++iWaveCk)) { if (wave->GetListType() == LIST_TYPE_WAVE) { // notify current progress @@ -6546,6 +6549,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; @@ -6553,6 +6563,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++; @@ -6699,7 +6716,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; @@ -7091,12 +7108,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). @@ -7109,9 +7124,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; } @@ -7144,7 +7160,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"); @@ -7159,7 +7175,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; @@ -7204,7 +7220,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); @@ -7311,8 +7327,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; @@ -7417,8 +7435,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(); }