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

20 21
#ifndef MPD_TAG_HANDLER_HXX
#define MPD_TAG_HANDLER_HXX
22 23

#include "check.h"
24
#include "Type.h"
25
#include "Chrono.hxx"
26
#include "Compiler.h"
27

28
struct AudioFormat;
29
class TagBuilder;
30 31

/**
32
 * An interface for receiving metadata of a song.
33
 */
34 35 36 37 38 39 40
class TagHandler {
	const unsigned want_mask;

public:
	static constexpr unsigned WANT_DURATION = 0x1;
	static constexpr unsigned WANT_TAG = 0x2;
	static constexpr unsigned WANT_PAIR = 0x4;
41
	static constexpr unsigned WANT_AUDIO_FORMAT = 0x8;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

	explicit TagHandler(unsigned _want_mask) noexcept
		:want_mask(_want_mask) {}

	TagHandler(const TagHandler &) = delete;
	TagHandler &operator=(const TagHandler &) = delete;

	bool WantDuration() const noexcept {
		return want_mask & WANT_DURATION;
	}

	bool WantTag() const noexcept {
		return want_mask & WANT_TAG;
	}

	bool WantPair() const noexcept {
		return want_mask & WANT_PAIR;
	}

61 62 63 64
	bool WantAudioFormat() const noexcept {
		return want_mask & WANT_AUDIO_FORMAT;
	}

65
	/**
66
	 * Declare the duration of a song.  Do not call
67 68 69
	 * this when the duration could not be determined, because
	 * there is no magic value for "unknown duration".
	 */
70
	virtual void OnDuration(SongTime duration) noexcept = 0;
71 72 73 74 75 76 77

	/**
	 * A tag has been read.
	 *
	 * @param the value of the tag; the pointer will become
	 * invalid after returning
	 */
78
	virtual void OnTag(TagType type, const char *value) noexcept = 0;
79 80 81 82 83

	/**
	 * A name-value pair has been read.  It is the codec specific
	 * representation of tags.
	 */
84
	virtual void OnPair(const char *key, const char *value) noexcept = 0;
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

	/**
	 * Declare the audio format of a song.
	 *
	 * Because the #AudioFormat type is limited to formats
	 * supported by MPD, the value passed to this method may be an
	 * approximation (should be the one passed to
	 * DecoderClient::Ready()).  For example, some codecs such as
	 * MP3 are bit depth agnostic, so the decoder plugin chooses a
	 * bit depth depending on what the codec library emits.
	 *
	 * This method is only called by those decoder plugins which
	 * implement it.  Some may not have any code for calling it,
	 * and others may decide that determining the audio format is
	 * too expensive.
	 */
	virtual void OnAudioFormat(AudioFormat af) noexcept = 0;
102 103
};

104 105 106 107 108 109 110 111 112 113
class NullTagHandler : public TagHandler {
public:
	explicit NullTagHandler(unsigned _want_mask) noexcept
		:TagHandler(_want_mask) {}

	void OnDuration(gcc_unused SongTime duration) noexcept override {}
	void OnTag(gcc_unused TagType type,
		   gcc_unused const char *value) noexcept override {}
	void OnPair(gcc_unused const char *key,
		    gcc_unused const char *value) noexcept override {}
114
	void OnAudioFormat(AudioFormat af) noexcept override;
115
};
116

117
/**
118 119
 * This #TagHandler implementation adds tag values to a #TagBuilder
 * object.
120
 */
121 122 123 124 125
class AddTagHandler : public NullTagHandler {
protected:
	TagBuilder &tag;

	AddTagHandler(unsigned _want_mask, TagBuilder &_builder) noexcept
126 127
		:NullTagHandler(WANT_DURATION|WANT_TAG|_want_mask),
		 tag(_builder) {}
128 129 130

public:
	explicit AddTagHandler(TagBuilder &_builder) noexcept
131
		:AddTagHandler(0, _builder) {}
132 133 134 135

	void OnDuration(SongTime duration) noexcept override;
	void OnTag(TagType type, const char *value) noexcept override;
};
136

137
/**
138
 * This #TagHandler implementation adds tag values to a #TagBuilder object
139 140 141
 * (casted from the context pointer), and supports the has_playlist
 * attribute.
 */
142
class FullTagHandler : public AddTagHandler {
143 144
	AudioFormat *const audio_format;

145
protected:
146 147 148 149 150 151
	FullTagHandler(unsigned _want_mask, TagBuilder &_builder,
		       AudioFormat *_audio_format) noexcept
		:AddTagHandler(WANT_PAIR|_want_mask
			       |(_audio_format ? WANT_AUDIO_FORMAT : 0),
			       _builder),
		 audio_format(_audio_format) {}
152

153
public:
154 155 156
	explicit FullTagHandler(TagBuilder &_builder,
				AudioFormat *_audio_format=nullptr) noexcept
		:FullTagHandler(0, _builder, _audio_format) {}
157 158

	void OnPair(const char *key, const char *value) noexcept override;
159
	void OnAudioFormat(AudioFormat af) noexcept override;
160
};
161

162
#endif