TagBuilder.cxx 5.69 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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"
#include "TagBuilder.hxx"
22
#include "Settings.hxx"
23 24 25
#include "TagPool.hxx"
#include "TagString.hxx"
#include "Tag.hxx"
26
#include "util/WritableBuffer.hxx"
27
#include "util/StringView.hxx"
28

29 30
#include <array>

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

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

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

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

	return *this;
}

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

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

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

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

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

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

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

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

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

	return false;
}

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

	has_playlist |= other.has_playlist;

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

181 182 183 184 185
	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];
186
		if (!present[item->type])
187 188 189 190 191
			items.push_back(tag_pool_dup_item(item));
	}
	tag_pool_lock.unlock();
}

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

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

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

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

	items.push_back(i);
}

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

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

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

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

230 231 232 233
void
TagBuilder::AddEmptyItem(TagType type)
{
	tag_pool_lock.lock();
234
	auto i = tag_pool_get_item(type, StringView::Empty());
235 236 237 238 239
	tag_pool_lock.unlock();

	items.push_back(i);
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
void
TagBuilder::RemoveAll()
{
	tag_pool_lock.lock();
	for (auto i : items)
		tag_pool_put_item(i);
	tag_pool_lock.unlock();

	items.clear();
}

void
TagBuilder::RemoveType(TagType type)
{
	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);
}