4#include <c++utilities/conversion/stringbuilder.h>
5#include <c++utilities/io/binaryreader.h>
6#include <c++utilities/io/binarywriter.h>
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 =
static_cast<int>(
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 =
static_cast<int>(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, end2 = m_children.end(); iterator != end2; ++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 =
static_cast<int>(at);
338 m_children[at] = newChild;
368 for (
Entry *
const child : m_children) {
369 if (
path.front() != child->label()) {
383 if (!creationType ||
path.size() != 1) {
386 switch (*creationType) {
397 BinaryWriter writer(&stream);
399 writer.writeLengthPrefixedString(
label());
401 writer.writeUInt16BE(
static_cast<std::uint16_t
>(1 +
m_extendedData.size()));
402 std::uint8_t flags = 0x00;
406 writer.writeByte(flags);
409 writer.writeUInt32BE(
static_cast<std::uint32_t
>(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());
499 writer.writeUInt16BE(
static_cast<std::uint16_t
>(
m_extendedData.size()));
502 writer.writeUInt32BE(
static_cast<std::uint32_t
>(m_fields.size()));
503 for (
const Field &field : m_fields) {
The exception that is thrown when a parsing error occurs.
~AccountEntry() override
Destroys the entry.
const std::vector< Field > & fields() const
void accumulateStatistics(EntryStatistics &stats) const override
Accumulates the statistics for this account entry and its fields.
AccountEntry * clone() const override
Clones the entry.
void make(std::ostream &stream) const override
Serializes the entry to the specified stream.
Instances of the Entry class form a hierarchic data structure used to store account information.
int index() const
Returns the index of the entry within its parent.
static Entry * parse(std::istream &stream)
Parses an entry from the specified stream.
Entry(const std::string &label=std::string(), NodeEntry *parent=nullptr)
Constructs a new entry with the specified label and parent.
void makeLabelUnique()
Internally called to make the entry's label unique within the parent.
bool isIndirectChildOf(const NodeEntry *entry) const
Returns an indication whether the instance is an indirect child of the specified entry.
void setParent(NodeEntry *parent, int index=-1)
Sets the parent for the entry.
NodeEntry * parent() const
Returns the parent entry.
static bool denotesNodeEntry(std::uint8_t version)
void setLabel(const std::string &label)
Sets the label.
std::string m_extendedData
const std::string & label() const
Returns the label.
virtual Entry * clone() const =0
Clones the entry.
std::list< std::string > path() const
Returns the path of the entry.
virtual ~Entry()
Destroys the entry.
The Field class holds field information which consists of a name and a value and is able to serialize...
The NodeEntry class acts as parent for other entries.
Entry * entryByPath(std::list< std::string > &path, bool includeThis=true, const EntryType *creationType=nullptr)
Returns an entry specified by the provided path.
void replaceChild(std::size_t at, Entry *newChild)
Replaces the child at the specified index with the specified newChild.
bool isExpandedByDefault() const
void accumulateStatistics(EntryStatistics &stats) const override
Accumulates the statistics for this node entry and its children.
NodeEntry * clone() const override
Clones the entry.
~NodeEntry() override
Destroys the entry.
NodeEntry()
Constructs a new node entry.
void deleteChildren(int begin, int end)
Deletes children from the node entry.
const std::vector< Entry * > & children() const
void make(std::ostream &stream) const override
Serializes the entry to the specified stream.
The exception that is thrown when a parsing error occurs.
Contains all IO related classes.
EntryType
Specifies the entry type.