diff --git a/src/Tags.cpp b/src/Tags.cpp index 591d7c4c6..4bd735f98 100644 --- a/src/Tags.cpp +++ b/src/Tags.cpp @@ -394,30 +394,9 @@ wxString Tags::GetTag(const wxString & name) return mMap[iter->second]; } -bool Tags::GetFirst(wxString & name, wxString & value) +Tags::Iterators Tags::GetRange() const { - mIter = mMap.begin(); - if (mIter == mMap.end()) { - return false; - } - - name = mIter->first; - value = mIter->second; - - return true; -} - -bool Tags::GetNext(wxString & name, wxString & value) -{ - ++mIter; - if (mIter == mMap.end()) { - return false; - } - - name = mIter->first; - value = mIter->second; - - return true; + return std::make_pair(mMap.begin(), mMap.end()); } void Tags::SetTag(const wxString & name, const wxString & value) @@ -510,8 +489,9 @@ void Tags::WriteXML(XMLWriter &xmlFile) { xmlFile.StartTag(wxT("tags")); - wxString n, v; - for (bool cont = GetFirst(n, v); cont; cont = GetNext(n, v)) { + for (const auto &pair : GetRange()) { + const auto &n = pair.first; + const auto &v = pair.second; xmlFile.StartTag(wxT("tag")); xmlFile.WriteAttr(wxT("name"), n); xmlFile.WriteAttr(wxT("value"), v); @@ -863,8 +843,6 @@ bool TagsEditor::TransferDataFromWindow() bool TagsEditor::TransferDataToWindow() { size_t i; - wxString n; - wxString v; TagMap popTagMap; // Disable redrawing until we're done @@ -895,12 +873,14 @@ bool TagsEditor::TransferDataToWindow() } // Populate the rest - for (bool cont = mLocal.GetFirst(n, v); cont; cont = mLocal.GetNext(n, v)) { - if ( popTagMap.find(n) == popTagMap.end() ) { + for (const auto &pair : mLocal.GetRange()) { + const auto &n = pair.first; + const auto &v = pair.second; + if (popTagMap.find(n) == popTagMap.end()) { mGrid->AppendRows(); mGrid->SetCellValue(i, 0, n); mGrid->SetCellValue(i, 1, v); - i++; + i++; } } @@ -1186,8 +1166,9 @@ void TagsEditor::OnSaveDefaults(wxCommandEvent & WXUNUSED(event)) gPrefs->DeleteGroup(wxT("/Tags")); // Write out each tag - wxString n, v; - for (bool cont = mLocal.GetFirst(n, v); cont; cont = mLocal.GetNext(n, v)) { + for (const auto &pair : mLocal.GetRange()) { + const auto &n = pair.first; + const auto &v = pair.second; gPrefs->Write(wxT("/Tags/") + n, v); } gPrefs->Flush(); diff --git a/src/Tags.h b/src/Tags.h index 7edb1b2e2..9373c54ec 100644 --- a/src/Tags.h +++ b/src/Tags.h @@ -33,6 +33,7 @@ #include "widgets/Grid.h" #include "xml/XMLTagHandler.h" +#include #include #include #include @@ -99,8 +100,14 @@ class AUDACITY_DLL_API Tags: public XMLTagHandler { bool HasTag(const wxString & name); wxString GetTag(const wxString & name); - bool GetFirst(wxString & name, wxString & value); - bool GetNext(wxString & name, wxString & value); + using IterPair = std::pair; + struct Iterators : public IterPair { + Iterators(IterPair p) : IterPair(p) {} + // Define begin() and end() for convenience in range-for + auto begin() -> decltype(first) const { return first; } + auto end() -> decltype(second) const { return second; } + }; + Iterators GetRange() const; void SetTag(const wxString & name, const wxString & value); void SetTag(const wxString & name, const int & value); @@ -111,7 +118,6 @@ class AUDACITY_DLL_API Tags: public XMLTagHandler { private: void LoadDefaults(); - TagMap::iterator mIter; TagMap mXref; TagMap mMap; diff --git a/src/export/ExportFLAC.cpp b/src/export/ExportFLAC.cpp index 0d62a736a..3fa6a5a13 100644 --- a/src/export/ExportFLAC.cpp +++ b/src/export/ExportFLAC.cpp @@ -388,8 +388,10 @@ bool ExportFLAC::GetMetadata(AudacityProject *project, Tags *tags) mMetadata = ::FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); - wxString n, v; - for (bool cont = tags->GetFirst(n, v); cont; cont = tags->GetNext(n, v)) { + wxString n; + for (const auto &pair : tags->GetRange()) { + n = pair.first; + const auto &v = pair.second; if (n == TAG_YEAR) { n = wxT("DATE"); } diff --git a/src/export/ExportMP2.cpp b/src/export/ExportMP2.cpp index 04eb8c3fc..b870225ae 100644 --- a/src/export/ExportMP2.cpp +++ b/src/export/ExportMP2.cpp @@ -344,8 +344,9 @@ int ExportMP2::AddTags(AudacityProject * WXUNUSED(project), char **buffer, bool #ifdef USE_LIBID3TAG struct id3_tag *tp = id3_tag_new(); - wxString n, v; - for (bool cont = tags->GetFirst(n, v); cont; cont = tags->GetNext(n, v)) { + for (const auto &pair : tags->GetRange()) { + const auto &n = pair.first; + const auto &v = pair.second; const char *name = "TXXX"; if (n.CmpNoCase(TAG_TITLE) == 0) { diff --git a/src/export/ExportMP3.cpp b/src/export/ExportMP3.cpp index c65ceb619..b2ac3c32b 100644 --- a/src/export/ExportMP3.cpp +++ b/src/export/ExportMP3.cpp @@ -1970,8 +1970,9 @@ int ExportMP3::AddTags(AudacityProject *WXUNUSED(project), char **buffer, bool * #ifdef USE_LIBID3TAG struct id3_tag *tp = id3_tag_new(); - wxString n, v; - for (bool cont = tags->GetFirst(n, v); cont; cont = tags->GetNext(n, v)) { + for (const auto &pair : tags->GetRange()) { + const auto &n = pair.first; + const auto &v = pair.second; const char *name = "TXXX"; if (n.CmpNoCase(TAG_TITLE) == 0) { diff --git a/src/export/ExportOGG.cpp b/src/export/ExportOGG.cpp index 3c2c5fdbe..b7f3c885f 100644 --- a/src/export/ExportOGG.cpp +++ b/src/export/ExportOGG.cpp @@ -349,8 +349,10 @@ bool ExportOGG::FillComment(AudacityProject *project, vorbis_comment *comment, T vorbis_comment_init(comment); - wxString n, v; - for (bool cont = metadata->GetFirst(n, v); cont; cont = metadata->GetNext(n, v)) { + wxString n; + for (const auto &pair : metadata->GetRange()) { + n = pair.first; + const auto &v = pair.second; if (n == TAG_YEAR) { n = wxT("DATE"); } diff --git a/src/export/ExportPCM.cpp b/src/export/ExportPCM.cpp index e5fdcc7a9..4c57fdfe5 100644 --- a/src/export/ExportPCM.cpp +++ b/src/export/ExportPCM.cpp @@ -746,8 +746,9 @@ void ExportPCM::AddID3Chunk(wxString fName, Tags *tags, int sf_format) #ifdef USE_LIBID3TAG struct id3_tag *tp = id3_tag_new(); - wxString n, v; - for (bool cont = tags->GetFirst(n, v); cont; cont = tags->GetNext(n, v)) { + for (const auto &pair : tags->GetRange()) { + const auto &n = pair.first; + const auto &v = pair.second; const char *name = "TXXX"; if (n.CmpNoCase(TAG_TITLE) == 0) {