4 #include <c++utilities/conversion/stringbuilder.h>
5 #include <c++utilities/io/binaryreader.h>
6 #include <c++utilities/io/binarywriter.h>
32 Entry::Entry(
const string &label,
NodeEntry *parent)
46 : m_label(other.m_label)
69 string newLabel(
label());
71 bool needsNewLabel =
false;
73 if (sibling ==
this || newLabel != sibling->label()) {
84 m_label.swap(newLabel);
108 m_parent->m_children.erase(m_parent->m_children.begin() + m_index);
109 for (
auto i = m_parent->m_children.begin() + m_index; i < m_parent->m_children.end(); ++i) {
117 m_index =
parent->m_children.size();
118 parent->m_children.push_back(
this);
120 for (
auto i =
parent->m_children.insert(
parent->m_children.begin() +
index,
this) + 1; i !=
parent->m_children.end(); ++i) {
169 res.push_back(
label());
178 const auto version = static_cast<std::uint8_t>(stream.peek());
213 , m_expandedByDefault(true)
221 :
Entry(label, parent)
222 , m_expandedByDefault(true)
230 : m_expandedByDefault(true)
232 BinaryReader reader(&stream);
233 const std::uint8_t version = reader.readByte();
237 if (version != 0x0 && version != 0x1) {
240 setLabel(reader.readLengthPrefixedString());
242 if (version == 0x1) {
243 std::uint16_t extendedHeaderSize = reader.readUInt16BE();
244 if (extendedHeaderSize >= 1) {
245 std::uint8_t flags = reader.readByte();
246 m_expandedByDefault = flags & 0x80;
247 extendedHeaderSize -= 1;
251 const std::uint32_t childCount = reader.readUInt32BE();
252 for (std::uint32_t i = 0; i != childCount; ++i) {
265 for (
Entry *
const otherChild : other.m_children) {
267 clonedChild->m_parent =
this;
268 clonedChild->m_index = m_children.size();
269 m_children.push_back(clonedChild);
278 for (
Entry *
const child : m_children) {
279 child->m_parent =
nullptr;
292 const auto endIterator = m_children.begin() + end;
295 for (
auto iterator = m_children.cbegin() + begin; iterator != endIterator; ++iterator) {
296 (*iterator)->m_parent =
nullptr;
301 m_children.erase(m_children.begin() + begin, endIterator);
304 const int diff = end - begin;
305 for (
auto iterator = m_children.begin() + begin, end = m_children.end(); iterator != end; ++iterator) {
306 (*iterator)->m_index -= diff;
319 if (at >= m_children.size()) {
324 m_children[at]->m_parent =
nullptr;
325 m_children[at]->m_index = -1;
328 if (
auto *newChildOldParent = newChild->m_parent) {
329 newChildOldParent->m_children.erase(newChildOldParent->m_children.begin() + newChild->m_index);
330 for (
auto i = newChildOldParent->m_children.begin() + newChild->m_index; i < newChildOldParent->m_children.end(); ++i) {
336 newChild->m_parent =
this;
337 newChild->m_index = at;
338 m_children[at] = newChild;
368 for (
Entry *
const child : m_children) {
369 if (
path.front() != child->label()) {
376 return static_cast<NodeEntry *>(child)->entryByPath(
path,
false, creationType);
383 if (!creationType ||
path.size() != 1) {
386 switch (*creationType) {
397 BinaryWriter writer(&stream);
399 writer.writeLengthPrefixedString(
label());
402 std::uint8_t flags = 0x00;
406 writer.writeByte(flags);
409 writer.writeUInt32BE(m_children.size());
410 for (
const Entry *
const child : m_children) {
427 children->accumulateStatistics(stats);
444 :
Entry(label, parent)
453 BinaryReader reader(&stream);
454 std::uint8_t version = reader.readByte();
459 if (version != 0x0 && version != 0x1) {
462 setLabel(reader.readLengthPrefixedString());
464 if (version == 0x1) {
465 const std::uint16_t extendedHeaderSize = reader.readUInt16BE();
469 const std::uint32_t fieldCount = reader.readUInt32BE();
470 for (std::uint32_t i = 0; i != fieldCount; ++i) {
471 m_fields.push_back(
Field(
this, stream));
483 m_fields = other.m_fields;
495 BinaryWriter writer(&stream);
497 writer.writeLengthPrefixedString(
label());
502 writer.writeUInt32BE(m_fields.size());
503 for (
const Field &field : m_fields) {