diff options
Diffstat (limited to 'src/tag/Tag.hxx')
-rw-r--r-- | src/tag/Tag.hxx | 93 |
1 files changed, 64 insertions, 29 deletions
diff --git a/src/tag/Tag.hxx b/src/tag/Tag.hxx index 5846e7a9d..74221417f 100644 --- a/src/tag/Tag.hxx +++ b/src/tag/Tag.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * Copyright (C) 2003-2014 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -20,11 +20,12 @@ #ifndef MPD_TAG_HXX #define MPD_TAG_HXX -#include "TagType.h" -#include "TagItem.hxx" +#include "TagType.h" // IWYU pragma: export +#include "TagItem.hxx" // IWYU pragma: export #include "Compiler.h" #include <algorithm> +#include <iterator> #include <stddef.h> @@ -47,23 +48,23 @@ struct Tag { */ bool has_playlist; + /** the total number of tag items in the #items array */ + unsigned short num_items; + /** an array of tag items */ TagItem **items; - /** the total number of tag items in the #items array */ - unsigned num_items; - /** * Create an empty tag. */ Tag():time(-1), has_playlist(false), - items(nullptr), num_items(0) {} + num_items(0), items(nullptr) {} Tag(const Tag &other); Tag(Tag &&other) :time(other.time), has_playlist(other.has_playlist), - items(other.items), num_items(other.num_items) { + num_items(other.num_items), items(other.items) { other.items = nullptr; other.num_items = 0; } @@ -71,7 +72,9 @@ struct Tag { /** * Free the tag object and all its items. */ - ~Tag(); + ~Tag() { + Clear(); + } Tag &operator=(const Tag &other) = delete; @@ -104,24 +107,6 @@ struct Tag { void Clear(); /** - * Appends a new tag item. - * - * @param type the type of the new tag item - * @param value the value of the tag item (not null-terminated) - * @param len the length of #value - */ - void AddItem(TagType type, const char *value, size_t len); - - /** - * Appends a new tag item. - * - * @param tag the #tag object - * @param type the type of the new tag item - * @param value the value of the tag item (null-terminated) - */ - void AddItem(TagType type, const char *value); - - /** * Merges the data from two tags. If both tags share data for the * same TagType, only data from "add" is used. * @@ -153,8 +138,58 @@ struct Tag { gcc_pure bool HasType(TagType type) const; -private: - void AddItemInternal(TagType type, const char *value, size_t len); + 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}; + } }; /** |