FotoSHOCK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
XmlSerializer.hpp
1 /*
2  * Copyright 2011, 2012 Lukas Jirkovsky
3  *
4  * This file is part of FotoSHOCKcore.
5  *
6  * FotoSHOCKcore is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, version 3 of the License.
9  *
10  * FotoSHOCKcore is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with FotoSHOCKcore. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef XMLSERIALIZER_H
20 #define XMLSERIALIZER_H
21 
22 #include "NVPtype.hpp"
23 #include "XmlNode.hpp"
24 
25 namespace FotoSHOCKcore {
26 
27 class XmlSerializer;
28 
30 
39  public:
41  XmlSerializer(const XmlNode& node) : m_node(node)
42  {}
43 
45 
51  template <typename T>
52  XmlSerializer& operator<<(const NVPtype<T>& nvp) {
53  XmlNode(m_node, nvp);
54  return *this;
55  }
56 
57  XmlSerializer& operator<<(const XmlNode& node) {
58  xmlAddChild(m_node.node(), node.node());
59  return *this;
60  }
61 
63 
70  // TODO: allow deserializing of multiple elements with a same name
71  template <typename T>
72  T deserialize(const char* name) {
73  // find a corresponding xmlNode by traversinf the children of the m_node
74  xmlNodePtr currNode = m_node.node()->children;
75  for (; currNode; currNode = currNode->next) {
76  if (currNode->type == XML::XML_ELEMENT_NODE) {
77  if (! strcmp(name, reinterpret_cast<const char*>(currNode->name))) {
78  // node found
79  break;
80  }
81  }
82  }
83 
84  if (! currNode) {
85  // TODO: throw an exception
86  }
87 
88  // deserialize the value from node
89  // we need to deserialize from the children (for primitive types tmp should be XML_TEXT_NODE)
90  xmlNodePtr tmp = currNode->children;
91  if (tmp) {
93  }
94 
95  // return a default value for the type (possibly throw an exception)
96  return T();
97  }
98  private:
99  const XmlNode& m_node;
100 };
101 
102 }
103 
104 #endif // OPERATIONSERIALIZER_H