17 BEGIN_NAMESPACE_SERIALIZATION
20 using NodePtr = std::shared_ptr<Node>;
30 virtual ~
Node() =
default;
43 void addAttribute(
const std::string& name,
const T& value);
51 void write(
const std::string& name,
const T& value);
60 template <
typename T,
typename U>
61 void write(
const std::string& name,
const T& value,
const U& defaultValue);
69 void read(
const std::string& name, T& value);
77 template <
typename T,
typename U>
78 void read(
const std::string& name, T& value,
const U& defaultValue);
87 void update(
const std::string& name, T& value);
95 template <
typename T,
typename U>
96 void update(
const std::string& name, T& value,
const U& defaultValue);
103 virtual NodePtr
updateChild(
const std::string& childName) = 0;
110 virtual NodePtr
readChild(
const std::string& childName) = 0;
118 virtual NodePtr
createNode(std::string name, std::string value) = 0;
125 virtual void initializeNode(std::string name, std::string value) = 0;
137 virtual void setValue(std::string value) = 0;
144 virtual NodePtr
addChild(std::string name) = 0;
151 virtual NodePtr
addChild(
const NodePtr& childNode) = 0;
157 virtual NodePtr
clone() = 0;
164 NodePtr
clone(
const NodePtr& nodeToClone);
202 virtual NodePtr
getAttribute(
const size_t attributeIndex) = 0;
209 virtual NodePtr
getChild(
const size_t childIndex) = 0;
245 virtual void removeChild(
const std::string& childName) = 0;
251 virtual void removeChild(
const size_t childIndex) = 0;
263 virtual std::string
getName() = 0;
311 virtual void internalWrite(
const std::string& name,
const std::string& value,
const std::string& defaultValue) = 0;
312 virtual void internalRead(
const std::string& name, std::string& value,
const std::string& defaultValue) = 0;
313 virtual void internalAddAttribute(
const std::string& name,
const std::string& value) = 0;
316 bool isDummyNode =
false;
319 const std::string defaultStringValue =
"";
322 const std::string dummyString =
"#";
324 template <
typename Iterator>
325 void writeIterable(
const std::string& name, Iterator first, Iterator last);
327 template <
typename Iterator>
328 void readIterable(
const std::string& name, Iterator first, Iterator last);
330 template <
typename OutputIterator>
331 void readIterable(
const std::string& name, OutputIterator output);
333 template <
typename T>
334 typename std::enable_if<!is_container<T>::value>::type properTypeWrite(
const std::string& name,
const T& value)
339 const std::string defaultValueString = valueString + this->dummyString;
340 internalWrite(name, valueString, defaultValueString);
343 template <
typename T>
344 typename std::enable_if<is_container<T>::value>::type properTypeWrite(
const std::string& name,
const T& value)
349 template <
typename T>
350 typename std::enable_if<!is_container<T>::value>::type properTypeRead(
const std::string& name, T& value)
354 std::string outputValueString;
358 internalRead(name, outputValueString, defaultValueString);
361 catch (std::invalid_argument&)
363 throw XmlException(XmlExceptionCode::xecNodeDoesNotContainExpectedType);
365 catch (std::out_of_range&)
367 throw XmlException(XmlExceptionCode::xecValueIsOutOfRange);
371 template <
typename T>
372 typename std::enable_if<is_container<T>::value>::type properTypeRead(
const std::string& name, T& value)
374 properContainerTypeRead(name, value);
377 template <
typename T>
378 typename std::enable_if<has_back_insert<T>::value>::type properContainerTypeRead(
const std::string& name, T& value)
380 readIterable(name, std::back_insert_iterator<T>(value));
383 template <
typename T>
384 typename std::enable_if<!has_back_insert<T>::value>::type properContainerTypeRead(
const std::string& name, T& value)
389 virtual NodePtr addUniqueChild(
const std::string& name) = 0;
392 template <
typename T>
395 properTypeWrite(name, value);
398 template <
typename T,
typename U>
399 void Node::write(
const std::string& name,
const T& value,
const U& defaultValue)
403 internalWrite(name, valueString, defaultValueString);
406 template <
typename Iterator>
407 void Node::writeIterable(
const std::string& name, Iterator first, Iterator last)
409 const size_t size = std::distance(first, last);
410 std::string inputValuesString;
411 using IteratorValueType =
typename std::iterator_traits<Iterator>::value_type;
413 for (
size_t i = 0; i < size; i++)
416 inputValuesString +=
";";
420 if (!inputValuesString.empty())
422 inputValuesString.pop_back();
424 internalWrite(name, inputValuesString, this->defaultStringValue);
427 template <
typename T>
430 properTypeRead(name, value);
433 template <
typename T,
typename U>
434 void Node::read(
const std::string& name, T& value,
const U& defaultValue)
436 std::string outputValueString;
441 internalRead(name, outputValueString, defaultValueString);
444 catch (std::invalid_argument&)
446 throw XmlException(XmlExceptionCode::xecNodeDoesNotContainExpectedType);
448 catch (std::out_of_range&)
450 throw XmlException(XmlExceptionCode::xecValueIsOutOfRange);
454 template <
typename T>
467 template <
typename T,
typename U>
468 void Node::update(
const std::string& name, T& value,
const U& defaultValue)
472 write(name, value, defaultValue);
476 read(name, value, defaultValue);
480 template <
typename Iterator>
481 void Node::readIterable(
const std::string& name, Iterator first, Iterator last)
483 std::string outputValuesString;
487 internalRead(name, outputValuesString,
"");
489 using IteratorValueType =
typename std::iterator_traits<Iterator>::value_type;
492 std::stringstream valuesStringStream(outputValuesString);
493 std::string stringSegment;
495 const size_t size = std::distance(first, last);
497 for (
size_t i = 0; i < size; i++)
499 if (std::getline(valuesStringStream, stringSegment,
';'))
505 *first =
static_cast<IteratorValueType
>(defaultBaseTypeValue.defaultValue);
510 catch (std::invalid_argument&)
512 throw XmlException(XmlExceptionCode::xecNodeDoesNotContainExpectedType);
514 catch (std::out_of_range&)
516 throw XmlException(XmlExceptionCode::xecValueIsOutOfRange);
521 template <
typename OutputIterator>
522 void Node::readIterable(
const std::string& name, OutputIterator output)
524 std::string outputValuesString;
527 internalRead(name, outputValuesString,
"");
529 if (!outputValuesString.empty())
531 std::stringstream valuesStringStream(outputValuesString);
532 std::string stringSegment;
534 while (std::getline(valuesStringStream, stringSegment,
';'))
541 catch (std::invalid_argument&)
543 throw XmlException(XmlExceptionCode::xecNodeDoesNotContainExpectedType);
545 catch (std::out_of_range&)
547 throw XmlException(XmlExceptionCode::xecValueIsOutOfRange);
551 template <
typename T>
557 END_NAMESPACE_SERIALIZATION
virtual NodePtr addAttributeNode(const NodePtr &attributeNode)=0
addAttributeNode adds a given attribute node to a node and returns a pointer to it.
void addAttribute(const std::string &name, const T &value)
addAttribute function adds an attribute with a given name and given value to a node.
Definition: node.h:552
virtual NodePtr addChild(std::string name)=0
addChild function creates and adds a child with a given name to a node and returns a pointer to it...
virtual void setValue(std::string value)=0
setValue function sets a given value to a node.
Node is an abstract class that represents a node in an xml tree and defines functions for writing and...
Definition: node.h:26
virtual void removeAttribute(const std::string &attributeName)=0
removeAttribute removes all attributes with a given name from a node.
virtual void initializeNode(std::string name, std::string value)=0
initializeNode function initializes an empty Node with a given name and given value.
void read(const std::string &name, T &value)
read function reads a value from a nodes child with a given name.
Definition: node.h:428
bool isDummy() const
isDummy returns true if node was created as a dummy node in updateChild function
virtual void clearChildNodes()=0
clearChildNodes clears all child nodes.
virtual NodePtr updateChild(const std::string &childName)=0
updateChild function creates or finds a node child with a given name based on update type ...
virtual bool isInitialized()=0
isInitialized returns true if node is initialized or false otherwise.
virtual NodePtr createNode(std::string name, std::string value)=0
createNode function creates a new Node and returns a pointer to it.
Definition: type_converter.h:273
virtual std::string getName()=0
getName returns a name of a node.
XmlException class that inherits from std::exception and is used for exception handling in xml serial...
Definition: xml_exception.h:42
void write(const std::string &name, const T &value)
write function creates a new child with a given name and adds a given value to it.
Definition: node.h:393
void update(const std::string &name, T &value)
update function writes or reads a value to/from a node, depends on nodes isWriteUpdateType ...
Definition: node.h:455
virtual bool isWrite()=0
isWrite returns true if node should be used for writting or false otherwise.
virtual size_t getAttributeCount()=0
getAttributeCount returns a number of attributes of a node.
virtual NodePtr getAttribute(const size_t attributeIndex)=0
getAttribute returns an attribute with a given index or nullptr if index is invalid.
virtual NodePtr getNextSibling()=0
getNextSibling returns a sibling of a node or nullptr if it doesnt exist.
virtual NodePtr findAttributeNode(const std::string &name)=0
findAttributeNode finds an attribute node with a given name and returns a pointer to it or nullptr if...
Definition: type_converter.h:89
virtual std::string getValue()=0
getValue returns a value of a node.
virtual void updateOperation(bool write)=0
updateOperation sets a node update operation (node can be used either for writting or reading)...
virtual void destroy()=0
destroy function releases internal node and sets it value to nullptr.
Structures for proper conversion between types that are used to build type generic write/read functio...
virtual void removeChild(const std::string &childName)=0
removeChild removes all children with a given name from a node.
virtual void clearAttributeNodes()=0
clearAttributeNodes clears all attribute nodes.
virtual size_t getChildCount()=0
getChildCount returns a number of children of a node.
Definition: type_converter.h:301
virtual NodePtr getChild(const size_t childIndex)=0
getChild returns a child with a given index or nullptr if index is invalid.
virtual NodePtr findChildNode(const std::string &name)=0
findChildNode finds a child node with a given name and returns a pointer to it or nullptr if not foun...
virtual NodePtr clone()=0
clone function creates a clone of a node.
virtual bool isEmpty()=0
isEmpty returns true if node does not have any children, attributes and if it does not have any value...
virtual NodePtr readChild(const std::string &childName)=0
readChild function finds a node child with a given name