KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
serializablexmllist.h
1/**
2 * @brief Template helper for serializing and deserializing lists of XML-capable objects.
3 */
4#ifndef SERIALIZABLEXMLLIST_H
5#define SERIALIZABLEXMLLIST_H
6
7#include <QDomDocument>
8#include <QList>
9#include "iserializabletoxml.h"
10#include "ideserializablefromxml.h"
11
12/**
13 * @brief A QList subclass that serializes/deserializes its elements via ISerializableToDomElement.
14 *
15 * @tparam T Element type; must derive from both ISerializableToDomElement and
16 * IDeserializableFromDomElement.
17 */
18template <typename T>
19class SerializableXmlList : public QList<T>
20{
21public:
22 /** @brief Equality comparison — lists are equal if they have the same elements in the same order.
23 * @param other List to compare against
24 * @return true if both lists have identical elements in the same order */
25 bool operator==(const SerializableXmlList<T>& other) const
26 {
27 bool result = SerializableXmlList<T>::count() == other.count();
28 for(int i = 0;i < SerializableXmlList<T>::count() && result == true;i++) {
29 result = this->at(i) == other.at(i);
30 }
31 return result;
32 }
33 /** @brief Inequality comparison.
34 * @param other List to compare against
35 * @return true if the lists differ */
36 bool operator!=(const SerializableXmlList<T>& other) const { return !(*this == other); }
37
38 /**
39 * @brief Serialize all elements into a QDomElement container.
40 *
41 * Each element's serializeToDomElement() result is appended as a child of a
42 * new element named tagName.
43 * @param tagName XML tag name for the wrapper element
44 * @return QDomElement with one child per list element
45 */
46 QDomElement serializeToDomElement(const QString& tagName) const {
47 static_assert(std::is_base_of<ISerializableToDomElement, T>::value, "T not derived from ISerializableToDomElement");
48 QDomDocument doc;
49 QDomElement result = doc.createElement(tagName);
50 for(typename QList<T>::const_iterator it = this->constBegin();it != this->constEnd();it++) {
51 T item = *it;
53 QDomElement obj = p->serializeToDomElement();
54 result.appendChild(obj);
55 }
56 return result;
57 }
58
59 /**
60 * @brief Populate this list by deserializing child elements of a QDomElement.
61 *
62 * Each direct child element is passed to a default-constructed element's
63 * deserializeFromDomElement() method.
64 * @param element Parent DOM element whose children to deserialize
65 */
66 void deserializeFromDomElement(const QDomElement& element) {
67 static_assert(std::is_base_of<IDeserializableFromDomElement, T>::value, "T not derived from IDeserializableFromDomElement");
68 for(QDomElement childElement = element.firstChildElement();childElement.isNull() == false;childElement = childElement.nextSiblingElement()) {
69 T item;
71 ptr->deserializeFromDomElement(childElement);
72 this->append(item);
73 }
74 }
75};
76
77#endif // SERIALIZABLEXMLLIST_H
Interface for objects that can deserialize themselves from a QDomElement.
virtual void deserializeFromDomElement(const QDomElement &element)=0
Populate this object from a QDomElement.
Interface for objects that can serialize themselves to a QDomElement.
virtual QDomElement serializeToDomElement() const =0
Serialize this object into a QDomElement.
Template helper for serializing and deserializing lists of XML-capable objects.
void deserializeFromDomElement(const QDomElement &element)
Populate this list by deserializing child elements of a QDomElement.
QDomElement serializeToDomElement(const QString &tagName) const
Serialize all elements into a QDomElement container.
bool operator==(const SerializableXmlList< T > &other) const
Equality comparison — lists are equal if they have the same elements in the same order.
bool operator!=(const SerializableXmlList< T > &other) const
Inequality comparison.