not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / libs / kephal / xml / xmlnodehandler.h
blob3a2995eb125fddcab86800976ba02cc6ff131bf8
1 /*
2 * Copyright 2008 Aike J Sommer <dev@aikesommer.name>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef KEPHAL_XMLNODEHANDLER_H
22 #define KEPHAL_XMLNODEHANDLER_H
25 #include "xmltype.h"
27 #include <QDomNode>
30 namespace Kephal {
32 class XMLType;
33 class XMLFactory;
36 class XMLNodeHandler {
37 public:
38 virtual ~XMLNodeHandler() {}
40 virtual void beginSave(XMLType * element) = 0;
41 virtual void beginLoad(XMLType * element) = 0;
42 virtual bool hasMore(XMLType * element) = 0;
43 virtual void setNode(XMLType * element, QDomNode node) = 0;
44 virtual QDomNode node(XMLType * element, QDomDocument doc, QString name) = 0;
45 virtual QString str(XMLType * element) = 0;
49 template <class ElementType, typename SimpleType>
50 class XMLSimpleNodeHandler : public XMLNodeHandler {
51 public:
52 typedef void (ElementType::*Setter)(SimpleType);
53 typedef SimpleType (ElementType::*Getter)();
55 XMLSimpleNodeHandler(Getter getter, Setter setter);
57 virtual void beginSave(XMLType * element);
58 virtual void beginLoad(XMLType * element);
59 virtual bool hasMore(XMLType * element);
60 virtual void setNode(XMLType * element, QDomNode node);
61 virtual QDomNode node(XMLType * element, QDomDocument doc, QString name);
62 virtual QString str(XMLType * element);
64 protected:
65 virtual SimpleType toValue(QString str) = 0;
66 virtual QString toString(SimpleType value) = 0;
68 private:
69 Getter m_getter;
70 Setter m_setter;
71 bool m_saved;
75 template <class ElementType>
76 class XMLStringNodeHandler : public XMLSimpleNodeHandler<ElementType, QString> {
77 public:
78 typedef void (ElementType::*Setter)(QString);
79 typedef QString (ElementType::*Getter)();
81 XMLStringNodeHandler(Getter getter, Setter setter);
83 protected:
84 virtual QString toValue(QString str);
85 virtual QString toString(QString value);
89 template <class ElementType>
90 class XMLIntNodeHandler : public XMLSimpleNodeHandler<ElementType, int> {
91 public:
92 typedef void (ElementType::*Setter)(int);
93 typedef int (ElementType::*Getter)();
95 XMLIntNodeHandler(Getter getter, Setter setter);
97 protected:
98 virtual int toValue(QString str);
99 virtual QString toString(int value);
103 template <class ElementType>
104 class XMLUIntNodeHandler : public XMLSimpleNodeHandler<ElementType, unsigned int> {
105 public:
106 typedef void (ElementType::*Setter)(unsigned int);
107 typedef unsigned int (ElementType::*Getter)();
109 XMLUIntNodeHandler(Getter getter, Setter setter);
111 protected:
112 virtual unsigned int toValue(QString str);
113 virtual QString toString(unsigned int value);
117 template <class ElementType>
118 class XMLDoubleNodeHandler : public XMLSimpleNodeHandler<ElementType, double> {
119 public:
120 typedef void (ElementType::*Setter)(double);
121 typedef double (ElementType::*Getter)();
123 XMLDoubleNodeHandler(Getter getter, Setter setter);
125 protected:
126 virtual double toValue(QString str);
127 virtual QString toString(double value);
131 template <class ElementType>
132 class XMLBoolNodeHandler : public XMLSimpleNodeHandler<ElementType, bool> {
133 public:
134 typedef void (ElementType::*Setter)(bool);
135 typedef bool (ElementType::*Getter)();
137 XMLBoolNodeHandler(Getter getter, Setter setter);
139 protected:
140 virtual bool toValue(QString str);
141 virtual QString toString(bool value);
145 template <class ElementType, class ComplexType>
146 class XMLComplexNodeHandler : public XMLNodeHandler {
147 public:
148 typedef void (ElementType::*Setter)(ComplexType *);
150 XMLComplexNodeHandler(XMLFactory * factory, Setter setter);
152 virtual void beginSave(XMLType * element);
153 virtual void beginLoad(XMLType * element);
154 virtual bool hasMore(XMLType * element);
155 virtual void setNode(XMLType * element, QDomNode node);
156 virtual QDomNode node(XMLType * element, QDomDocument doc, QString name);
157 virtual QString str(XMLType * element);
159 private:
160 XMLFactory * m_factory;
161 Setter m_setter;
162 bool m_saved;
166 template <class ElementType, class ComplexType>
167 class XMLComplexListNodeHandler : public XMLNodeHandler {
168 public:
169 typedef QList<ComplexType *> & (ElementType::*ListGetter)();
171 XMLComplexListNodeHandler(XMLFactory * factory, ListGetter listGetter);
173 virtual void beginSave(XMLType * element);
174 virtual void beginLoad(XMLType * element);
175 virtual bool hasMore(XMLType * element);
176 virtual void setNode(XMLType * element, QDomNode node);
177 virtual QDomNode node(XMLType * element, QDomDocument doc, QString name);
178 virtual QString str(XMLType * element);
180 private:
181 XMLFactory * m_factory;
182 ListGetter m_listGetter;
183 int m_pos;
188 #include "xmlnodehandler.h.cpp"
191 #define STRING_ATTRIBUTE(name, class, getter, setter) attribute(name, new XMLStringNodeHandler<class>(&class::getter, &class::setter))
192 #define INT_ATTRIBUTE(name, class, getter, setter) attribute(name, new XMLIntNodeHandler<class>(&class::getter, &class::setter))
193 #define UINT_ATTRIBUTE(name, class, getter, setter) attribute(name, new XMLUIntNodeHandler<class>(&class::getter, &class::setter))
194 #define DOUBLE_ATTRIBUTE(name, class, getter, setter) attribute(name, new XMLDoubleNodeHandler<class>(&class::getter, &class::setter))
195 #define BOOL_ATTRIBUTE(name, class, getter, setter) attribute(name, new XMLBoolNodeHandler<class>(&class::getter, &class::setter))
197 #define STRING_ELEMENT(name, class, getter, setter) element(name, new XMLStringNodeHandler<class>(&class::getter, &class::setter))
198 #define INT_ELEMENT(name, class, getter, setter) element(name, new XMLIntNodeHandler<class>(&class::getter, &class::setter))
199 #define UINT_ELEMENT(name, class, getter, setter) element(name, new XMLUIntNodeHandler<class>(&class::getter, &class::setter))
200 #define DOUBLE_ELEMENT(name, class, getter, setter) element(name, new XMLDoubleNodeHandler<class>(&class::getter, &class::setter))
201 #define BOOL_ELEMENT(name, class, getter, setter) element(name, new XMLBoolNodeHandler<class>(&class::getter, &class::setter))
202 #define COMPLEX_ELEMENT(name, class, setter, factory, complex) element(name, new XMLComplexNodeHandler<class, complex>(factory, &class::setter))
203 #define COMPLEX_ELEMENT_LIST(name, class, listGetter, factory, complex) element(name, new XMLComplexListNodeHandler<class, complex>(factory, &class::listGetter))
206 #endif // KEPHAL_XMLNODEHANDLER_H