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
36 class XMLNodeHandler
{
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
{
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
);
65 virtual SimpleType
toValue(QString str
) = 0;
66 virtual QString
toString(SimpleType value
) = 0;
75 template <class ElementType
>
76 class XMLStringNodeHandler
: public XMLSimpleNodeHandler
<ElementType
, QString
> {
78 typedef void (ElementType::*Setter
)(QString
);
79 typedef QString (ElementType::*Getter
)();
81 XMLStringNodeHandler(Getter getter
, Setter setter
);
84 virtual QString
toValue(QString str
);
85 virtual QString
toString(QString value
);
89 template <class ElementType
>
90 class XMLIntNodeHandler
: public XMLSimpleNodeHandler
<ElementType
, int> {
92 typedef void (ElementType::*Setter
)(int);
93 typedef int (ElementType::*Getter
)();
95 XMLIntNodeHandler(Getter getter
, Setter setter
);
98 virtual int toValue(QString str
);
99 virtual QString
toString(int value
);
103 template <class ElementType
>
104 class XMLUIntNodeHandler
: public XMLSimpleNodeHandler
<ElementType
, unsigned int> {
106 typedef void (ElementType::*Setter
)(unsigned int);
107 typedef unsigned int (ElementType::*Getter
)();
109 XMLUIntNodeHandler(Getter getter
, Setter setter
);
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> {
120 typedef void (ElementType::*Setter
)(double);
121 typedef double (ElementType::*Getter
)();
123 XMLDoubleNodeHandler(Getter getter
, Setter setter
);
126 virtual double toValue(QString str
);
127 virtual QString
toString(double value
);
131 template <class ElementType
>
132 class XMLBoolNodeHandler
: public XMLSimpleNodeHandler
<ElementType
, bool> {
134 typedef void (ElementType::*Setter
)(bool);
135 typedef bool (ElementType::*Getter
)();
137 XMLBoolNodeHandler(Getter getter
, Setter setter
);
140 virtual bool toValue(QString str
);
141 virtual QString
toString(bool value
);
145 template <class ElementType
, class ComplexType
>
146 class XMLComplexNodeHandler
: public XMLNodeHandler
{
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
);
160 XMLFactory
* m_factory
;
166 template <class ElementType
, class ComplexType
>
167 class XMLComplexListNodeHandler
: public XMLNodeHandler
{
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
);
181 XMLFactory
* m_factory
;
182 ListGetter m_listGetter
;
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