Builder.cxx 5.66 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)
35
	:duration(other.duration), has_playlist(other.has_playlist)
36 37 38 39 40 41 42 43 44 45
{
	items.reserve(other.num_items);

	tag_pool_lock.lock();
	for (unsigned i = 0, n = other.num_items; i != n; ++i)
		items.push_back(tag_pool_dup_item(other.items[i]));
	tag_pool_lock.unlock();
}

TagBuilder::TagBuilder(Tag &&other)
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 61 62 63
TagBuilder &
TagBuilder::operator=(const TagBuilder &other)
{
	/* copy all attributes */
64
	duration = other.duration;
65 66 67 68 69 70 71 72 73 74 75 76
	has_playlist = other.has_playlist;
	items = other.items;

	/* increment the tag pool refcounters */
	tag_pool_lock.lock();
	for (auto i : items)
		tag_pool_dup_item(i);
	tag_pool_lock.unlock();

	return *this;
}

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

	return *this;
}

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

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

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

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

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

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

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

147
Tag *
148
TagBuilder::CommitNew()
149 150 151
{
	Tag *tag = new Tag();
	Commit(*tag);
152 153 154
	return tag;
}

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

	return false;
}

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

	has_playlist |= other.has_playlist;

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

180 181 182 183 184
	items.reserve(items.size() + other.num_items);

	tag_pool_lock.lock();
	for (unsigned i = 0, n = other.num_items; i != n; ++i) {
		TagItem *item = other.items[i];
185
		if (!present[item->type])
186 187 188 189 190
			items.push_back(tag_pool_dup_item(item));
	}
	tag_pool_lock.unlock();
}

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

196 197 198
	auto f = FixTagString(value);
	if (!f.IsNull())
		value = { f.data, f.size };
199 200

	tag_pool_lock.lock();
201
	auto i = tag_pool_get_item(type, value);
202 203
	tag_pool_lock.unlock();

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

	items.push_back(i);
}

void
210
TagBuilder::AddItem(TagType type, StringView value)
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)
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 230 231 232
void
TagBuilder::AddEmptyItem(TagType type)
{
	tag_pool_lock.lock();
233
	auto i = tag_pool_get_item(type, "");
234 235 236 237 238
	tag_pool_lock.unlock();

	items.push_back(i);
}

239
void
240
TagBuilder::RemoveAll() noexcept
241 242 243 244 245 246 247 248 249 250
{
	tag_pool_lock.lock();
	for (auto i : items)
		tag_pool_put_item(i);
	tag_pool_lock.unlock();

	items.clear();
}

void
251
TagBuilder::RemoveType(TagType type) noexcept
252 253 254 255 256 257 258 259 260 261 262 263
{
	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);
}