00001 #ifndef __COLLECTION_SERIALIZERS_H
00002 #define __COLLECTION_SERIALIZERS_H
00003
00004 #include "Serializable.h"
00005 #include "XmlRpcCollection.h"
00006
00012 namespace mace {
00013
00015
00019 template<typename Value>
00020 class _SerializeArray : public Serializable {
00021 public:
00023 class iterator {
00024 public:
00025 virtual bool isValid() = 0;
00026 virtual void next() = 0;
00027 virtual Value& getValue() = 0;
00028 virtual ~iterator() {}
00029 };
00031 class const_iterator {
00032 public:
00033 virtual bool isValid() = 0;
00034 virtual void next() = 0;
00035 virtual const Value& getValue() = 0;
00036 virtual ~const_iterator() {}
00037 };
00038
00039 protected:
00040 virtual iterator* getIterator() { ABORT("getIterator"); }
00041 virtual const_iterator* getIterator() const { ABORT("getIterator"); }
00042
00043 public:
00045
00049 void serialize(std::string& str) const {
00050 const_iterator* i = getIterator();
00051 for(; i->isValid(); i->next()) {
00052 mace::serialize(str, &(i->getValue()));
00053 }
00054 delete i;
00055 }
00057 int deserialize(std::istream& in) throw(SerializationException) {
00058 int bytes = 0;
00059 iterator* i = getIterator();
00060 for (; i->isValid(); i->next()) {
00061 bytes += mace::deserialize(in, &(i->getValue()));
00062 }
00063 delete i;
00064 return bytes;
00065 }
00067 int deserializeXML_RPC(std::istream& in) throw(SerializationException) {
00068 long offset = in.tellg();
00069
00070
00071 SerializationUtil::expectTag(in, "<array>");
00072 SerializationUtil::expectTag(in, "<data>");
00073 std::string tag = SerializationUtil::getTag(in);
00074 iterator* i = getIterator();
00075 for (; i->isValid(); i->next()) {
00076 SerializationUtil::expectTag(in, "<value>");
00077 mace::deserializeXML_RPC(in, &(i->getValue()), i->getValue());
00078 SerializationUtil::expectTag(in, "</value>");
00079 tag = SerializationUtil::getTag(in);
00080 }
00081 delete i;
00082 SerializationUtil::expectTag(in, "</data>");
00083 SerializationUtil::expectTag(in, "</array>");
00084
00085 return (long)in.tellg() - offset;
00086 }
00088 void serializeXML_RPC(std::string& str) const throw(SerializationException) {
00089 str.append("<array><data>");
00090 const_iterator* i = getIterator();
00091 for(; i->isValid(); i->next()) {
00092 str.append("<value>");
00093 mace::serializeXML_RPC(str, &(i->getValue()), i->getValue());
00094 str.append("</value>");
00095 }
00096 delete i;
00097 str.append("</data></array>");
00098 }
00099 };
00100
00102 template<typename Value>
00103 class SerializeArray : public _SerializeArray<Value> {
00104 BOOST_CLASS_REQUIRE(Value, mace, SerializeConcept);
00105 };
00106
00107
00109
00115 template<typename Value>
00116 class _SerializeList : public _SerializeArray<Value> {
00117 public:
00118 virtual void resize(size_t sz) { ABORT("resize"); };
00119 virtual size_t size() const { ABORT("size"); };
00120 virtual void add(const Value& v) { ABORT("add"); }
00121
00123 void serialize(std::string& str) const {
00124 uint32_t sz = (uint32_t)size();
00125 mace::serialize(str, &sz);
00126 _SerializeArray<Value>::serialize(str);
00127 }
00129 int deserialize(std::istream& in) throw(SerializationException) {
00130 uint32_t sz;
00131 int bytes = sizeof(sz);
00132
00133 mace::deserialize(in, &sz);
00134 resize(sz);
00135
00136 bytes += _SerializeArray<Value>::deserialize(in);
00137 return bytes;
00138 }
00140 int deserializeXML_RPC(std::istream& in) throw(SerializationException) {
00141 long offset = in.tellg();
00142
00143 resize(0);
00144 SerializationUtil::expectTag(in, "<array>");
00145 SerializationUtil::expectTag(in, "<data>");
00146 std::string tag = SerializationUtil::getTag(in);
00147 while (tag == "<value>") {
00148 Value v;
00149 mace::deserializeXML_RPC(in, &(v), v);
00150 add(v);
00151 SerializationUtil::expectTag(in, "</value>");
00152 tag = SerializationUtil::getTag(in);
00153 }
00154
00155 if (tag != "</data>") {
00156 throw SerializationException("error parsing tag: expected </data>, parsed " + tag);
00157 }
00158
00159 SerializationUtil::expectTag(in, "</array>");
00160
00161 return (long)in.tellg() - offset;
00162 }
00163 };
00164
00166 template<typename Value>
00167 class SerializeList : public _SerializeList<Value> {
00168 BOOST_CLASS_REQUIRE(Value, mace, SerializeConcept);
00169 };
00170
00172
00181 template<typename Value>
00182 class _SerializeSet : public Serializable {
00183 public:
00185 class const_iterator {
00186 public:
00187 virtual bool isValid() = 0;
00188 virtual void next() = 0;
00189 virtual const Value& getValue() = 0;
00190 virtual ~const_iterator() {}
00191 };
00192
00193 protected:
00194 virtual const_iterator* getIterator() const { ABORT("getIterator"); }
00195 virtual void add(const Value& v) { ABORT("add"); }
00196
00197 public:
00198 virtual void clear() { ABORT("clear"); }
00199 virtual size_t size() const { ABORT("size"); }
00200
00202 void serialize(std::string& str) const {
00203 uint32_t sz = (uint32_t)size();
00204 mace::serialize(str, &sz);
00205 const_iterator* i = getIterator();
00206 for(; i->isValid(); i->next()) {
00207 mace::serialize(str, &(i->getValue()));
00208 }
00209 delete i;
00210 }
00212 int deserialize(std::istream& in) throw(SerializationException) {
00213 Value v;
00214 uint32_t sz;
00215 int bytes = sizeof(sz);
00216
00217
00218 clear();
00219 mace::deserialize(in, &sz);
00220 for (uint32_t i = 0; i < sz; i++) {
00221 bytes += mace::deserialize(in, &v);
00222 add(v);
00223
00224 }
00225 return bytes;
00226 }
00228 int deserializeXML_RPC(std::istream& in) throw(SerializationException) {
00229 long offset = in.tellg();
00230
00231 clear();
00232 SerializationUtil::expectTag(in, "<array>");
00233 SerializationUtil::expectTag(in, "<data>");
00234 std::string tag = SerializationUtil::getTag(in);
00235 while (tag == "<value>") {
00236 Value v;
00237 mace::deserializeXML_RPC(in, &(v), v);
00238 add(v);
00239 SerializationUtil::expectTag(in, "</value>");
00240 tag = SerializationUtil::getTag(in);
00241 }
00242
00243 if (tag != "</data>") {
00244 throw SerializationException("error parsing tag: expected </data>, parsed " + tag);
00245 }
00246
00247 SerializationUtil::expectTag(in, "</array>");
00248
00249 return (long)in.tellg() - offset;
00250 }
00252 void serializeXML_RPC(std::string& str) const throw(SerializationException) {
00253 str.append("<array><data>");
00254 const_iterator* i = getIterator();
00255 for(; i->isValid(); i->next()) {
00256 str.append("<value>");
00257 mace::serializeXML_RPC(str, &(i->getValue()), i->getValue());
00258 str.append("</value>");
00259 }
00260 delete i;
00261 str.append("</data></array>");
00262 }
00263 };
00264
00266 template<typename Value>
00267 class SerializeSet : public _SerializeSet<Value> {
00268 BOOST_CLASS_REQUIRE(Value, mace, SerializeConcept);
00269 };
00270
00272
00277 template<typename Key, typename Value>
00278 class _SerializeMap : public Serializable {
00279
00280 public:
00281
00282
00283
00284
00285
00286
00287
00288
00290 class const_iterator {
00291 public:
00292 virtual bool isValid() = 0;
00293 virtual void next() = 0;
00294 virtual const Key& getKey() = 0;
00295 virtual const Value& getValue() = 0;
00296 virtual ~const_iterator() {}
00297 };
00298
00299 protected:
00300 virtual Value& insertKey(const Key& k) { ABORT("insertKey"); }
00301 virtual const_iterator* getIterator() const { ABORT("getIterator"); }
00302
00303 public:
00304 virtual void clear() { ABORT("clear"); }
00305 virtual size_t size() const { ABORT("size"); }
00306
00308 void serialize(std::string& str) const {
00309 uint32_t sz = (uint32_t)size();
00310 mace::serialize(str, &sz);
00311 const_iterator* i = getIterator();
00312 for(; i->isValid(); i->next()) {
00313 mace::serialize(str, &(i->getKey()));
00314 mace::serialize(str, &(i->getValue()));
00315 }
00316 delete i;
00317 }
00319 int deserialize(std::istream& in) throw(SerializationException) {
00320 Key k;
00321 uint32_t sz;
00322 int bytes = sizeof(sz);
00323
00324 clear();
00325 mace::deserialize(in, &sz);
00326 for (uint32_t i = 0; i < sz; i++) {
00327 bytes += mace::deserialize(in, &k);
00328 bytes += mace::deserialize(in, &insertKey(k));
00329
00330 }
00331 return bytes;
00332 }
00334 int deserializeXML_RPC(std::istream& in) throw(SerializationException) {
00335 std::istream::pos_type offset = in.tellg();
00336
00337 clear();
00338
00339 SerializationUtil::expectTag(in, "<struct>");
00340
00341 std::string tag = SerializationUtil::getTag(in);
00342 while (tag == "<member>") {
00343
00344
00345 Key k;
00346
00347
00348 SerializationUtil::expectTag(in, "<name>");
00349 k = KeyTraits<Key>::extract(in);
00350 Value& d = insertKey(k);
00351
00352
00353 SerializationUtil::expectTag(in, "</name>");
00354 SerializationUtil::expectTag(in, "<value>");
00355 mace::deserializeXML_RPC(in, &d, d);
00356 SerializationUtil::expectTag(in, "</value>");
00357 SerializationUtil::expectTag(in, "</member>");
00358
00359 tag = SerializationUtil::getTag(in);
00360
00361 }
00362
00363 if (tag != "</struct>") {
00364 throw SerializationException("error parsing tag: expected </struct>, parsed " + tag);
00365 }
00366
00367
00368 return in.tellg() - offset;
00369 }
00371 void serializeXML_RPC(std::string& str) const throw(SerializationException) {
00372 if(!KeyTraits<Key>::isString() && !KeyTraits<Key>::isInt()) {
00373 throw SerializationException("Bad key type for map serialization");
00374 }
00375 str.append("<struct>");
00376 const_iterator* i = getIterator();
00377 for(; i->isValid(); i->next()) {
00378 str.append("<member>");
00379 str.append("<name>");
00380 KeyTraits<Key>::append(str, i->getKey());
00381 str.append("</name>");
00382 str.append("<value>");
00383 mace::serializeXML_RPC(str, &(i->getValue()), i->getValue());
00384 str.append("</value>");
00385 str.append("</member>");
00386 }
00387 delete i;
00388 str.append("</struct>");
00389 }
00390 };
00391
00393 template<typename Key, typename Value>
00394 class SerializeMap : public _SerializeMap<Key, Value> {
00395 BOOST_CLASS_REQUIRE(Key, mace, SerializeConcept);
00396 BOOST_CLASS_REQUIRE(Value, mace, SerializeConcept);
00397 };
00398
00399 }
00400
00401 #endif //__COLLECTION_SERIALIZERS_H