diff --git a/Makefile.am b/Makefile.am index f746d25aa..1061d838d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -978,6 +978,7 @@ libtag_a_SOURCES =\ src/tag/TagHandler.cxx src/tag/TagHandler.hxx \ src/tag/Mask.hxx \ src/tag/Fallback.hxx \ + src/tag/VisitFallback.hxx \ src/tag/Settings.cxx src/tag/Settings.hxx \ src/tag/TagConfig.cxx src/tag/TagConfig.hxx \ src/tag/TagNames.c \ diff --git a/src/db/Count.cxx b/src/db/Count.cxx index 72f492ee3..ba0d443e7 100644 --- a/src/db/Count.cxx +++ b/src/db/Count.cxx @@ -25,7 +25,7 @@ #include "client/Response.hxx" #include "LightSong.hxx" #include "tag/Tag.hxx" -#include "tag/Fallback.hxx" +#include "tag/VisitFallback.hxx" #include #include @@ -73,24 +73,15 @@ stats_visitor_song(SearchStats &stats, const LightSong &song) stats.total_duration += duration; } -static bool -CollectGroupCounts(TagCountMap &map, TagType group, const Tag &tag) +static void +CollectGroupCounts(TagCountMap &map, const Tag &tag, + const char *value) noexcept { - bool found = false; - for (const auto &item : tag) { - if (item.type == group) { - auto r = map.insert(std::make_pair(item.value, - SearchStats())); - SearchStats &s = r.first->second; - ++s.n_songs; - if (!tag.duration.IsNegative()) - s.total_duration += tag.duration; - - found = true; - } - } - - return found; + auto r = map.insert(std::make_pair(value, SearchStats())); + SearchStats &s = r.first->second; + ++s.n_songs; + if (!tag.duration.IsNegative()) + s.total_duration += tag.duration; } static void @@ -99,9 +90,10 @@ GroupCountVisitor(TagCountMap &map, TagType group, const LightSong &song) assert(song.tag != nullptr); const Tag &tag = *song.tag; - ApplyTagWithFallback(group, + VisitTagWithFallback(tag, group, std::bind(CollectGroupCounts, std::ref(map), - std::placeholders::_1, std::cref(tag))); + std::cref(tag), + std::placeholders::_1)); } void diff --git a/src/tag/VisitFallback.hxx b/src/tag/VisitFallback.hxx new file mode 100644 index 000000000..3ad486f5f --- /dev/null +++ b/src/tag/VisitFallback.hxx @@ -0,0 +1,52 @@ +/* + * Copyright 2003-2018 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_TAG_VISIT_FALLBACK_HXX +#define MPD_TAG_VISIT_FALLBACK_HXX + +#include "Fallback.hxx" +#include "Tag.hxx" + +template +bool +VisitTagType(const Tag &tag, TagType type, F &&f) noexcept +{ + bool found = false; + + for (const auto &item : tag) { + if (item.type == type) { + found = true; + f(item.value); + } + } + + return found; +} + +template +bool +VisitTagWithFallback(const Tag &tag, TagType type, F &&f) noexcept +{ + return ApplyTagWithFallback(type, + [&](TagType type2) { + return VisitTagType(tag, type2, f); + }); +} + +#endif