4#include <c++utilities/conversion/stringbuilder.h>
5#include <c++utilities/io/binaryreader.h>
6#include <c++utilities/io/binarywriter.h>
43 : m_label(other.m_label)
66 string newLabel(
label());
68 bool needsNewLabel =
false;
69 for (
Entry *
const sibling : m_parent->children()) {
70 if (sibling ==
this || newLabel != sibling->label()) {
81 m_label.swap(newLabel);
105 m_parent->m_children.erase(m_parent->m_children.begin() + m_index);
106 for (
auto i = m_parent->m_children.begin() + m_index; i < m_parent->m_children.end(); ++i) {
114 m_index =
static_cast<int>(
parent->m_children.size());
115 parent->m_children.push_back(
this);
117 for (
auto i =
parent->m_children.insert(
parent->m_children.begin() +
index,
this) + 1; i !=
parent->m_children.end(); ++i) {
166 res.push_back(
label());
175 const auto version =
static_cast<std::uint8_t
>(stream.peek());
210 , m_expandedByDefault(true)
219 , m_expandedByDefault(true)
227 : m_expandedByDefault(true)
229 BinaryReader reader(&stream);
230 const std::uint8_t version = reader.readByte();
234 if (version != 0x0 && version != 0x1) {
237 setLabel(reader.readLengthPrefixedString());
239 if (version == 0x1) {
240 std::uint16_t extendedHeaderSize = reader.readUInt16BE();
241 if (extendedHeaderSize >= 1) {
242 std::uint8_t flags = reader.readByte();
243 m_expandedByDefault = flags & 0x80;
244 extendedHeaderSize -= 1;
248 const std::uint32_t childCount = reader.readUInt32BE();
249 for (std::uint32_t i = 0; i != childCount; ++i) {
262 for (
Entry *
const otherChild : other.m_children) {
263 Entry *clonedChild = otherChild->clone();
264 clonedChild->m_parent = this;
265 clonedChild->m_index = static_cast<int>(m_children.size());
266 m_children.push_back(clonedChild);
275 for (
Entry *
const child : m_children) {
276 child->m_parent =
nullptr;
289 const auto endIterator = m_children.begin() + end;
292 for (
auto iterator = m_children.cbegin() + begin; iterator != endIterator; ++iterator) {
293 (*iterator)->m_parent =
nullptr;
298 m_children.erase(m_children.begin() + begin, endIterator);
301 const int diff = end - begin;
302 for (
auto iterator = m_children.begin() + begin, end2 = m_children.end(); iterator != end2; ++iterator) {
303 (*iterator)->m_index -= diff;
316 if (at >= m_children.size()) {
321 m_children[at]->m_parent =
nullptr;
322 m_children[at]->m_index = -1;
325 if (
auto *newChildOldParent = newChild->m_parent) {
326 newChildOldParent->m_children.erase(newChildOldParent->m_children.begin() + newChild->m_index);
327 for (
auto i = newChildOldParent->m_children.begin() + newChild->m_index; i < newChildOldParent->m_children.end(); ++i) {
333 newChild->m_parent =
this;
334 newChild->m_index =
static_cast<int>(at);
335 m_children[at] = newChild;
365 for (
Entry *
const child : m_children) {
366 if (
path.front() != child->label()) {
380 if (!creationType ||
path.size() != 1) {
383 switch (*creationType) {
394 BinaryWriter writer(&stream);
396 writer.writeLengthPrefixedString(
label());
398 writer.writeUInt16BE(
static_cast<std::uint16_t
>(1 +
m_extendedData.size()));
399 std::uint8_t flags = 0x00;
403 writer.writeByte(flags);
406 writer.writeUInt32BE(
static_cast<std::uint32_t
>(m_children.size()));
407 for (
const Entry *
const child : m_children) {
424 children->accumulateStatistics(stats);
450 BinaryReader reader(&stream);
451 std::uint8_t version = reader.readByte();
456 if (version != 0x0 && version != 0x1) {
459 setLabel(reader.readLengthPrefixedString());
461 if (version == 0x1) {
462 const std::uint16_t extendedHeaderSize = reader.readUInt16BE();
466 const std::uint32_t fieldCount = reader.readUInt32BE();
467 for (std::uint32_t i = 0; i != fieldCount; ++i) {
468 m_fields.push_back(
Field(
this, stream));
480 m_fields = other.m_fields;
492 BinaryWriter writer(&stream);
494 writer.writeLengthPrefixedString(
label());
496 writer.writeUInt16BE(
static_cast<std::uint16_t
>(
m_extendedData.size()));
499 writer.writeUInt32BE(
static_cast<std::uint32_t
>(m_fields.size()));
500 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.
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.
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...
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.