1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_DATABASE_VISITOR_HELPER_HXX
21 #define MPD_DATABASE_VISITOR_HELPER_HXX
22 
23 #include "Visitor.hxx"
24 #include "Selection.hxx"
25 
26 #include <vector>
27 
28 class DetachedSong;
29 
30 /**
31  * This class helps implementing Database::Visit() by emulating
32  * #DatabaseSelection features that the #Database implementation
33  * doesn't have, e.g. filtering, sorting and window.
34  *
35  * To use this class, construct it, passing unsupported features and
36  * the visitor callback to the constructor; before leaving Visit(),
37  * call Commit() (unless an error has occurred).
38  */
39 class DatabaseVisitorHelper {
40 	const DatabaseSelection selection;
41 
42 	/**
43 	 * If the plugin can't sort, then this container will collect
44 	 * all songs, sort them and report them to the visitor in
45 	 * Commit().
46 	 */
47 	std::vector<DetachedSong> songs;
48 
49 	VisitSong original_visit_song;
50 
51 	/**
52 	 * Used to emulate the "window".
53 	 */
54 	unsigned counter = 0;
55 
56 public:
57 	/**
58 	 * @param selection a #DatabaseSelection instance with only
59 	 * features enabled which shall be emulated by this class
60 	 * @param visit_song the callback function passed to
61 	 * Database::Visit(); may be replaced by this class
62 	 */
63 	DatabaseVisitorHelper(DatabaseSelection selection,
64 			      VisitSong &visit_song) noexcept;
65 	~DatabaseVisitorHelper() noexcept;
66 
67 	void Commit();
68 };
69 
70 #endif
71