From b3c0c0798e380e527882ffb29faca2ccfeb188f6 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 13 Jun 2015 16:10:26 +0200 Subject: [PATCH] helpers: restore old behavior when playing a song already in playlist --- src/helpers.cpp | 12 ++++++++++-- src/utility/functional.h | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/helpers.cpp b/src/helpers.cpp index b233b61d..52988025 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -103,12 +103,20 @@ bool addSongToPlaylist(const MPD::Song &s, bool play, int position) { bool result = false; if (Config.space_add_mode == SpaceAddMode::AddRemove - && !play && myPlaylist->checkForSong(s) ) { result = true; - removeSongFromPlaylist(myPlaylist->main(), s); + if (play) + { + const auto begin = myPlaylist->main().beginV(), end = myPlaylist->main().endV(); + auto it = find_map_first(begin, end, s, [](const MPD::Song &found) { + Mpd.PlayID(found.getID()); + }); + assert(it != end); + } + else + removeSongFromPlaylist(myPlaylist->main(), s); return result; } int id = Mpd.AddSong(s, position); diff --git a/src/utility/functional.h b/src/utility/functional.h index 54480edd..d2b1a08d 100644 --- a/src/utility/functional.h +++ b/src/utility/functional.h @@ -61,6 +61,26 @@ struct pointer_extractor ValueT *operator()(ValueT &v) const { return &v; } }; +/// Map over the first element in range satisfying the predicate. +template +InputIterator find_map_first(InputIterator first, InputIterator last, PredicateT &&p, MapT &&f) +{ + auto it = std::find(first, last, std::forward(p)); + if (it != last) + f(*it); + return it; +} + +/// Map over all elements in range satisfying the predicate. +template +void find_map_all(InputIterator first, InputIterator last, PredicateT &&p, MapT &&f) +{ + InputIterator it = first; + do + it = find_map_first(it, last, p, f); + while (it != last); +} + // identity function object struct id_ {