From a22bad7ad5509b236ba1ff7e30bb6af96a2f4a15 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Mon, 27 Mar 2017 13:08:27 +0200 Subject: [PATCH] Rename SongTagMap to TagVector and allow it to store regular string chunks --- src/format.h | 15 +++++++++++++-- src/format_impl.h | 37 ++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/format.h b/src/format.h index 03dd121..21f8804 100644 --- a/src/format.h +++ b/src/format.h @@ -60,10 +60,18 @@ private: unsigned m_delimiter; }; +inline bool operator==(const SongTag &lhs, const SongTag &rhs) { + return lhs.function() == rhs.function() + && lhs.delimiter() == rhs.delimiter(); +} +inline bool operator!=(const SongTag &lhs, const SongTag &rhs) { + return !(lhs == rhs); +} + template -using SongTagMap = std::vector< +using TagVector = std::vector< std::pair< - SongTag, + boost::optional, std::basic_string > >; @@ -112,6 +120,9 @@ void print(const AST &ast, NC::BasicBuffer &buffer, template std::basic_string stringify(const AST &ast, const MPD::Song *song); +template +TagVector flatten(const AST &ast, const MPD::Song &song); + AST parse(const std::string &s, const unsigned flags = Flags::All); AST parse(const std::wstring &ws, const unsigned flags = Flags::All); diff --git a/src/format_impl.h b/src/format_impl.h index 400277e..7655919 100644 --- a/src/format_impl.h +++ b/src/format_impl.h @@ -125,7 +125,8 @@ struct Printer: boost::static_visitor if (st.delimiter() > 0) { // shorten date/length by simple truncation - if (st.function() == &MPD::Song::getDate || st.function() == &MPD::Song::getLength) + if (st.function() == &MPD::Song::getDate + || st.function() == &MPD::Song::getLength) tags.resize(st.delimiter()); else tags = wideShorten(tags, st.delimiter()); @@ -137,7 +138,8 @@ struct Printer: boost::static_visitor return Result::Missing; } - // If all Empty -> Empty, if any Ok -> continue with Ok, if any Missing -> stop with Empty. + // If all Empty -> Empty, if any Ok -> continue with Ok, if any Missing -> + // stop with Empty. Result operator()(const Group &group) { auto visit = [this, &group] { @@ -196,8 +198,8 @@ private: result += s; } }; - // when writing to a string, we should ignore all other - // properties. if this code is reached, throw an exception. + // When writing to a string, we should ignore all other properties. If this + // code is reached, throw an exception. template struct output_> { static void exec(std::basic_string &, const ValueT &, const SongTag *) { @@ -205,26 +207,27 @@ private: } }; - // Specialization for SongTagMap. + // Specialization for TagVector. template - struct output_, SongTagMap > { + struct output_, TagVector > { // Compile only if string types are the same. static typename std::enable_if< std::is_same::value, void - >::type exec(SongTagMap &acc, + >::type exec(TagVector &acc, const std::basic_string &s, const SongTag *st) { - if (st != nullptr) { + if (st != nullptr) acc.emplace_back(*st, s); - } + else + acc.emplace_back(boost::none, s); } }; - // When extracting tags from a song all the other properties should - // be ignored. If that's not the case, throw an exception. + // When extracting tags from a song all the other properties should be + // ignored. If that's not the case, throw an exception. template - struct output_ > { - static void exec(SongTagMap &, const ValueT &, const SongTag *) { - throw std::logic_error("Non-string property can't be inserted into the SongTagMap"); + struct output_ > { + static void exec(TagVector &, const ValueT &, const SongTag *) { + throw std::logic_error("Non-string property can't be inserted into the TagVector"); } }; @@ -283,10 +286,10 @@ std::basic_string stringify(const AST &ast, const MPD::Song *song) } template -SongTagMap extractTags(const AST &ast, const MPD::Song &song) +TagVector flatten(const AST &ast, const MPD::Song &song) { - SongTagMap result; - Printer > printer(result, &song, &result, Flags::Tag); + TagVector result; + Printer> printer(result, &song, &result, Flags::Tag); visit(printer, ast); return result; } -- 2.11.4.GIT