Filter.hxx 2.67 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
#include "util/Compiler.h"
25

26
#include <string>
27

28 29
#include <stdint.h>

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

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

class SongFilter {
	AndSongFilter and_filter;

42 43 44 45
public:
	SongFilter() = default;

	gcc_nonnull(3)
46
	SongFilter(TagType tag, const char *value, bool fold_case=false);
47 48 49

	~SongFilter();

50 51 52
	SongFilter(SongFilter &&) = default;
	SongFilter &operator=(SongFilter &&) = default;

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

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

62
	gcc_nonnull(2,3)
63
	void Parse(const char *tag, const char *value, bool fold_case=false);
64

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

71 72
	void Optimize() noexcept;

73
	gcc_pure
74
	bool Match(const LightSong &song) const noexcept;
75

76
	const auto &GetItems() const noexcept {
77
		return and_filter.GetItems();
78
	}
79

80
	gcc_pure
81
	bool IsEmpty() const noexcept {
82
		return and_filter.IsEmpty();
83 84
	}

85 86 87 88
	/**
	 * Is there at least one item with "fold case" enabled?
	 */
	gcc_pure
89
	bool HasFoldCase() const noexcept;
90

91 92 93 94
	/**
	 * Does this filter contain constraints other than "base"?
	 */
	gcc_pure
95
	bool HasOtherThanBase() const noexcept;
96

97
	/**
98 99
	 * Returns the "base" specification (if there is one) or
	 * nullptr.
100 101
	 */
	gcc_pure
102
	const char *GetBase() const noexcept;
103 104 105 106 107 108 109

	/**
	 * 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.
	 */
	SongFilter WithoutBasePrefix(const char *prefix) const noexcept;
110 111
};

112
#endif