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) {
171 res.push_back(
label());
180 const auto version =
static_cast<byte
>(stream.peek());
215 , m_expandedByDefault(true)
223 :
Entry(label, parent)
224 , m_expandedByDefault(true)
232 : m_expandedByDefault(true)
234 BinaryReader reader(&stream);
235 const byte version = reader.readByte();
239 if (version != 0x0 && version != 0x1) {
242 setLabel(reader.readLengthPrefixedString());
244 if (version == 0x1) {
245 uint16 extendedHeaderSize = reader.readUInt16BE();
246 if (extendedHeaderSize >= 1) {
247 byte flags = reader.readByte();
248 m_expandedByDefault = flags & 0x80;
249 extendedHeaderSize -= 1;
253 const uint32 childCount = reader.readUInt32BE();
254 for (uint32 i = 0; i != childCount; ++i) {
267 for (
Entry *
const otherChild : other.m_children) {
269 clonedChild->m_parent =
this;
270 clonedChild->m_index = m_children.size();
271 m_children.push_back(clonedChild);
280 for (
Entry *
const child : m_children) {
281 child->m_parent =
nullptr;
294 const auto endIterator = m_children.begin() + end;
297 for (
auto iterator = m_children.cbegin() + begin; iterator != endIterator; ++iterator) {
298 (*iterator)->m_parent =
nullptr;
303 m_children.erase(m_children.begin() + begin, endIterator);
306 const int diff = end - begin;
307 for (
auto iterator = m_children.begin() + begin, end = m_children.end(); iterator != end; ++iterator) {
308 (*iterator)->m_index -= diff;
321 if (at >= m_children.size()) {
326 m_children[at]->m_parent =
nullptr;
327 m_children[at]->m_index = -1;
330 if (
auto *newChildOldParent = newChild->m_parent) {
331 newChildOldParent->m_children.erase(newChildOldParent->m_children.begin() + newChild->m_index);
332 for (
auto i = newChildOldParent->m_children.begin() + newChild->m_index; i < newChildOldParent->m_children.end(); ++i) {
338 newChild->m_parent =
this;
339 newChild->m_index = at;
340 m_children[at] = newChild;
371 for (
Entry *
const child : m_children) {
372 if (
path.front() != child->label()) {
386 if (!creationType ||
path.size() != 1) {
389 switch (*creationType) {
400 BinaryWriter writer(&stream);
402 writer.writeLengthPrefixedString(
label());
409 writer.writeByte(flags);
412 writer.writeUInt32BE(m_children.size());
413 for (
const Entry *
const child : m_children) {
436 :
Entry(label, parent)
445 BinaryReader reader(&stream);
446 byte version = reader.readByte();
451 if (version != 0x0 && version != 0x1) {
454 setLabel(reader.readLengthPrefixedString());
456 if (version == 0x1) {
457 const uint16 extendedHeaderSize = reader.readUInt16BE();
461 const uint32 fieldCount = reader.readUInt32BE();
462 for (uint32 i = 0; i != fieldCount; ++i) {
463 m_fields.push_back(
Field(
this, stream));
475 m_fields = other.m_fields;
487 BinaryWriter writer(&stream);
489 writer.writeLengthPrefixedString(
label());
494 writer.writeUInt32BE(m_fields.size());
495 for (
const Field &field : m_fields) {
~NodeEntry()
Destroys the entry.
NodeEntry * parent() const
Returns the parent entry.
The NodeEntry class acts as parent for other entries.
virtual void make(std::ostream &stream) const
Serializes the entry to the specified stream.
const std::string & label() const
Returns the label.
void setLabel(const std::string &label)
Sets the label.
~AccountEntry()
Destroys the entry.
bool isIndirectChildOf(NodeEntry *entry) const
Returns an indication whether the instance is an indirect child of the specified entry.
NodeEntry()
Constructs a new node entry.
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.
Entry * entryByPath(std::list< std::string > &path, bool includeThis=true, EntryType *creationType=nullptr)
Returns an entry specified by the provided path.
virtual NodeEntry * clone() const
Clones the entry.
std::list< std::string > path() const
Returns the path of the entry.
std::string m_extendedData
The Field class holds field information which consists of a name and a value and is able to serialize...
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.
virtual void make(std::ostream &stream) const
Serializes the entry to the specified stream.
static Entry * parse(std::istream &stream)
Parses an entry from the specified stream.
virtual AccountEntry * clone() const
Clones 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.
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.
Instances of the Entry class form a hierarchic data strucutre used to store account information...