Builder.cxx 5.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "config.h"
21
#include "Builder.hxx"
22
#include "Settings.hxx"
23 24
#include "Pool.hxx"
#include "FixString.hxx"
25
#include "Tag.hxx"
26
#include "util/WritableBuffer.hxx"
27
#include "util/StringView.hxx"
28

29 30
#include <array>

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

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

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

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

45
TagBuilder::TagBuilder(Tag &&other) noexcept
46
	:duration(other.duration), has_playlist(other.has_playlist)
47 48 49 50
{
	/* move all TagItem pointers from the Tag object; we don't
	   need to contact the tag pool, because all we do is move
	   references */
51
	items.reserve(other.num_items);
52 53 54 55 56 57 58 59
	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;
}

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

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

	return *this;
}

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

	return *this;
}

TagBuilder &
87
TagBuilder::operator=(Tag &&other) noexcept
88
{
89
	duration = other.duration;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	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;
}

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

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

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

	/* 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();
128
	tag.num_items = n_items;
129
	tag.items = new TagItem *[n_items];
130
	std::copy_n(items.begin(), n_items, tag.items);
131 132 133 134 135
	items.clear();

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

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

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

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

	return false;
}

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

	has_playlist |= other.has_playlist;

172 173 174 175 176 177 178
	/* 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;

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

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

189
inline void
190
TagBuilder::AddItemInternal(TagType type, StringView value) noexcept
191
{
192
	assert(!value.empty());
193

194 195 196
	auto f = FixTagString(value);
	if (!f.IsNull())
		value = { f.data, f.size };
197

198 199 200 201 202
	TagItem *i;
	{
		const std::lock_guard<Mutex> protect(tag_pool_lock);
		i = tag_pool_get_item(type, value);
	}
203

204
	free(f.data);
205 206 207 208 209

	items.push_back(i);
}

void
210
TagBuilder::AddItem(TagType type, StringView value) noexcept
211
{
212
	if (value.empty() || !IsTagEnabled(type))
213 214
		return;

215
	AddItemInternal(type, value);
216 217 218
}

void
219
TagBuilder::AddItem(TagType type, const char *value) noexcept
220
{
221 222
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
223
	assert(value != nullptr);
224
#endif
225

226
	AddItem(type, StringView(value));
227
}
228

229
void
230
TagBuilder::AddEmptyItem(TagType type) noexcept
231
{
232 233 234 235 236
	TagItem *i;
	{
		const std::lock_guard<Mutex> protect(tag_pool_lock);
		i = tag_pool_get_item(type, "");
	}
237 238 239 240

	items.push_back(i);
}

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);
}