mpd/src/tag/Tag.hxx

213 lines
4.7 KiB
C++
Raw Normal View History

2013-07-30 20:11:57 +02:00
/*
2015-01-01 19:48:13 +01:00
* Copyright (C) 2003-2015 The Music Player Daemon Project
2013-07-30 20:11:57 +02:00
* 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.
*/
#ifndef MPD_TAG_HXX
#define MPD_TAG_HXX
2013-11-28 11:50:54 +01:00
#include "TagType.h" // IWYU pragma: export
#include "TagItem.hxx" // IWYU pragma: export
#include "Chrono.hxx"
2013-10-15 09:21:13 +02:00
#include "Compiler.h"
2013-07-30 20:11:57 +02:00
#include <algorithm>
#include <iterator>
2013-07-30 20:11:57 +02:00
#include <stddef.h>
/**
* The meta information about a song file. It is a MPD specific
* subset of tags (e.g. from ID3, vorbis comments, ...).
*/
struct Tag {
/**
* The duration of the song. A negative value means that the
* length is unknown.
2013-07-30 20:11:57 +02:00
*/
SignedSongTime duration;
2013-07-30 20:11:57 +02:00
/**
* Does this file have an embedded playlist (e.g. embedded CUE
* sheet)?
*/
bool has_playlist;
/** the total number of tag items in the #items array */
unsigned short num_items;
2013-07-30 20:11:57 +02:00
/** an array of tag items */
TagItem **items;
/**
* Create an empty tag.
*/
Tag():duration(SignedSongTime::Negative()), has_playlist(false),
num_items(0), items(nullptr) {}
2013-07-30 20:11:57 +02:00
Tag(const Tag &other);
Tag(Tag &&other)
:duration(other.duration), has_playlist(other.has_playlist),
num_items(other.num_items), items(other.items) {
2013-07-30 20:11:57 +02:00
other.items = nullptr;
other.num_items = 0;
}
/**
* Free the tag object and all its items.
*/
~Tag() {
Clear();
}
2013-07-30 20:11:57 +02:00
Tag &operator=(const Tag &other) = delete;
Tag &operator=(Tag &&other) {
duration = other.duration;
2013-07-30 20:11:57 +02:00
has_playlist = other.has_playlist;
std::swap(items, other.items);
std::swap(num_items, other.num_items);
return *this;
}
/**
* Returns true if the tag contains no items. This ignores
* the "duration" attribute.
2013-07-30 20:11:57 +02:00
*/
bool IsEmpty() const {
return num_items == 0;
}
/**
* Returns true if the tag contains any information.
*/
bool IsDefined() const {
return !IsEmpty() || !duration.IsNegative();
2013-07-30 20:11:57 +02:00
}
/**
* Clear everything, as if this was a new Tag object.
*/
void Clear();
2013-07-30 20:11:57 +02:00
/**
* Merges the data from two tags. If both tags share data for the
* same TagType, only data from "add" is used.
2013-07-30 20:11:57 +02:00
*
* @return a newly allocated tag
*/
gcc_malloc
static Tag *Merge(const Tag &base, const Tag &add);
/**
2013-10-28 23:58:17 +01:00
* Merges the data from two tags. Any of the two may be nullptr. Both
2013-07-30 20:11:57 +02:00
* are freed by this function.
*
* @return a newly allocated tag
*/
gcc_malloc
static Tag *MergeReplace(Tag *base, Tag *add);
/**
2013-10-28 23:58:17 +01:00
* Returns the first value of the specified tag type, or
* nullptr if none is present in this tag object.
2013-07-30 20:11:57 +02:00
*/
gcc_pure
const char *GetValue(TagType type) const;
2013-07-30 20:11:57 +02:00
/**
* Checks whether the tag contains one or more items with
* the specified type.
*/
2013-09-26 18:11:00 +02:00
gcc_pure
bool HasType(TagType type) const;
class const_iterator {
friend struct Tag;
const TagItem *const*cursor;
constexpr const_iterator(const TagItem *const*_cursor)
:cursor(_cursor) {}
public:
constexpr const TagItem &operator*() const {
return **cursor;
}
constexpr const TagItem *operator->() const {
return *cursor;
}
const_iterator &operator++() {
++cursor;
return *this;
}
const_iterator operator++(int) {
auto result = cursor++;
return const_iterator{result};
}
const_iterator &operator--() {
--cursor;
return *this;
}
const_iterator operator--(int) {
auto result = cursor--;
return const_iterator{result};
}
constexpr bool operator==(const_iterator other) const {
return cursor == other.cursor;
}
constexpr bool operator!=(const_iterator other) const {
return cursor != other.cursor;
}
};
const_iterator begin() const {
return const_iterator{items};
}
const_iterator end() const {
return const_iterator{items + num_items};
}
2013-07-30 20:11:57 +02:00
};
/**
* Parse the string, and convert it into a #TagType. Returns
2013-07-30 20:11:57 +02:00
* #TAG_NUM_OF_ITEM_TYPES if the string could not be recognized.
*/
2013-09-26 18:11:00 +02:00
gcc_pure
TagType
2013-07-30 20:11:57 +02:00
tag_name_parse(const char *name);
/**
* Parse the string, and convert it into a #TagType. Returns
2013-07-30 20:11:57 +02:00
* #TAG_NUM_OF_ITEM_TYPES if the string could not be recognized.
*
* Case does not matter.
*/
2013-09-26 18:11:00 +02:00
gcc_pure
TagType
2013-07-30 20:11:57 +02:00
tag_name_parse_i(const char *name);
#endif