Builder.cxx 5.88 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
#include "Builder.hxx"
21
#include "Settings.hxx"
22 23
#include "Pool.hxx"
#include "FixString.hxx"
24
#include "Tag.hxx"
25
#include "util/WritableBuffer.hxx"
26
#include "util/StringView.hxx"
27

28 29
#include <array>

30
#include <assert.h>
31
#include <stdlib.h>
32

33
TagBuilder::TagBuilder(const Tag &other) noexcept
34
	:duration(other.duration), has_playlist(other.has_playlist)
35 36 37
{
	items.reserve(other.num_items);

38 39
	const std::lock_guard<Mutex> protect(tag_pool_lock);

40 41 42 43
	for (unsigned i = 0, n = other.num_items; i != n; ++i)
		items.push_back(tag_pool_dup_item(other.items[i]));
}

44
TagBuilder::TagBuilder(Tag &&other) noexcept
45
	:duration(other.duration), has_playlist(other.has_playlist)
46 47 48 49
{
	/* move all TagItem pointers from the Tag object; we don't
	   need to contact the tag pool, because all we do is move
	   references */
50
	items.reserve(other.num_items);
51 52 53 54 55 56 57 58
	std::copy_n(other.items, other.num_items, std::back_inserter(items));

	/* discard the pointers from the Tag object */
	other.num_items = 0;
	delete[] other.items;
	other.items = nullptr;
}

59
TagBuilder &
60
TagBuilder::operator=(const TagBuilder &other) noexcept
61 62
{
	/* copy all attributes */
63
	duration = other.duration;
64 65 66 67
	has_playlist = other.has_playlist;
	items = other.items;

	/* increment the tag pool refcounters */
68
	const std::lock_guard<Mutex> protect(tag_pool_lock);
69 70 71 72 73 74
	for (auto i : items)
		tag_pool_dup_item(i);

	return *this;
}

75
TagBuilder &
76
TagBuilder::operator=(TagBuilder &&other) noexcept
77
{
78
	duration = other.duration;
79 80 81 82 83 84 85
	has_playlist = other.has_playlist;
	items = std::move(other.items);

	return *this;
}

TagBuilder &
86
TagBuilder::operator=(Tag &&other) noexcept
87
{
88
	duration = other.duration;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	has_playlist = other.has_playlist;

	/* move all TagItem pointers from the Tag object; we don't
	   need to contact the tag pool, because all we do is move
	   references */
	items.clear();
	items.reserve(other.num_items);
	std::copy_n(other.items, other.num_items, std::back_inserter(items));

	/* discard the pointers from the Tag object */
	other.num_items = 0;
	delete[] other.items;
	other.items = nullptr;

	return *this;
}

106
void
107
TagBuilder::Clear() noexcept
108
{
109
	duration = SignedSongTime::Negative();
110
	has_playlist = false;
111
	RemoveAll();
112 113
}

114
void
115
TagBuilder::Commit(Tag &tag) noexcept
116
{
117 118
	tag.Clear();

119
	tag.duration = duration;
120
	tag.has_playlist = has_playlist;
121 122 123 124 125 126

	/* move all TagItem pointers to the new Tag object without
	   touching the TagPool reference counters; the
	   vector::clear() call is important to detach them from this
	   object */
	const unsigned n_items = items.size();
127
	tag.num_items = n_items;
128
	tag.items = new TagItem *[n_items];
129
	std::copy_n(items.begin(), n_items, tag.items);
130 131 132 133 134
	items.clear();

	/* now ensure that this object is fresh (will not delete any
	   items because we've already moved them out) */
	Clear();
135
}
136

137
Tag
138
TagBuilder::Commit() noexcept
139 140 141 142 143 144
{
	Tag tag;
	Commit(tag);
	return tag;
}

145
std::unique_ptr<Tag>
146
TagBuilder::CommitNew() noexcept
147
{
148
	std::unique_ptr<Tag> tag(new Tag());
149
	Commit(*tag);
150 151 152
	return tag;
}

153
bool
154
TagBuilder::HasType(TagType type) const noexcept
155 156 157 158 159 160 161 162
{
	for (auto i : items)
		if (i->type == type)
			return true;

	return false;
}

163
void
164
TagBuilder::Complement(const Tag &other) noexcept
165
{
166 167
	if (duration.IsNegative())
		duration = other.duration;
168 169 170

	has_playlist |= other.has_playlist;

171 172 173 174 175 176 177
	/* build a table of tag types that were already present in
	   this object, which will not be copied from #other */
	std::array<bool, TAG_NUM_OF_ITEM_TYPES> present;
	present.fill(false);
	for (const TagItem *i : items)
		present[i->type] = true;

178 179
	items.reserve(items.size() + other.num_items);

180
	const std::lock_guard<Mutex> protect(tag_pool_lock);
181 182
	for (unsigned i = 0, n = other.num_items; i != n; ++i) {
		TagItem *item = other.items[i];
183
		if (!present[item->type])
184 185 186 187
			items.push_back(tag_pool_dup_item(item));
	}
}

188 189 190
void
TagBuilder::AddItemUnchecked(TagType type, StringView value) noexcept
{
191 192 193 194 195 196
	TagItem *i;

	{
		const std::lock_guard<Mutex> protect(tag_pool_lock);
		i = tag_pool_get_item(type, value);
	}
197 198 199 200

	items.push_back(i);
}

201
inline void
202
TagBuilder::AddItemInternal(TagType type, StringView value) noexcept
203
{
204
	assert(!value.empty());
205

206 207 208
	auto f = FixTagString(value);
	if (!f.IsNull())
		value = { f.data, f.size };
209

210
	AddItemUnchecked(type, value);
211

212
	free(f.data);
213 214 215
}

void
216
TagBuilder::AddItem(TagType type, StringView value) noexcept
217
{
218
	if (value.empty() || !IsTagEnabled(type))
219 220
		return;

221
	AddItemInternal(type, value);
222 223 224
}

void
225
TagBuilder::AddItem(TagType type, const char *value) noexcept
226
{
227 228
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
229
	assert(value != nullptr);
230
#endif
231

232
	AddItem(type, StringView(value));
233
}
234

235
void
236
TagBuilder::AddEmptyItem(TagType type) noexcept
237
{
238
	AddItemUnchecked(type, "");
239 240
}

241
void
242
TagBuilder::RemoveAll() noexcept
243
{
244 245 246 247 248
	{
		const std::lock_guard<Mutex> protect(tag_pool_lock);
		for (auto i : items)
			tag_pool_put_item(i);
	}
249 250 251 252 253

	items.clear();
}

void
254
TagBuilder::RemoveType(TagType type) noexcept
255 256 257 258 259 260 261 262 263 264 265 266
{
	const auto begin = items.begin(), end = items.end();

	items.erase(std::remove_if(begin, end,
				   [type](TagItem *item) {
					   if (item->type != type)
						   return false;
					   tag_pool_put_item(item);
					   return true;
				   }),
		    end);
}