Handler.hxx 2.61 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 27 28 29 30 31

#include <assert.h>

/**
 * A callback table for receiving metadata of a song.
 */
32
struct TagHandler {
33
	/**
34
	 * Declare the duration of a song.  Do not call
35 36 37
	 * this when the duration could not be determined, because
	 * there is no magic value for "unknown duration".
	 */
38
	void (*duration)(SongTime duration, void *ctx);
39 40 41 42 43 44 45

	/**
	 * A tag has been read.
	 *
	 * @param the value of the tag; the pointer will become
	 * invalid after returning
	 */
46
	void (*tag)(TagType type, const char *value, void *ctx);
47 48 49 50 51 52

	/**
	 * A name-value pair has been read.  It is the codec specific
	 * representation of tags.
	 */
	void (*pair)(const char *key, const char *value, void *ctx);
53 54 55
};

static inline void
56
tag_handler_invoke_duration(const TagHandler &handler, void *ctx,
57
			    SongTime duration)
58
{
59 60
	if (handler.duration != nullptr)
		handler.duration(duration, ctx);
61 62 63
}

static inline void
64
tag_handler_invoke_tag(const TagHandler &handler, void *ctx,
65
		       TagType type, const char *value)
66 67
{
	assert((unsigned)type < TAG_NUM_OF_ITEM_TYPES);
68
	assert(value != nullptr);
69

70 71
	if (handler.tag != nullptr)
		handler.tag(type, value, ctx);
72 73
}

74
static inline void
75
tag_handler_invoke_pair(const TagHandler &handler, void *ctx,
76 77
			const char *name, const char *value)
{
78 79
	assert(name != nullptr);
	assert(value != nullptr);
80

81 82
	if (handler.pair != nullptr)
		handler.pair(name, value, ctx);
83 84
}

85
/**
86
 * This #TagHandler implementation adds tag values to a #TagBuilder object
87 88
 * (casted from the context pointer).
 */
89
extern const TagHandler add_tag_handler;
90

91
/**
92
 * This #TagHandler implementation adds tag values to a #TagBuilder object
93 94 95
 * (casted from the context pointer), and supports the has_playlist
 * attribute.
 */
96
extern const TagHandler full_tag_handler;
97

98
#endif