Filter.hxx 2.66 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21
#ifndef MPD_SONG_FILTER_HXX
#define MPD_SONG_FILTER_HXX
22

23
#include "AndSongFilter.hxx"
24

25
#include <cstdint>
26
#include <string>
27
#include <string_view>
28

29 30 31 32 33
/**
 * Special value for the db_selection_print() sort parameter.
 */
#define SORT_TAG_LAST_MODIFIED (TAG_NUM_OF_ITEM_TYPES + 3)

34
template<typename T> struct ConstBuffer;
35
enum TagType : uint8_t;
36
struct LightSong;
37 38 39 40

class SongFilter {
	AndSongFilter and_filter;

41 42 43
public:
	SongFilter() = default;

44
	SongFilter(TagType tag, const char *value, bool fold_case=false);
45 46 47

	~SongFilter();

48 49 50
	SongFilter(SongFilter &&) = default;
	SongFilter &operator=(SongFilter &&) = default;

51 52 53 54 55 56
	/**
	 * Convert this object into an "expression".  This is
	 * only useful for debugging.
	 */
	std::string ToExpression() const noexcept;

57
private:
58
	static ISongFilterPtr ParseExpression(const char *&s, bool fold_case=false);
59

60
	void Parse(const char *tag, const char *value, bool fold_case=false);
61

62
public:
63 64 65 66
	/**
	 * Throws on error.
	 */
	void Parse(ConstBuffer<const char *> args, bool fold_case=false);
67

68 69
	void Optimize() noexcept;

70
	[[gnu::pure]]
71
	bool Match(const LightSong &song) const noexcept;
72

73
	const auto &GetItems() const noexcept {
74
		return and_filter.GetItems();
75
	}
76

77
	[[gnu::pure]]
78
	bool IsEmpty() const noexcept {
79
		return and_filter.IsEmpty();
80 81
	}

82 83 84
	/**
	 * Is there at least one item with "fold case" enabled?
	 */
85
	[[gnu::pure]]
86
	bool HasFoldCase() const noexcept;
87

88 89 90
	/**
	 * Does this filter contain constraints other than "base"?
	 */
91
	[[gnu::pure]]
92
	bool HasOtherThanBase() const noexcept;
93

94
	/**
95 96
	 * Returns the "base" specification (if there is one) or
	 * nullptr.
97
	 */
98
	[[gnu::pure]]
99
	const char *GetBase() const noexcept;
100 101 102 103 104 105

	/**
	 * Create a copy of the filter with the given prefix stripped
	 * from all #LOCATE_TAG_BASE_TYPE items.  This is used to
	 * filter songs in mounted databases.
	 */
106
	SongFilter WithoutBasePrefix(std::string_view prefix) const noexcept;
107 108
};

109
#endif