mpd/src/tag/Tag.hxx

179 lines
4.4 KiB
C++
Raw Normal View History

2013-07-30 20:11:57 +02:00
/*
2022-07-14 17:58:12 +02:00
* Copyright 2003-2022 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
2017-02-08 08:26:58 +01:00
#include "Type.h" // IWYU pragma: export
#include "Item.hxx" // IWYU pragma: export
#include "Chrono.hxx"
2020-04-24 16:00:05 +02:00
#include "util/DereferenceIterator.hxx"
2013-07-30 20:11:57 +02:00
#include <memory>
2019-07-05 09:59:00 +02:00
#include <utility>
2013-07-30 20:11:57 +02:00
/**
* 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
*/
2018-01-20 19:39:06 +01:00
SignedSongTime duration = SignedSongTime::Negative();
2013-07-30 20:11:57 +02:00
/**
* Does this file have an embedded playlist (e.g. embedded CUE
* sheet)?
*/
2018-01-20 19:39:06 +01:00
bool has_playlist = false;
2013-07-30 20:11:57 +02:00
/** the total number of tag items in the #items array */
2018-01-20 19:39:06 +01:00
unsigned short num_items = 0;
2013-07-30 20:11:57 +02:00
/** an array of tag items */
2018-01-20 19:39:06 +01:00
TagItem **items = nullptr;
2013-07-30 20:11:57 +02:00
/**
* Create an empty tag.
*/
2018-01-20 19:39:06 +01:00
Tag() = default;
2013-07-30 20:11:57 +02:00
2018-01-20 19:38:52 +01:00
Tag(const Tag &other) noexcept;
2013-07-30 20:11:57 +02:00
2018-01-20 19:38:52 +01:00
Tag(Tag &&other) noexcept
: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.
*/
2018-01-20 19:38:52 +01:00
~Tag() noexcept {
Clear();
}
2013-07-30 20:11:57 +02:00
Tag &operator=(const Tag &other) = delete;
2018-01-20 19:38:52 +01:00
Tag &operator=(Tag &&other) noexcept {
duration = other.duration;
2013-07-30 20:11:57 +02:00
has_playlist = other.has_playlist;
2016-02-23 20:57:56 +01:00
MoveItemsFrom(std::move(other));
return *this;
}
/**
* Similar to the move operator, but move only the #TagItem
* array.
*/
void MoveItemsFrom(Tag &&other) noexcept {
2013-07-30 20:11:57 +02:00
std::swap(items, other.items);
std::swap(num_items, other.num_items);
}
/**
* Returns true if the tag contains no items. This ignores
* the "duration" attribute.
2013-07-30 20:11:57 +02:00
*/
bool IsEmpty() const noexcept {
2013-07-30 20:11:57 +02:00
return num_items == 0;
}
/**
* Returns true if the tag contains any information.
*/
bool IsDefined() const noexcept {
return !IsEmpty() || !duration.IsNegative();
2013-07-30 20:11:57 +02:00
}
/**
* Clear everything, as if this was a new Tag object.
*/
void Clear() noexcept;
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
*/
static Tag Merge(const Tag &base,
const Tag &add) noexcept;
static std::unique_ptr<Tag> MergePtr(const Tag &base,
const Tag &add) noexcept;
2013-07-30 20:11:57 +02:00
/**
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
*/
static std::unique_ptr<Tag> Merge(std::unique_ptr<Tag> base,
2018-01-20 19:38:52 +01:00
std::unique_ptr<Tag> add) noexcept;
2013-07-30 20:11:57 +02:00
/**
* Merges the data from two tags. Any of the two may be nullptr.
*
* @return a newly allocated tag (or nullptr if both
* parameters are nullptr)
*/
static std::unique_ptr<Tag> Merge(const Tag *base,
const Tag *add) noexcept;
2013-07-30 20:11:57 +02:00
/**
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
*/
2021-10-13 10:30:43 +02:00
[[gnu::pure]]
const char *GetValue(TagType type) const noexcept;
2013-07-30 20:11:57 +02:00
/**
* Checks whether the tag contains one or more items with
* the specified type.
*/
2021-10-13 10:30:43 +02:00
[[gnu::pure]]
bool HasType(TagType type) const noexcept;
/**
* Returns a value for sorting on the specified type, with
* automatic fallbacks to the next best tag type
* (e.g. #TAG_ALBUM_ARTIST falls back to #TAG_ARTIST). If
* there is no such value, returns an empty string.
*/
2021-10-13 10:30:43 +02:00
[[gnu::pure]] [[gnu::returns_nonnull]]
const char *GetSortValue(TagType type) const noexcept;
2020-04-24 16:00:05 +02:00
using const_iterator = DereferenceIterator<TagItem *const*,
const TagItem>;
2018-01-20 19:38:52 +01:00
const_iterator begin() const noexcept {
return const_iterator{items};
}
2018-01-20 19:38:52 +01:00
const_iterator end() const noexcept {
return const_iterator{items + num_items};
}
2013-07-30 20:11:57 +02:00
};
#endif