00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _XMLRPCCOLLECTION_H
00034 #define _XMLRPCCOLLECTION_H
00035
00036 #include <string>
00037 #include "Traits.h"
00038 #include "XML_RPCSerialization.h"
00039
00047 namespace mace {
00048
00050 template<class T, class Key>
00051 void XmlRpcMapSerialize(std::string& str, const T& begin, const T& end)
00052 throw(SerializationException) {
00053 str.append("<struct>");
00054 for(T i=begin; i!=end; i++) {
00055 str.append("<member><name>");
00056 KeyTraits<Key>::append(str, i->first);
00057 str.append("</name><value>");
00058 mace::serializeXML_RPC(str, &(i->second), i->second);
00059 str.append("</value></member>");
00060 }
00061 str.append("</struct>");
00062 }
00063
00065 template<class T, class Key, class Data>
00066 int XmlRpcMapDeserialize(std:: istream& in, T& obj)
00067 throw(SerializationException) {
00068 std::istream::pos_type offset = in.tellg();
00069
00070 obj.clear();
00071
00072 SerializationUtil::expectTag(in, "<struct>");
00073
00074 std::string tag = SerializationUtil::getTag(in);
00075 while (tag == "<member>") {
00076
00077
00078 Key k;
00079 Data d;
00080
00081
00082 SerializationUtil::expectTag(in, "<name>");
00083 k = KeyTraits<Key>::extract(in);
00084
00085
00086 SerializationUtil::expectTag(in, "</name>");
00087 SerializationUtil::expectTag(in, "<value>");
00088 mace::deserializeXML_RPC(in, &d, d);
00089 SerializationUtil::expectTag(in, "</value>");
00090 SerializationUtil::expectTag(in, "</member>");
00091 obj[k] = d;
00092
00093 tag = SerializationUtil::getTag(in);
00094
00095 }
00096
00097 if (tag != "</struct>") {
00098 throw SerializationException("error parsing tag: expected </struct>, parsed " + tag);
00099 }
00100
00101
00102 return in.tellg() - offset;
00103 }
00104
00106 template<class T>
00107 void XmlRpcArraySerialize(std::string& str, const T& begin, const T& end)
00108 throw(SerializationException) {
00109 str.append("<array><data>");
00110 for(T i=begin; i!=end; i++) {
00111 str.append("<value>");
00112 mace::serializeXML_RPC(str, &(*i), *i);
00113 str.append("</value>");
00114 }
00115 str.append("</data></array>");
00116 }
00117
00119 template<class T, class Key>
00120 int XmlRpcArrayDeserialize(std::istream& in, T& obj)
00121 throw(SerializationException) {
00122 long offset = in.tellg();
00123
00124 obj.clear();
00125 SerializationUtil::expectTag(in, "<array>");
00126 SerializationUtil::expectTag(in, "<data>");
00127 std::string tag = SerializationUtil::getTag(in);
00128 while (tag == "<value>") {
00129 Key t;
00130 mace::deserializeXML_RPC(in, &t, t);
00131 obj.push_back(t);
00132 SerializationUtil::expectTag(in, "</value>");
00133 tag = SerializationUtil::getTag(in);
00134 }
00135
00136 if (tag != "</data>") {
00137 throw SerializationException("error parsing tag: expected </data>, parsed " + tag);
00138 }
00139
00140 SerializationUtil::expectTag(in, "</array>");
00141
00142 return (long)in.tellg() - offset;
00143 }
00144
00145 }
00146
00147 #endif // _XMLRPCOLLECTION_H