4 #include <c++utilities/conversion/stringbuilder.h> 5 #include <c++utilities/io/binaryreader.h> 6 #include <c++utilities/io/binarywriter.h> 33 Entry::Entry(
const string &label,
NodeEntry *parent)
47 : m_label(other.m_label)
70 string newLabel(
label());
72 bool needsNewLabel =
false;
74 if (sibling ==
this || newLabel != sibling->label()) {
85 m_label.swap(newLabel);
109 m_parent->m_children.erase(m_parent->m_children.begin() + m_index);
110 for (
auto i = m_parent->m_children.begin() + m_index; i < m_parent->m_children.end(); ++i) {
118 m_index =
parent->m_children.size();
119 parent->m_children.push_back(
this);
121 for (
auto i =
parent->m_children.insert(
parent->m_children.begin() +
index,
this) + 1; i !=
parent->m_children.end(); ++i) {
170 res.push_back(
label());
179 const auto version =
static_cast<byte
>(stream.peek());
214 , m_expandedByDefault(true)
222 :
Entry(label, parent)
223 , m_expandedByDefault(true)
231 : m_expandedByDefault(true)
233 BinaryReader reader(&stream);
234 const byte version = reader.readByte();
238 if (version != 0x0 && version != 0x1) {
241 setLabel(reader.readLengthPrefixedString());
243 if (version == 0x1) {
244 uint16 extendedHeaderSize = reader.readUInt16BE();
245 if (extendedHeaderSize >= 1) {
246 byte flags = reader.readByte();
247 m_expandedByDefault = flags & 0x80;
248 extendedHeaderSize -= 1;
252 const uint32 childCount = reader.readUInt32BE();
253 for (uint32 i = 0; i != childCount; ++i) {
266 for (
Entry *
const otherChild : other.m_children) {
268 clonedChild->m_parent =
this;
269 clonedChild->m_index = m_children.size();
270 m_children.push_back(clonedChild);
279 for (
Entry *
const child : m_children) {
280 child->m_parent =
nullptr;
293 const auto endIterator = m_children.begin() + end;
296 for (
auto iterator = m_children.cbegin() + begin; iterator != endIterator; ++iterator) {
297 (*iterator)->m_parent =
nullptr;
302 m_children.erase(m_children.begin() + begin, endIterator);
305 const int diff = end - begin;
306 for (
auto iterator = m_children.begin() + begin, end = m_children.end(); iterator != end; ++iterator) {
307 (*iterator)->m_index -= diff;
320 if (at >= m_children.size()) {
325 m_children[at]->m_parent =
nullptr;
326 m_children[at]->m_index = -1;
329 if (
auto *newChildOldParent = newChild->m_parent) {
330 newChildOldParent->m_children.erase(newChildOldParent->m_children.begin() + newChild->m_index);
331 for (
auto i = newChildOldParent->m_children.begin() + newChild->m_index; i < newChildOldParent->m_children.end(); ++i) {
337 newChild->m_parent =
this;
338 newChild->m_index = at;
339 m_children[at] = newChild;
369 for (
Entry *
const child : m_children) {
370 if (
path.front() != child->label()) {
384 if (!creationType ||
path.size() != 1) {
387 switch (*creationType) {
398 BinaryWriter writer(&stream);
400 writer.writeLengthPrefixedString(
label());
407 writer.writeByte(flags);
410 writer.writeUInt32BE(m_children.size());
411 for (
const Entry *
const child : m_children) {
428 children->accumulateStatistics(stats);
445 :
Entry(label, parent)
454 BinaryReader reader(&stream);
455 byte version = reader.readByte();
460 if (version != 0x0 && version != 0x1) {
463 setLabel(reader.readLengthPrefixedString());
465 if (version == 0x1) {
466 const uint16 extendedHeaderSize = reader.readUInt16BE();
470 const uint32 fieldCount = reader.readUInt32BE();
471 for (uint32 i = 0; i != fieldCount; ++i) {
472 m_fields.push_back(
Field(
this, stream));
484 m_fields = other.m_fields;
496 BinaryWriter writer(&stream);
498 writer.writeLengthPrefixedString(
label());
503 writer.writeUInt32BE(m_fields.size());
504 for (
const Field &field : m_fields) {
NodeEntry * parent() const
Returns the parent entry.
The NodeEntry class acts as parent for other entries.
void accumulateStatistics(EntryStatistics &stats) const override
Accumulates the statistics for this account entry and its fields.
const std::string & label() const
Returns the label.
void setLabel(const std::string &label)
Sets the label.
bool isIndirectChildOf(const NodeEntry *entry) const
Returns an indication whether the instance is an indirect child of the specified entry.
AccountEntry * clone() const override
Clones the entry.
~AccountEntry() override
Destroys the entry.
NodeEntry()
Constructs a new node entry.
Entry * entryByPath(std::list< std::string > &path, bool includeThis=true, const EntryType *creationType=nullptr)
Returns an entry specified by the provided path.
virtual Entry * clone() const =0
Clones the entry.
bool isExpandedByDefault() const
Contains all IO related classes.
int index() const
Returns the index of the entry within its parent.
NodeEntry * clone() const override
Clones the entry.
std::list< std::string > path() const
Returns the path of the entry.
void make(std::ostream &stream) const override
Serializes the entry to the specified stream.
std::string m_extendedData
The Field class holds field information which consists of a name and a value and is able to serialize...
void accumulateStatistics(EntryStatistics &stats) const override
Accumulates the statistics for this node entry and its children.
The exception that is thrown when a parsing error occurs.
Entry(const std::string &label=std::string(), NodeEntry *parent=nullptr)
Constructs a new entry with the specified label and parent.
static Entry * parse(std::istream &stream)
Parses an entry from the specified stream.
~NodeEntry() override
Destroys the entry.
virtual ~Entry()
Destroys the entry.
static bool denotesNodeEntry(byte version)
const std::vector< Entry * > & children() const
void makeLabelUnique()
Internally called to make the entry's label unique within the parent.
void setParent(NodeEntry *parent, int index=-1)
Sets the parent for the entry.
const std::vector< Field > & fields() const
void deleteChildren(int begin, int end)
Deletes children from the node entry.
void replaceChild(std::size_t at, Entry *newChild)
Replaces the child at the specified index with the specified newChild.
EntryType
Specifies the entry type.
The exception that is thrown when a parsing error occurs.
void make(std::ostream &stream) const override
Serializes the entry to the specified stream.
Instances of the Entry class form a hierarchic data strucutre used to store account information...