MPD  0.20.18
Tag.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_TAG_HXX
21 #define MPD_TAG_HXX
22 
23 #include "TagType.h" // IWYU pragma: export
24 #include "TagItem.hxx" // IWYU pragma: export
25 #include "Chrono.hxx"
26 #include "Compiler.h"
27 
28 #include <algorithm>
29 
34 struct Tag {
40 
46 
48  unsigned short num_items;
49 
52 
56  Tag():duration(SignedSongTime::Negative()), has_playlist(false),
57  num_items(0), items(nullptr) {}
58 
59  Tag(const Tag &other);
60 
61  Tag(Tag &&other)
62  :duration(other.duration), has_playlist(other.has_playlist),
63  num_items(other.num_items), items(other.items) {
64  other.items = nullptr;
65  other.num_items = 0;
66  }
67 
71  ~Tag() {
72  Clear();
73  }
74 
75  Tag &operator=(const Tag &other) = delete;
76 
77  Tag &operator=(Tag &&other) {
78  duration = other.duration;
79  has_playlist = other.has_playlist;
80  MoveItemsFrom(std::move(other));
81  return *this;
82  }
83 
88  void MoveItemsFrom(Tag &&other) {
89  std::swap(items, other.items);
90  std::swap(num_items, other.num_items);
91  }
92 
97  bool IsEmpty() const {
98  return num_items == 0;
99  }
100 
104  bool IsDefined() const {
105  return !IsEmpty() || !duration.IsNegative();
106  }
107 
111  void Clear();
112 
119  gcc_malloc
120  static Tag *Merge(const Tag &base, const Tag &add);
121 
128  gcc_malloc
129  static Tag *MergeReplace(Tag *base, Tag *add);
130 
135  gcc_pure
136  const char *GetValue(TagType type) const noexcept;
137 
142  gcc_pure
143  bool HasType(TagType type) const noexcept;
144 
146  friend struct Tag;
147  const TagItem *const*cursor;
148 
149  constexpr const_iterator(const TagItem *const*_cursor)
150  :cursor(_cursor) {}
151 
152  public:
153  constexpr const TagItem &operator*() const {
154  return **cursor;
155  }
156 
157  constexpr const TagItem *operator->() const {
158  return *cursor;
159  }
160 
162  ++cursor;
163  return *this;
164  }
165 
167  auto result = cursor++;
168  return const_iterator{result};
169  }
170 
172  --cursor;
173  return *this;
174  }
175 
177  auto result = cursor--;
178  return const_iterator{result};
179  }
180 
181  constexpr bool operator==(const_iterator other) const {
182  return cursor == other.cursor;
183  }
184 
185  constexpr bool operator!=(const_iterator other) const {
186  return cursor != other.cursor;
187  }
188  };
189 
191  return const_iterator{items};
192  }
193 
194  const_iterator end() const {
195  return const_iterator{items + num_items};
196  }
197 };
198 
203 gcc_pure
204 TagType
205 tag_name_parse(const char *name) noexcept;
206 
213 gcc_pure
214 TagType
215 tag_name_parse_i(const char *name) noexcept;
216 
217 #endif
Tag & operator=(Tag &&other)
Definition: Tag.hxx:77
const_iterator begin() const
Definition: Tag.hxx:190
void Clear()
Clear everything, as if this was a new Tag object.
const_iterator & operator--()
Definition: Tag.hxx:171
const_iterator operator--(int)
Definition: Tag.hxx:176
constexpr const TagItem * operator->() const
Definition: Tag.hxx:157
TagItem ** items
an array of tag items
Definition: Tag.hxx:51
const_iterator end() const
Definition: Tag.hxx:194
The meta information about a song file.
Definition: Tag.hxx:34
unsigned short num_items
the total number of tag items in the items array
Definition: Tag.hxx:48
constexpr bool operator!=(const_iterator other) const
Definition: Tag.hxx:185
constexpr bool IsNegative() const
Definition: Chrono.hxx:202
#define gcc_malloc
Definition: Compiler.h:112
constexpr const TagItem & operator*() const
Definition: Tag.hxx:153
gcc_pure TagType tag_name_parse_i(const char *name) noexcept
Parse the string, and convert it into a TagType.
SignedSongTime duration
The duration of the song.
Definition: Tag.hxx:39
Tag(Tag &&other)
Definition: Tag.hxx:61
constexpr bool operator==(const_iterator other) const
Definition: Tag.hxx:181
~Tag()
Free the tag object and all its items.
Definition: Tag.hxx:71
bool has_playlist
Does this file have an embedded playlist (e.g.
Definition: Tag.hxx:45
static gcc_malloc Tag * Merge(const Tag &base, const Tag &add)
Merges the data from two tags.
Tag()
Create an empty tag.
Definition: Tag.hxx:56
TagType
Codes for the type of a tag item.
Definition: TagType.h:30
void MoveItemsFrom(Tag &&other)
Similar to the move operator, but move only the TagItem array.
Definition: Tag.hxx:88
gcc_pure bool HasType(TagType type) const noexcept
Checks whether the tag contains one or more items with the specified type.
One tag value.
Definition: TagItem.hxx:30
gcc_pure TagType tag_name_parse(const char *name) noexcept
Parse the string, and convert it into a TagType.
const_iterator & operator++()
Definition: Tag.hxx:161
gcc_pure const char * GetValue(TagType type) const noexcept
Returns the first value of the specified tag type, or nullptr if none is present in this tag object...
A variant of SongTime that is based on a signed integer.
Definition: Chrono.hxx:115
bool IsDefined() const
Returns true if the tag contains any information.
Definition: Tag.hxx:104
Tag & operator=(const Tag &other)=delete
static gcc_malloc Tag * MergeReplace(Tag *base, Tag *add)
Merges the data from two tags.
const_iterator operator++(int)
Definition: Tag.hxx:166
#define gcc_pure
Definition: Compiler.h:116
bool IsEmpty() const
Returns true if the tag contains no items.
Definition: Tag.hxx:97
const Partition const char * name
Definition: Count.hxx:34