ffead.server.doc
XMLSerialize.h
1 /*
2  Copyright 2010, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 #ifndef XMLSERIALIZABLE_H_
17 #define XMLSERIALIZABLE_H_
18 #include "XmlParser.h"
19 #include "CastUtil.h"
20 #include <stdexcept>
21 #include <dlfcn.h>
22 #include <cxxabi.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "string"
26 #include <sstream>
27 #include <typeinfo>
28 #include "Object.h"
29 #include "queue"
30 #include "deque"
31 #include "list"
32 #include "map"
33 #include "set"
34 #include "stack"
35 #include "DateFormat.h"
36 #include "BinaryData.h"
37 #include "Constants.h"
38 #include "AMEFResources.h"
39 #include "JSONUtil.h"
40 
41 using namespace std;
42 typedef string (*SerPtr) (void*);
43 typedef void* (*UnSerPtr) (string);
44 
45 class XMLSerialize {
46  static string demangle(const char *mangled);
47  static string getClassName(void* instance);
48  static string _handleAllSerialization(string className,void *t);
49  static void* _handleAllUnSerialization(string objXml,string className);
50  static string _ser(void* t,string classN);
51  static string _servec(void* t,string classN);
52  static string _serlis(void* t,string classN);
53  static string _serset(void* t,string classN);
54  static string _sermultiset(void* t,string classN);
55  static string _serq(void* t,string classN);
56  static string _serdq(void* t,string classN);
57  static string _ser(Object);
58  static void* _unser(string objXml,string classN);
59  static void* unserializevec(string objXml);
60  static void* unserializelist(string objXml);
61  static void* unserializeset(string objXml);
62  static void* unserializemultiset(string objXml);
63  static void* unserializeq(string objXml);
64  static void* unserializedq(string objXml);
65 
66  static void* _unserVec(string objXml,string className);
67  static void* _unserLis(string objXml,string className);
68  static void* _unserQ(string objXml,string className);
69  static void* _unserDq(string objXml,string className);
70  static void* _unserSet(string objXml,string className);
71  static void* _unserMulSet(string objXml,string className);
72 
73  template <class T> static string serializeset(set<T> t)
74  {
75  set<T> st = t;
76  T td;
77  int cnt = 0;
78  const char *mangled = typeid(td).name();
79  string className = demangle(mangled);
80  string objXml = "<set-"+className+">";
81  while(cnt++<(int)t.size())
82  {
83  objXml += serialize<T>(*st.begin());
84  st.erase(st.begin());
85  }
86  objXml += "</set-"+className+">";
87  return objXml;
88  }
89 
90  template <class T> static string serializemultiset(multiset<T> t)
91  {
92  multiset<T> st = t;
93  T td;
94  int cnt = 0;
95  const char *mangled = typeid(td).name();
96  string className = demangle(mangled);
97  string objXml = "<multiset-"+className+">";
98  while(cnt++<(int)t.size())
99  {
100  objXml += serialize<T>(*st.begin());
101  st.erase(st.begin());
102  }
103  objXml += "</multiset-"+className+">";
104  return objXml;
105  }
106 
107  template <class T> static string serializevec(vector<T> t)
108  {
109  vector<T> st = t;
110  T td;
111  int cnt = 0;
112  const char *mangled = typeid(td).name();
113  string className = demangle(mangled);
114  string objXml = "<vector-"+className+">";
115  while(cnt++<(int)t.size())
116  {
117  objXml += serialize<T>(*st.begin());
118  st.erase(st.begin());
119  }
120  objXml += "</vector-"+className+">";
121  return objXml;
122  }
123  template <class T> static string serializedq(deque<T> t)
124  {
125  deque<T> st = t;
126  T td;
127  int cnt = 0;
128  const char *mangled = typeid(td).name();
129  string className = demangle(mangled);
130  string objXml = "<deque-"+className+">";
131  while(cnt++<(int)t.size())
132  {
133  objXml += serialize<T>(*st.begin());
134  st.erase(st.begin());
135  }
136  objXml += "</deque-"+className+">";
137  return objXml;
138  }
139  template <class T> static string serializelist(list<T> t)
140  {
141  list<T> st = t;
142  T td;
143  int cnt = 0;
144  const char *mangled = typeid(td).name();
145  string className = demangle(mangled);
146  list<string>::iterator it;
147  string objXml = "<list-"+className+">";
148  while(cnt++<(int)t.size())
149  {
150  objXml += serialize<T>(*st.begin());
151  st.erase(st.begin());
152  }
153  objXml += "</list-"+className+">";
154  return objXml;
155  }
156  template <class T> static string serializeq(std::queue<T> t)
157  {
158  std::queue<T> tt = t;
159  T td;
160  const char *mangled = typeid(td).name();
161  string className = demangle(mangled);
162  string objXml = "<queue-"+className+">";
163  if(!tt.empty())
164  {
165  for(int var=0;var<(int)tt.size();var++)
166  {
167  objXml += serialize<T>(tt.front());
168  tt.pop();
169  }
170  }
171  objXml += "</queue-"+className+">";
172  return objXml;
173  }
174 
175 public:
176  XMLSerialize(){}
177  ~XMLSerialize(){}
178 
179  template <class T> static string serialize(T t)
180  {
181  string objXml;
182  const char *mangled = typeid(t).name();
183  string className = demangle(mangled);
184  return _handleAllSerialization(className,&t);
185  }
186 
187  static string serializeObject(Object t)
188  {
189  return _handleAllSerialization(t.getTypeName(),t.getVoidPointer());
190  }
191  static string serializeUnknown(void* t,string className)
192  {
193  return _handleAllSerialization(className,t);
194  }
195 
196  template <class K,class V> static string serialize(map<K,V> mp)
197  {
198  map<K,V> mpt = mp;
199  string objXml;
200  objXml = "<map>";
201  while (mpt.begin()!=mpt.end())
202  {
203  objXml += "<pair>";
204  objXml += serialize<K>(mpt.begin()->first);
205  objXml += serialize<V>(mpt.begin()->second);
206  mpt.erase(mpt.begin());
207  objXml += "</pair>";
208  }
209  objXml += "</map>";
210  return objXml;
211  }
212  template <class K,class V> static string serialize(multimap<K,V> mp)
213  {
214  multimap<K,V> mpt = mp;
215  string objXml;
216  objXml = "<multimap>";
217  while (mpt.begin()!=mpt.end())
218  {
219  objXml += "<pair>";
220  objXml += serialize<K>(mpt.begin()->first);
221  objXml += serialize<V>(mpt.begin()->second);
222  mpt.erase(mpt.begin());
223  objXml += "</pair>";
224  }
225  objXml += "</multimap>";
226  return objXml;
227  }
228 
229  template <class T> static T unserialize(string objXml)
230  {
231  T t;
232  const char *mangled = typeid(t).name();
233  string className = demangle(mangled);
234  return *(T*)_handleAllUnSerialization(objXml,className);
235  }
236  static void* unSerializeUnknown(string objXml,string className)
237  {
238  return _handleAllUnSerialization(objXml,className);
239  }
240 };
241 #endif