Commit db27bb76 authored by Max Kellermann's avatar Max Kellermann

db: fix broken command "list ... group"

Grouping in the "list" command was completely broken from the start, unlike "count group". I have no idea what I have been thinking when I wrote commit ae178c77, but it didn't make any sense. This commit is a rewrite of the feature. For clients to be able to detect this feature, this commit also increments the protocol version.
parent 7cfe929c
...@@ -985,7 +985,6 @@ libtag_a_SOURCES =\ ...@@ -985,7 +985,6 @@ libtag_a_SOURCES =\
src/tag/TagString.cxx src/tag/TagString.hxx \ src/tag/TagString.cxx src/tag/TagString.hxx \
src/tag/TagPool.cxx src/tag/TagPool.hxx \ src/tag/TagPool.cxx src/tag/TagPool.hxx \
src/tag/TagTable.cxx src/tag/TagTable.hxx \ src/tag/TagTable.cxx src/tag/TagTable.hxx \
src/tag/Set.cxx src/tag/Set.hxx \
src/tag/Format.cxx src/tag/Format.hxx \ src/tag/Format.cxx src/tag/Format.hxx \
src/tag/VorbisComment.cxx src/tag/VorbisComment.hxx \ src/tag/VorbisComment.cxx src/tag/VorbisComment.hxx \
src/tag/ReplayGain.cxx src/tag/ReplayGain.hxx \ src/tag/ReplayGain.cxx src/tag/ReplayGain.hxx \
......
...@@ -2,6 +2,7 @@ ver 0.20.22 (not yet released) ...@@ -2,6 +2,7 @@ ver 0.20.22 (not yet released)
* protocol * protocol
- add tag fallbacks for AlbumArtistSort, ArtistSort - add tag fallbacks for AlbumArtistSort, ArtistSort
- "count group ..." can print an empty group - "count group ..." can print an empty group
- fix broken command "list ... group"
* storage * storage
- curl: URL-encode paths - curl: URL-encode paths
* Android * Android
......
...@@ -14,7 +14,7 @@ AM_SILENT_RULES ...@@ -14,7 +14,7 @@ AM_SILENT_RULES
AC_CONFIG_HEADERS(config.h) AC_CONFIG_HEADERS(config.h)
AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_MACRO_DIR([m4])
AC_DEFINE(PROTOCOL_VERSION, "0.20.0", [The MPD protocol version]) AC_DEFINE(PROTOCOL_VERSION, "0.20.22", [The MPD protocol version])
GIT_COMMIT=`cd "$srcdir" && git describe --dirty --always 2>/dev/null` GIT_COMMIT=`cd "$srcdir" && git describe --dirty --always 2>/dev/null`
if test x$GIT_COMMIT != x; then if test x$GIT_COMMIT != x; then
......
...@@ -191,7 +191,7 @@ handle_list(Client &client, Request args, Response &r) ...@@ -191,7 +191,7 @@ handle_list(Client &client, Request args, Response &r)
} }
std::unique_ptr<SongFilter> filter; std::unique_ptr<SongFilter> filter;
tag_mask_t group_mask = 0; TagType group = TAG_NUM_OF_ITEM_TYPES;
if (args.size == 1) { if (args.size == 1) {
/* for compatibility with < 0.12.0 */ /* for compatibility with < 0.12.0 */
...@@ -206,18 +206,16 @@ handle_list(Client &client, Request args, Response &r) ...@@ -206,18 +206,16 @@ handle_list(Client &client, Request args, Response &r)
args.shift())); args.shift()));
} }
while (args.size >= 2 && if (args.size >= 2 &&
StringIsEqual(args[args.size - 2], "group")) { StringIsEqual(args[args.size - 2], "group")) {
const char *s = args[args.size - 1]; const char *s = args[args.size - 1];
TagType gt = tag_name_parse_i(s); group = tag_name_parse_i(s);
if (gt == TAG_NUM_OF_ITEM_TYPES) { if (group == TAG_NUM_OF_ITEM_TYPES) {
r.FormatError(ACK_ERROR_ARG, r.FormatError(ACK_ERROR_ARG,
"Unknown tag type: %s", s); "Unknown tag type: %s", s);
return CommandResult::ERROR; return CommandResult::ERROR;
} }
group_mask |= tag_mask_t(1) << unsigned(gt);
args.pop_back(); args.pop_back();
args.pop_back(); args.pop_back();
} }
...@@ -230,14 +228,13 @@ handle_list(Client &client, Request args, Response &r) ...@@ -230,14 +228,13 @@ handle_list(Client &client, Request args, Response &r)
} }
} }
if (tagType < TAG_NUM_OF_ITEM_TYPES && if (tagType < TAG_NUM_OF_ITEM_TYPES && tagType == group) {
group_mask & (tag_mask_t(1) << tagType)) {
r.Error(ACK_ERROR_ARG, "Conflicting group"); r.Error(ACK_ERROR_ARG, "Conflicting group");
return CommandResult::ERROR; return CommandResult::ERROR;
} }
PrintUniqueTags(r, client.partition, PrintUniqueTags(r, client.partition,
tagType, group_mask, filter.get()); tagType, group, filter.get());
return CommandResult::OK; return CommandResult::OK;
} }
......
...@@ -187,22 +187,34 @@ PrintSongURIVisitor(Response &r, Partition &partition, const LightSong &song) ...@@ -187,22 +187,34 @@ PrintSongURIVisitor(Response &r, Partition &partition, const LightSong &song)
} }
static void static void
PrintUniqueTag(Response &r, TagType tag_type, PrintUniqueTags(Response &r, TagType tag_type,
const Tag &tag) const std::set<std::string> &values)
{ {
const char *value = tag.GetValue(tag_type); const char *const name = tag_item_names[tag_type];
assert(value != nullptr); for (const auto &i : values)
r.Format("%s: %s\n", tag_item_names[tag_type], value); r.Format("%s: %s\n", name, i.c_str());
}
for (const auto &item : tag) static void
if (item.type != tag_type) PrintGroupedUniqueTags(Response &r, TagType tag_type, TagType group,
r.Format("%s: %s\n", const std::map<std::string, std::set<std::string>> &groups)
tag_item_names[item.type], item.value); {
if (group == TAG_NUM_OF_ITEM_TYPES) {
for (const auto &i : groups)
PrintUniqueTags(r, tag_type, i.second);
return;
}
const char *const group_name = tag_item_names[group];
for (const auto &i : groups) {
r.Format("%s: %s\n", group_name, i.first.c_str());
PrintUniqueTags(r, tag_type, i.second);
}
} }
void void
PrintUniqueTags(Response &r, Partition &partition, PrintUniqueTags(Response &r, Partition &partition,
unsigned type, tag_mask_t group_mask, unsigned type, TagType group,
const SongFilter *filter) const SongFilter *filter)
{ {
const Database &db = partition.GetDatabaseOrThrow(); const Database &db = partition.GetDatabaseOrThrow();
...@@ -217,10 +229,9 @@ PrintUniqueTags(Response &r, Partition &partition, ...@@ -217,10 +229,9 @@ PrintUniqueTags(Response &r, Partition &partition,
} else { } else {
assert(type < TAG_NUM_OF_ITEM_TYPES); assert(type < TAG_NUM_OF_ITEM_TYPES);
using namespace std::placeholders; PrintGroupedUniqueTags(r, TagType(type), group,
const auto f = std::bind(PrintUniqueTag, std::ref(r), db.CollectUniqueTags(selection,
(TagType)type, _1); TagType(type),
db.VisitUniqueTags(selection, (TagType)type, group));
group_mask, f);
} }
} }
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#ifndef MPD_DB_PRINT_H #ifndef MPD_DB_PRINT_H
#define MPD_DB_PRINT_H #define MPD_DB_PRINT_H
#include "tag/Mask.hxx" #include "tag/TagType.h"
class SongFilter; class SongFilter;
struct DatabaseSelection; struct DatabaseSelection;
...@@ -44,7 +44,7 @@ db_selection_print(Response &r, Partition &partition, ...@@ -44,7 +44,7 @@ db_selection_print(Response &r, Partition &partition,
void void
PrintUniqueTags(Response &r, Partition &partition, PrintUniqueTags(Response &r, Partition &partition,
unsigned type, tag_mask_t group_mask, unsigned type, TagType group,
const SongFilter *filter); const SongFilter *filter);
#endif #endif
...@@ -22,9 +22,12 @@ ...@@ -22,9 +22,12 @@
#include "Visitor.hxx" #include "Visitor.hxx"
#include "tag/TagType.h" #include "tag/TagType.h"
#include "tag/Mask.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <map>
#include <set>
#include <string>
#include <time.h> #include <time.h>
struct DatabasePlugin; struct DatabasePlugin;
...@@ -99,12 +102,9 @@ public: ...@@ -99,12 +102,9 @@ public:
return Visit(selection, VisitDirectory(), visit_song); return Visit(selection, VisitDirectory(), visit_song);
} }
/** virtual std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
* Visit all unique tag values. TagType tag_type,
*/ TagType group=TAG_NUM_OF_ITEM_TYPES) const = 0;
virtual void VisitUniqueTags(const DatabaseSelection &selection,
TagType tag_type, tag_mask_t group_mask,
VisitTag visit_tag) const = 0;
virtual DatabaseStats GetStats(const DatabaseSelection &selection) const = 0; virtual DatabaseStats GetStats(const DatabaseSelection &selection) const = 0;
......
...@@ -20,34 +20,42 @@ ...@@ -20,34 +20,42 @@
#include "UniqueTags.hxx" #include "UniqueTags.hxx"
#include "Interface.hxx" #include "Interface.hxx"
#include "LightSong.hxx" #include "LightSong.hxx"
#include "tag/Set.hxx" #include "tag/VisitFallback.hxx"
#include <functional> #include <functional>
#include <assert.h> #include <assert.h>
static void static void
CollectTags(TagSet &set, TagType tag_type, tag_mask_t group_mask, CollectTags(std::set<std::string> &result,
const LightSong &song) const Tag &tag,
TagType tag_type) noexcept
{ {
assert(song.tag != nullptr); VisitTagWithFallbackOrEmpty(tag, tag_type, [&result](const char *value){
const Tag &tag = *song.tag; result.emplace(value);
});
}
set.InsertUnique(tag, tag_type, group_mask); static void
CollectGroupTags(std::map<std::string, std::set<std::string>> &result,
const Tag &tag,
TagType tag_type,
TagType group) noexcept
{
VisitTagWithFallbackOrEmpty(tag, group, [&](const char *group_name){
CollectTags(result[group_name], tag, tag_type);
});
} }
void std::map<std::string, std::set<std::string>>
VisitUniqueTags(const Database &db, const DatabaseSelection &selection, CollectUniqueTags(const Database &db, const DatabaseSelection &selection,
TagType tag_type, tag_mask_t group_mask, TagType tag_type, TagType group)
VisitTag visit_tag)
{ {
TagSet set; std::map<std::string, std::set<std::string>> result;
using namespace std::placeholders; db.Visit(selection, [&result, tag_type, group](const LightSong &song){
const auto f = std::bind(CollectTags, std::ref(set), CollectGroupTags(result, *song.tag, tag_type, group);
tag_type, group_mask, _1); });
db.Visit(selection, f);
for (const auto &value : set) return result;
visit_tag(value);
} }
...@@ -20,16 +20,19 @@ ...@@ -20,16 +20,19 @@
#ifndef MPD_DB_UNIQUE_TAGS_HXX #ifndef MPD_DB_UNIQUE_TAGS_HXX
#define MPD_DB_UNIQUE_TAGS_HXX #define MPD_DB_UNIQUE_TAGS_HXX
#include "Visitor.hxx"
#include "tag/TagType.h" #include "tag/TagType.h"
#include "tag/Mask.hxx" #include "Compiler.h"
#include <map>
#include <set>
#include <string>
class Database; class Database;
struct DatabaseSelection; struct DatabaseSelection;
void gcc_pure
VisitUniqueTags(const Database &db, const DatabaseSelection &selection, std::map<std::string, std::set<std::string>>
TagType tag_type, tag_mask_t group_mask, CollectUniqueTags(const Database &db, const DatabaseSelection &selection,
VisitTag visit_tag); TagType tag_type, TagType group);
#endif #endif
...@@ -120,9 +120,9 @@ public: ...@@ -120,9 +120,9 @@ public:
VisitSong visit_song, VisitSong visit_song,
VisitPlaylist visit_playlist) const override; VisitPlaylist visit_playlist) const override;
void VisitUniqueTags(const DatabaseSelection &selection, std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type, tag_mask_t group_mask, TagType tag_type,
VisitTag visit_tag) const override; TagType group) const override;
DatabaseStats GetStats(const DatabaseSelection &selection) const override; DatabaseStats GetStats(const DatabaseSelection &selection) const override;
...@@ -334,28 +334,19 @@ SendConstraints(mpd_connection *connection, const DatabaseSelection &selection) ...@@ -334,28 +334,19 @@ SendConstraints(mpd_connection *connection, const DatabaseSelection &selection)
} }
static bool static bool
SendGroupMask(mpd_connection *connection, tag_mask_t mask) SendGroup(mpd_connection *connection, TagType group)
{ {
#if LIBMPDCLIENT_CHECK_VERSION(2,12,0) if (group == TAG_NUM_OF_ITEM_TYPES)
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) { return true;
if ((mask & (tag_mask_t(1) << i)) == 0)
continue;
const auto tag = Convert(TagType(i));
if (tag == MPD_TAG_COUNT)
throw std::runtime_error("Unsupported tag");
if (!mpd_search_add_group_tag(connection, tag)) #if LIBMPDCLIENT_CHECK_VERSION(2,12,0)
return false; const auto tag = Convert(group);
} if (tag == MPD_TAG_COUNT)
throw std::runtime_error("Unsupported tag");
return true; return mpd_search_add_group_tag(connection, tag);
#else #else
(void)connection; (void)connection;
(void)mask;
if (mask != 0)
throw std::runtime_error("Grouping requires libmpdclient 2.12");
return true; return true;
#endif #endif
...@@ -799,11 +790,9 @@ ProxyDatabase::Visit(const DatabaseSelection &selection, ...@@ -799,11 +790,9 @@ ProxyDatabase::Visit(const DatabaseSelection &selection,
visit_directory, visit_song, visit_playlist); visit_directory, visit_song, visit_playlist);
} }
void std::map<std::string, std::set<std::string>>
ProxyDatabase::VisitUniqueTags(const DatabaseSelection &selection, ProxyDatabase::CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type, TagType tag_type, TagType group) const
tag_mask_t group_mask,
VisitTag visit_tag) const
try { try {
// TODO: eliminate the const_cast // TODO: eliminate the const_cast
const_cast<ProxyDatabase *>(this)->EnsureConnected(); const_cast<ProxyDatabase *>(this)->EnsureConnected();
...@@ -814,54 +803,56 @@ try { ...@@ -814,54 +803,56 @@ try {
if (!mpd_search_db_tags(connection, tag_type2) || if (!mpd_search_db_tags(connection, tag_type2) ||
!SendConstraints(connection, selection) || !SendConstraints(connection, selection) ||
!SendGroupMask(connection, group_mask)) !SendGroup(connection, group))
ThrowError(connection); ThrowError(connection);
if (!mpd_search_commit(connection)) if (!mpd_search_commit(connection))
ThrowError(connection); ThrowError(connection);
TagBuilder builder; std::map<std::string, std::set<std::string>> result;
while (auto *pair = mpd_recv_pair(connection)) { if (group == TAG_NUM_OF_ITEM_TYPES) {
AtScopeExit(this, pair) { auto &values = result[std::string()];
mpd_return_pair(connection, pair);
};
const auto current_type = tag_name_parse_i(pair->name); while (auto *pair = mpd_recv_pair(connection)) {
if (current_type == TAG_NUM_OF_ITEM_TYPES) AtScopeExit(this, pair) {
continue; mpd_return_pair(connection, pair);
};
if (current_type == tag_type && !builder.IsEmpty()) { const auto current_type = tag_name_parse_i(pair->name);
try { if (current_type == TAG_NUM_OF_ITEM_TYPES)
visit_tag(builder.Commit()); continue;
} catch (...) {
mpd_response_finish(connection); if (current_type == tag_type)
throw; values.emplace(pair->value);
}
} }
} else {
std::set<std::string> *current_group = nullptr;
builder.AddItem(current_type, pair->value); while (auto *pair = mpd_recv_pair(connection)) {
AtScopeExit(this, pair) {
mpd_return_pair(connection, pair);
};
if (!builder.HasType(current_type)) const auto current_type = tag_name_parse_i(pair->name);
/* if no tag item has been added, then the if (current_type == TAG_NUM_OF_ITEM_TYPES)
given value was not acceptable continue;
(e.g. empty); forcefully insert an empty
tag in this case, as the caller expects the
given tag type to be present */
builder.AddEmptyItem(current_type);
}
if (!builder.IsEmpty()) { if (current_type == tag_type) {
try { if (current_group == nullptr)
visit_tag(builder.Commit()); current_group = &result[std::string()];
} catch (...) {
mpd_response_finish(connection); current_group->emplace(pair->value);
throw; } else if (current_type == group) {
current_group = &result[pair->value];
}
} }
} }
if (!mpd_response_finish(connection)) if (!mpd_response_finish(connection))
ThrowError(connection); ThrowError(connection);
return result;
} catch (...) { } catch (...) {
if (connection != nullptr) if (connection != nullptr)
mpd_search_cancel(connection); mpd_search_cancel(connection);
......
...@@ -312,12 +312,11 @@ SimpleDatabase::Visit(const DatabaseSelection &selection, ...@@ -312,12 +312,11 @@ SimpleDatabase::Visit(const DatabaseSelection &selection,
"No such directory"); "No such directory");
} }
void std::map<std::string, std::set<std::string>>
SimpleDatabase::VisitUniqueTags(const DatabaseSelection &selection, SimpleDatabase::CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type, tag_mask_t group_mask, TagType tag_type, TagType group) const
VisitTag visit_tag) const
{ {
::VisitUniqueTags(*this, selection, tag_type, group_mask, visit_tag); return ::CollectUniqueTags(*this, selection, tag_type, group);
} }
DatabaseStats DatabaseStats
......
...@@ -119,9 +119,9 @@ public: ...@@ -119,9 +119,9 @@ public:
VisitSong visit_song, VisitSong visit_song,
VisitPlaylist visit_playlist) const override; VisitPlaylist visit_playlist) const override;
void VisitUniqueTags(const DatabaseSelection &selection, std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type, tag_mask_t group_mask, TagType tag_type,
VisitTag visit_tag) const override; TagType group) const override;
DatabaseStats GetStats(const DatabaseSelection &selection) const override; DatabaseStats GetStats(const DatabaseSelection &selection) const override;
......
...@@ -87,9 +87,9 @@ public: ...@@ -87,9 +87,9 @@ public:
VisitSong visit_song, VisitSong visit_song,
VisitPlaylist visit_playlist) const override; VisitPlaylist visit_playlist) const override;
void VisitUniqueTags(const DatabaseSelection &selection, std::map<std::string, std::set<std::string>> CollectUniqueTags(const DatabaseSelection &selection,
TagType tag_type, tag_mask_t group_mask, TagType tag_type,
VisitTag visit_tag) const override; TagType group) const override;
DatabaseStats GetStats(const DatabaseSelection &selection) const override; DatabaseStats GetStats(const DatabaseSelection &selection) const override;
...@@ -603,17 +603,15 @@ UpnpDatabase::Visit(const DatabaseSelection &selection, ...@@ -603,17 +603,15 @@ UpnpDatabase::Visit(const DatabaseSelection &selection,
visit_directory, visit_song, visit_playlist); visit_directory, visit_song, visit_playlist);
} }
void std::map<std::string, std::set<std::string>>
UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection, UpnpDatabase::CollectUniqueTags(const DatabaseSelection &selection,
TagType tag, gcc_unused tag_mask_t group_mask, TagType tag, TagType group) const
VisitTag visit_tag) const
{ {
// TODO: use group_mask (void)group; // TODO: use group
if (!visit_tag) std::map<std::string, std::set<std::string>> result;
return; auto &values = result[std::string()];
std::set<std::string> values;
for (auto& server : discovery->GetDirectories()) { for (auto& server : discovery->GetDirectories()) {
const auto dirbuf = SearchSongs(server, rootid, selection); const auto dirbuf = SearchSongs(server, rootid, selection);
...@@ -633,11 +631,7 @@ UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection, ...@@ -633,11 +631,7 @@ UpnpDatabase::VisitUniqueTags(const DatabaseSelection &selection,
} }
} }
for (const auto& value : values) { return result;
TagBuilder builder;
builder.AddItem(tag, value.c_str());
visit_tag(builder.Commit());
}
} }
DatabaseStats DatabaseStats
......
/*
* Copyright 2003-2017 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.
*/
#include "Set.hxx"
#include "Fallback.hxx"
#include "TagBuilder.hxx"
#include "util/StringView.hxx"
#include <functional>
#include <assert.h>
/**
* Copy all tag items of the specified type.
*/
static bool
CopyTagItem2(TagBuilder &dest, TagType dest_type,
const Tag &src, TagType src_type)
{
bool found = false;
for (const auto &item : src) {
if (item.type == src_type) {
dest.AddItem(dest_type, item.value);
found = true;
}
}
return found;
}
/**
* Copy all tag items of the specified type. Fall back to "Artist" if
* there is no "AlbumArtist".
*/
static void
CopyTagItem(TagBuilder &dest, const Tag &src, TagType type)
{
ApplyTagWithFallback(type,
std::bind(CopyTagItem2, std::ref(dest), type,
std::cref(src), std::placeholders::_1));
}
/**
* Copy all tag items of the types in the mask.
*/
static void
CopyTagMask(TagBuilder &dest, const Tag &src, tag_mask_t mask)
{
for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
if ((mask & (tag_mask_t(1) << i)) != 0)
CopyTagItem(dest, src, TagType(i));
}
void
TagSet::InsertUnique(const Tag &src, TagType type, const char *value,
tag_mask_t group_mask) noexcept
{
TagBuilder builder;
builder.AddItemUnchecked(type, value);
CopyTagMask(builder, src, group_mask);
#if CLANG_OR_GCC_VERSION(4,8)
emplace(builder.Commit());
#else
insert(builder.Commit());
#endif
}
bool
TagSet::CheckUnique(TagType dest_type,
const Tag &tag, TagType src_type,
tag_mask_t group_mask) noexcept
{
bool found = false;
for (const auto &item : tag) {
if (item.type == src_type) {
InsertUnique(tag, dest_type, item.value, group_mask);
found = true;
}
}
return found;
}
void
TagSet::InsertUnique(const Tag &tag,
TagType type, tag_mask_t group_mask) noexcept
{
static_assert(sizeof(group_mask) * 8 >= TAG_NUM_OF_ITEM_TYPES,
"Mask is too small");
assert((group_mask & (tag_mask_t(1) << unsigned(type))) == 0);
if (!ApplyTagWithFallback(type,
std::bind(&TagSet::CheckUnique, this,
type, std::cref(tag),
std::placeholders::_1,
group_mask)))
InsertUnique(tag, type, "", group_mask);
}
/*
* Copyright 2003-2017 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_SET_HXX
#define MPD_TAG_SET_HXX
#include "Compiler.h"
#include "Tag.hxx"
#include "Mask.hxx"
#include <set>
#include <string.h>
/**
* Helper class for #TagSet which compares two #Tag objects.
*/
struct TagLess {
gcc_pure
bool operator()(const Tag &a, const Tag &b) const noexcept {
if (a.num_items != b.num_items)
return a.num_items < b.num_items;
const unsigned n = a.num_items;
for (unsigned i = 0; i < n; ++i) {
const TagItem &ai = *a.items[i];
const TagItem &bi = *b.items[i];
if (ai.type != bi.type)
return unsigned(ai.type) < unsigned(bi.type);
const int cmp = strcmp(ai.value, bi.value);
if (cmp != 0)
return cmp < 0;
}
return false;
}
};
/**
* A set of #Tag objects.
*/
class TagSet : public std::set<Tag, TagLess> {
public:
void InsertUnique(const Tag &tag,
TagType type, tag_mask_t group_mask) noexcept;
private:
void InsertUnique(const Tag &src, TagType type, const char *value,
tag_mask_t group_mask) noexcept;
bool CheckUnique(TagType dest_type,
const Tag &tag, TagType src_type,
tag_mask_t group_mask) noexcept;
};
#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