Commit 6c06244e authored by Max Kellermann's avatar Max Kellermann

db/Count: move code to tag/VisitCallback.hxx

parent 53448e46
...@@ -978,6 +978,7 @@ libtag_a_SOURCES =\ ...@@ -978,6 +978,7 @@ libtag_a_SOURCES =\
src/tag/TagHandler.cxx src/tag/TagHandler.hxx \ src/tag/TagHandler.cxx src/tag/TagHandler.hxx \
src/tag/Mask.hxx \ src/tag/Mask.hxx \
src/tag/Fallback.hxx \ src/tag/Fallback.hxx \
src/tag/VisitFallback.hxx \
src/tag/Settings.cxx src/tag/Settings.hxx \ src/tag/Settings.cxx src/tag/Settings.hxx \
src/tag/TagConfig.cxx src/tag/TagConfig.hxx \ src/tag/TagConfig.cxx src/tag/TagConfig.hxx \
src/tag/TagNames.c \ src/tag/TagNames.c \
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include "client/Response.hxx" #include "client/Response.hxx"
#include "LightSong.hxx" #include "LightSong.hxx"
#include "tag/Tag.hxx" #include "tag/Tag.hxx"
#include "tag/Fallback.hxx" #include "tag/VisitFallback.hxx"
#include <functional> #include <functional>
#include <map> #include <map>
...@@ -73,24 +73,15 @@ stats_visitor_song(SearchStats &stats, const LightSong &song) ...@@ -73,24 +73,15 @@ stats_visitor_song(SearchStats &stats, const LightSong &song)
stats.total_duration += duration; stats.total_duration += duration;
} }
static bool static void
CollectGroupCounts(TagCountMap &map, TagType group, const Tag &tag) CollectGroupCounts(TagCountMap &map, const Tag &tag,
const char *value) noexcept
{ {
bool found = false; auto r = map.insert(std::make_pair(value, SearchStats()));
for (const auto &item : tag) { SearchStats &s = r.first->second;
if (item.type == group) { ++s.n_songs;
auto r = map.insert(std::make_pair(item.value, if (!tag.duration.IsNegative())
SearchStats())); s.total_duration += tag.duration;
SearchStats &s = r.first->second;
++s.n_songs;
if (!tag.duration.IsNegative())
s.total_duration += tag.duration;
found = true;
}
}
return found;
} }
static void static void
...@@ -99,9 +90,10 @@ GroupCountVisitor(TagCountMap &map, TagType group, const LightSong &song) ...@@ -99,9 +90,10 @@ GroupCountVisitor(TagCountMap &map, TagType group, const LightSong &song)
assert(song.tag != nullptr); assert(song.tag != nullptr);
const Tag &tag = *song.tag; const Tag &tag = *song.tag;
ApplyTagWithFallback(group, VisitTagWithFallback(tag, group,
std::bind(CollectGroupCounts, std::ref(map), std::bind(CollectGroupCounts, std::ref(map),
std::placeholders::_1, std::cref(tag))); std::cref(tag),
std::placeholders::_1));
} }
void void
......
/*
* 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<typename F>
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<typename F>
bool
VisitTagWithFallback(const Tag &tag, TagType type, F &&f) noexcept
{
return ApplyTagWithFallback(type,
[&](TagType type2) {
return VisitTagType(tag, type2, f);
});
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment