SerializationInfo.h
1 /*
2  * Copyright (C) 2005-2013 by Dr. Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_SerializationInfo_h
30 #define Pt_SerializationInfo_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/String.h>
34 #include <Pt/Types.h>
35 #include <Pt/LiteralPtr.h>
36 #include <Pt/FixupInfo.h>
37 #include <Pt/TextCodec.h>
38 #include <Pt/SerializationError.h>
39 #include <Pt/SerializationSurrogate.h>
40 #include <typeinfo>
41 #include <limits>
42 #include <vector>
43 #include <set>
44 #include <map>
45 #include <list>
46 #include <deque>
47 #include <cstring>
48 
49 namespace Pt {
50 
51 class SerializationContext;
52 class Formatter;
53 
58 class PT_API SerializationInfo
59 {
60  public:
61  class Iterator;
62  class ConstIterator;
63 
65  enum Type {
66  Void = 0,
67  Context = 1,
68  Reference = 2,
69  Boolean = 3,
70  Char = 4,
71  Str = 5,
72  Int8 = 6,
73  Int16 = 7,
74  Int32 = 8,
75  Int64 = 9,
76  UInt8 = 10,
77  UInt16 = 11,
78  UInt32 = 12,
79  UInt64 = 13,
80  Float = 14,
81  Double = 15,
82  LongDouble = 16,
83  Binary = 17,
84  Struct = 18,
85  Sequence = 19,
86  Dict = 20,
87  DictElement= 21
88  };
89 
90  public:
94  : _context(0)
95  , _parent(0)
96  , _next(0)
97  , _Name("")
98  , _TypeName("")
99  , _id("")
100  , _bound(false)
101  , _isCompound(false)
102  , _isAlloc(false)
103  , _type(Void)
104  , _flags(0)
105  { }
106 
110  : _context(context)
111  , _parent(0)
112  , _next(0)
113  , _Name("")
114  , _TypeName("")
115  , _id("")
116  , _bound(false)
117  , _isCompound(false)
118  , _isAlloc(false)
119  , _type(Void)
120  , _flags(0)
121  { }
122 
126 
127  public:
130  void clear();
131 
134  inline Type type() const
135  { return static_cast<Type>(_type); }
136 
139  inline bool isVoid() const
140  { return _type == Void; }
141 
144  void setVoid();
145 
148  inline bool isScalar() const
149  { return _isCompound == false; }
150 
153  inline bool isStruct() const
154  { return _type == Struct; }
155 
158  inline bool isDict() const
159  { return _type == Dict; }
160 
163  inline bool isSequence() const
164  { return _type == Sequence; }
165 
168  inline bool isReference() const
169  { return _type == Reference; }
170 
173  void setSequence();
174 
177  void setDict();
178 
181  void setStruct();
182 
186  { return _context; }
187 
189  template <typename T>
190  bool compose(T& type) const;
191 
193  template <typename T>
194  bool decompose(const T& type);
195 
198  void rebind(void* obj) const;
199 
202  void rebindFixup(void* obj) const;
203 
207  { return _parent; }
208 
211  const SerializationInfo* parent() const
212  { return _parent; }
213 
216  const char* typeName() const
217  { return _TypeName; }
218 
221  void setTypeName(const std::string& type);
222 
225  void setTypeName(const char* type);
226 
229  void setTypeName(const char* type, std::size_t len);
230 
233  void setTypeName(const LiteralPtr<char>& type);
234 
237  const char* name() const
238  { return _Name; }
239 
242  void setName(const std::string& name);
243 
246  void setName(const char* name);
247 
250  void setName(const char* type, std::size_t len);
251 
254  void setName(const LiteralPtr<char>& type);
255 
258  const char* id() const
259  { return _id; }
260 
263  void setId(const std::string& id);
264 
267  void setId(const char* id);
268 
271  void setId(const char* id, std::size_t len);
272 
275  void getString(std::string& s, const TextCodec<Pt::Char, char>& codec) const;
276 
279  void getString(std::string& s) const;
280 
283  void getString(Pt::String& s) const;
284 
287  void setString(const char* s);
288 
291  void setString(const char* s, std::size_t len, const TextCodec<Pt::Char, char>& codec);
292 
295  void setString(const std::string& s);
296 
299  void setString(const std::string& str, const TextCodec<Pt::Char, char>& codec)
300  { setString(str.c_str(), str.size(), codec); }
301 
304  void setString(const Pt::String& s)
305  { setString( s.c_str(), s.length() ); }
306 
309  void setString(const Pt::Char* s, std::size_t len);
310 
313  const char* getBinary(std::size_t& length) const;
314 
317  void setBinary(const char* data, std::size_t length);
318 
321  void getChar(char c) const;
322 
325  void setChar(char c);
326 
329  void getChar(Pt::Char& c) const;
330 
333  void setChar(const Pt::Char& c);
334 
337  void getBool(bool& b) const;
338 
341  void setBool(bool b);
342 
345  void getInt8(Pt::int8_t& n) const;
346 
349  void setInt8(Pt::int8_t n);
350 
353  void getInt16(Pt::int16_t& n) const;
354 
357  void setInt16(Pt::int16_t n);
358 
361  void getInt32(Pt::int32_t& i) const;
362 
365  void setInt32(Pt::int32_t n);
366 
369  void getInt64(Pt::int64_t& l) const;
370 
373  void setInt64(Pt::int64_t l);
374 
377  void getUInt8(Pt::uint8_t& n) const;
378 
381  void setUInt8(Pt::uint8_t n);
382 
385  void getUInt16(Pt::uint16_t& n) const;
386 
389  void setUInt16(Pt::uint16_t n);
390 
393  void getUInt32(Pt::uint32_t& n) const;
394 
397  void setUInt32(Pt::uint32_t n);
398 
401  void getUInt64(Pt::uint64_t& n) const;
402 
405  void setUInt64(Pt::uint64_t n);
406 
409  void getFloat(float& f) const;
410 
413  void setFloat(float f);
414 
417  void getDouble(double& f) const;
418 
421  void setDouble(double f);
422 
425  void getLongDouble(long double& d) const;
426 
429  void setLongDouble(long double d);
430 
433  bool beginSave(const void* p);
434 
437  void finishSave();
438 
441  void beginLoad(void* p, const std::type_info& ti) const;
442 
445  void finishLoad() const;
446 
449  SerializationInfo& addMember(const std::string& name)
450  { return this->addMember( name.c_str(), name.length() ); }
451 
454  SerializationInfo& addMember(const char* name)
455  { return this->addMember(name, std::strlen(name)); }
456 
459  SerializationInfo& addMember(const char* name, std::size_t len);
460 
463  SerializationInfo& addMember(const LiteralPtr<char>& name);
464 
467  void removeMember(const std::string& name)
468  { return this->removeMember( name.c_str() ); }
469 
472  void removeMember(const char* name);
473 
476  SerializationInfo& addElement();
477 
480  SerializationInfo& addDictElement();
481 
484  SerializationInfo& addDictKey();
485 
488  SerializationInfo& addDictValue();
489 
492  const SerializationInfo& getMember(const std::string& name) const
493  { return this->getMember( name.c_str() ); }
494 
497  const SerializationInfo& getMember(const char* name) const;
498 
504  const SerializationInfo* findMember(const std::string& name) const
505  { return this->findMember( name.c_str() ); }
506 
512  const SerializationInfo* findMember(const char* name) const;
513 
519  SerializationInfo* findMember(const std::string& name)
520  { return this->findMember( name.c_str() ); }
521 
527  SerializationInfo* findMember(const char* name);
528 
531  std::size_t memberCount() const;
532 
535  SerializationInfo* sibling() const
536  { return _next; }
537 
540  void setSibling(SerializationInfo* si)
541  { _next = si; }
542 
545  Iterator begin();
546 
549  Iterator end();
550 
553  ConstIterator begin() const;
554 
557  ConstIterator end() const;
558 
561  void setReference(const void* ref);
562 
565  void setReference(const std::string& id)
566  { setReference( id.c_str(), id.length() ); }
567 
570  void setReference(const char* id, std::size_t idlen);
571 
574  template <typename T>
575  void loadReference(T& fixme, unsigned mid = 0) const
576  {
577  this->load(&fixme, FixupThunk<T>::fixupReference, mid);
578  }
579 
582  template <typename T>
583  void loadPointer(T*& fixme, unsigned mid = 0) const
584  {
585  this->load(&fixme, FixupThunk<T>::fixupPointer, mid);
586  }
587 
590  Iterator beginFormat(Formatter& formatter);
591 
594  void endFormat(Formatter& formatter);
595 
598  void format(Formatter& formatter) const;
599 
600  protected:
602  void setContextual(SerializationContext& ctx);
603 
605  template <typename T>
606  const BasicSerializationSurrogate<T>* getSurrogate() const;
607 
609  template <typename T>
610  friend const BasicSerializationSurrogate<T>* getSurrogate(SerializationInfo*);
611 
613  const SerializationSurrogate* getSurrogate(const std::type_info& ti) const;
614 
616  void load(void* fixme, FixupInfo::FixupHandler fh, unsigned mid) const;
617 
619  void clearValue();
620 
622  SerializationInfo& addChild();
623 
624  private:
626  {}
627 
628  SerializationInfo& operator=(const SerializationInfo&)
629  { return *this; }
630 
631  private:
632  struct Ref
633  {
634  void* address;
635  char* refId;
636  };
637 
638  struct BlobValue
639  {
640  char* data;
641  std::size_t length;
642  };
643 
644  struct StrValue
645  {
646  Pt::Char* str;
647  std::size_t length;
648  };
649 
650  struct Seq
651  {
652  SerializationInfo* first;
653  SerializationInfo* last;
654  std::size_t size;
655  };
656 
657  union Variant
658  {
659  bool b;
660  uint32_t ui32;
661  long long l;
662  unsigned long long ul;
663  long double f;
664  StrValue ustr;
665  BlobValue blob;
666  Ref ref;
667  Seq seq;
668  };
669 
670  private:
671  mutable Variant _value;
672  SerializationContext* _context;
673  SerializationInfo* _parent;
674  SerializationInfo* _next;
675  const char* _Name;
676  const char* _TypeName;
677  const char* _id;
678  mutable bool _bound; // TODO: join into bitfield
679  bool _isCompound; // TODO: join into bitfield
680  bool _isAlloc; // TODO: join into bitfield
681  Pt::uint8_t _type; // TODO: join into bitfield
682  Pt::uint8_t _flags;
683 
684  // TODO: possible type info layout
685  // 0 - public / private
686  // 1 - scalar / compound
687  // 2 - type id
688  // 3 - type id
689  // 4 - type id
690  // 5 - type id
691  // 6 - type id
692  // 7 - type id
693 };
694 
695 
696 template <typename T>
697 inline const BasicSerializationSurrogate<T>* SerializationInfo::getSurrogate() const
698 {
699  const SerializationSurrogate* surr = this->getSurrogate( typeid(T) );
700  if( ! surr )
701  return 0;
702 
703  return static_cast<const BasicSerializationSurrogate<T>*>(surr);
704 }
705 
706 
707 template <typename T>
708 inline bool SerializationInfo::compose(T& type) const
709 {
710  const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
711  if( ! surr )
712  return false;
713 
714  surr->compose(*this, type);
715  return true;
716 }
717 
718 
719 template <typename T>
720 inline bool SerializationInfo::decompose(const T& type)
721 {
722  const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
723  if( ! surr )
724  return false;
725 
726  surr->decompose(*this, type);
727  this->setTypeName( surr->typeName() );
728  return true;
729 }
730 
734 {
735  public:
736  Iterator()
737  : _si(0)
738  {}
739 
740  Iterator(const Iterator& other)
741  : _si(other._si)
742  {}
743 
744  explicit Iterator(SerializationInfo* si)
745  : _si(si)
746  {}
747 
748  Iterator& operator=(const Iterator& other)
749  {
750  _si = other._si;
751  return *this;
752  }
753 
754  Iterator& operator++()
755  {
756  _si = _si->sibling();
757  return *this;
758  }
759 
760  SerializationInfo& operator*() const
761  { return *_si; }
762 
763  SerializationInfo* operator->() const
764  { return _si; }
765 
766  bool operator!=(const Iterator& other) const
767  { return _si != other._si; }
768 
769  bool operator==(const Iterator& other) const
770  { return _si == other._si; }
771 
772  private:
773  SerializationInfo* _si;
774 };
775 
779 {
780  public:
781  ConstIterator()
782  : _si(0)
783  {}
784 
785  ConstIterator(const ConstIterator& other)
786  : _si(other._si)
787  {}
788 
789  explicit ConstIterator(const SerializationInfo* si)
790  : _si(si)
791  {}
792 
793  ConstIterator& operator=(const ConstIterator& other)
794  {
795  _si = other._si;
796  return *this;
797  }
798 
799  ConstIterator& operator++()
800  {
801  _si = _si->sibling();
802  return *this;
803  }
804 
805  const SerializationInfo& operator*() const
806  { return *_si; }
807 
808  const SerializationInfo* operator->() const
809  { return _si; }
810 
811  bool operator!=(const ConstIterator& other) const
812  { return _si != other._si; }
813 
814  bool operator==(const ConstIterator& other) const
815  { return _si == other._si; }
816 
817  private:
818  const SerializationInfo* _si;
819 };
820 
821 
823 {
825 }
826 
827 
829 {
831 }
832 
833 
838 class SaveInfo
839 {
840  public:
842  explicit SaveInfo(SerializationInfo& info)
843  : si(&info)
844  {}
845 
849  { return *si; }
850 
853  template <typename T>
854  bool save(const T& type)
855  {
856  bool first = si->beginSave( &type );
857  if(first)
858  {
859  *si <<= type;
860  si->finishSave();
861  }
862 
863  return first;
864  }
865 
866  SerializationInfo* si;
867 };
868 
870 struct Save
871 {};
872 
874 inline Save save()
875 {
876  return Save();
877 }
878 
880 inline SaveInfo operator <<(SerializationInfo& si, const Save&)
881 {
882  return SaveInfo(si);
883 }
884 
886 template <typename T>
887 inline void operator <<=(SaveInfo info, const T& type)
888 {
889  save( info, type );
890 }
891 
897 template <typename T>
898 inline void save(SaveInfo& si, const T& type)
899 {
900  if( ! si.save(type) )
901  {
902  si.out() <<= type;
903  }
904 }
905 
906 
911 class LoadInfo
912 {
913  public:
915  explicit LoadInfo(const SerializationInfo& info)
916  : si(&info)
917  {}
918 
921  const SerializationInfo& in() const
922  { return *si; }
923 
926  template <typename T>
927  void load(T& type) const
928  {
929  T* tp = &type;
930 
931  si->beginLoad( tp, typeid(T) );
932  *si >>= type;
933  si->finishLoad();
934  }
935 
936  private:
937  const SerializationInfo* si;
938 };
939 
941 struct Load
942 {};
943 
945 inline Load load()
946 {
947  return Load();
948 }
949 
951 inline LoadInfo operator >>(const SerializationInfo& si, const Load&)
952 {
953  return LoadInfo(si);
954 }
955 
957 template <typename T>
958 inline void operator >>=(const LoadInfo& li, T& type)
959 {
960  load(li, type);
961 }
962 
968 template <typename T>
969 inline void load(const LoadInfo& li, T& type)
970 {
971  li.load(type);
972 }
973 
974 
979 template <typename T>
980 inline void operator >>=(const SerializationInfo& si, T*& ptr)
981 {
982  si.loadPointer(ptr);
983 }
984 
989 template <typename T>
990 inline void operator <<=(SerializationInfo& si, const T* ptr)
991 {
992  si.setReference( ptr );
993 }
994 
999 inline void operator >>=(const SerializationInfo& si, bool& n)
1000 {
1001  si.getBool(n);
1002 }
1003 
1008 inline void operator <<=(SerializationInfo& si, bool n)
1009 {
1010  si.setBool(n);
1011 }
1012 
1013 
1018 inline void operator >>=(const SerializationInfo& si, Pt::int8_t& n)
1019 {
1020  si.getInt8(n);
1021 }
1022 
1027 inline void operator <<=(SerializationInfo& si, Pt::int8_t n)
1028 {
1029  si.setInt8(n);
1030 }
1031 
1036 inline void operator >>=(const SerializationInfo& si, Pt::int16_t& n)
1037 {
1038  si.getInt16(n);
1039 }
1040 
1045 inline void operator <<=(SerializationInfo& si, Pt::int16_t n)
1046 {
1047  si.setInt16(n);
1048 }
1049 
1054 inline void operator >>=(const SerializationInfo& si, Pt::int32_t& n)
1055 {
1056  si.getInt32(n);
1057 }
1058 
1063 inline void operator <<=(SerializationInfo& si, Pt::int32_t n)
1064 {
1065  si.setInt32(n);
1066 }
1067 
1072 inline void operator >>=(const SerializationInfo& si, Pt::int64_t& n)
1073 {
1074  si.getInt64(n);
1075 }
1076 
1081 inline void operator <<=(SerializationInfo& si, Pt::int64_t n)
1082 {
1083  si.setInt64(n);
1084 }
1085 
1090 inline void operator >>=(const SerializationInfo& si, Pt::uint8_t& n)
1091 {
1092  si.getUInt8(n);
1093 }
1094 
1099 inline void operator <<=(SerializationInfo& si, Pt::uint8_t n)
1100 {
1101  si.setUInt8(n);
1102 }
1103 
1108 inline void operator >>=(const SerializationInfo& si, Pt::uint16_t& n)
1109 {
1110  si.getUInt16(n);
1111 }
1112 
1117 inline void operator <<=(SerializationInfo& si, Pt::uint16_t n)
1118 {
1119  si.setUInt16(n);
1120 }
1121 
1126 inline void operator >>=(const SerializationInfo& si, Pt::uint32_t& n)
1127 {
1128  si.getUInt32(n);
1129 }
1130 
1135 inline void operator <<=(SerializationInfo& si, Pt::uint32_t n)
1136 {
1137  si.setUInt32(n);
1138 }
1139 
1144 inline void operator >>=(const SerializationInfo& si, Pt::uint64_t& n)
1145 {
1146  si.getUInt64(n);
1147 }
1148 
1153 inline void operator <<=(SerializationInfo& si, Pt::uint64_t n)
1154 {
1155  si.setUInt64(n);
1156 }
1157 
1162 inline void operator >>=(const SerializationInfo& si, float& n)
1163 {
1164  si.getFloat(n);
1165 }
1166 
1171 inline void operator <<=(SerializationInfo& si, float n)
1172 {
1173  si.setFloat(n);
1174 }
1175 
1180 inline void operator >>=(const SerializationInfo& si, double& n)
1181 {
1182  si.getDouble(n);
1183 }
1184 
1189 inline void operator <<=(SerializationInfo& si, double n)
1190 {
1191  si.setDouble(n);
1192 }
1193 
1198 inline void operator >>=(const SerializationInfo& si, long double& n)
1199 {
1200  si.getLongDouble(n);
1201 }
1202 
1207 inline void operator <<=(SerializationInfo& si, long double n)
1208 {
1209  si.setLongDouble(n);
1210 }
1211 
1216 inline void operator >>=(const SerializationInfo& si, char& ch)
1217 {
1218  Pt::Char tmp;
1219  si.getChar(tmp);
1220  ch = static_cast<char>( tmp.value() );
1221 }
1222 
1227 inline void operator <<=(SerializationInfo& si, char ch)
1228 {
1229  si.setChar( Pt::Char(ch) );
1230 }
1231 
1236 inline void operator <<=(SerializationInfo& si, const char* str)
1237 {
1238  si.setString(str);
1239 }
1240 
1245 inline void operator >>=(const SerializationInfo& si, std::string& str)
1246 {
1247  si.getString(str);
1248 }
1249 
1254 inline void operator <<=(SerializationInfo& si, const std::string& str)
1255 {
1256  si.setString( str.c_str() );
1257 }
1258 
1263 inline void operator >>=(const SerializationInfo& si, Pt::String& str)
1264 {
1265  si.getString(str);
1266 }
1267 
1272 inline void operator <<=(SerializationInfo& si, const Pt::String& str)
1273 {
1274  si.setString(str);
1275 }
1276 
1281 template <typename T, typename A>
1282 inline void operator >>=(const SerializationInfo& si, std::vector<T, A>& vec)
1283 {
1284  T elem = T();
1285  vec.clear();
1286  vec.reserve( si.memberCount() );
1287 
1289  for(SerializationInfo::ConstIterator it = si.begin(); it != end; ++it)
1290  {
1291  vec.push_back(elem);
1292  *it >> Pt::load() >>= vec.back();
1293  }
1294 }
1295 
1300 template <typename T, typename A>
1301 inline void operator <<=(SerializationInfo& si, const std::vector<T, A>& vec)
1302 {
1303  typename std::vector<T, A>::const_iterator it;
1304 
1305  for(it = vec.begin(); it != vec.end(); ++it)
1306  {
1307  si.addElement() << Pt::save() <<= *it;
1308  }
1309 
1310  si.setTypeName( Pt::LiteralPtr<char>("std::vector") );
1311  si.setSequence();
1312 }
1313 
1318 template <typename T, typename A>
1319 inline void operator >>=(const SerializationInfo& si, std::list<T, A>& list)
1320 {
1321  list.clear();
1322  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1323  {
1324  list.resize( list.size() + 1 );
1325  *it >> Pt::load() >>= list.back();
1326  }
1327 }
1328 
1333 template <typename T, typename A>
1334 inline void operator <<=(SerializationInfo& si, const std::list<T, A>& list)
1335 {
1336  typename std::list<T, A>::const_iterator it;
1337 
1338  for(it = list.begin(); it != list.end(); ++it)
1339  {
1340  si.addElement() << Pt::save() <<= *it;
1341  }
1342 
1343  si.setTypeName( Pt::LiteralPtr<char>("std::list") );
1344  si.setSequence();
1345 }
1346 
1351 template <typename T, typename A>
1352 inline void operator >>=(const SerializationInfo& si, std::deque<T, A>& deque)
1353 {
1354  deque.clear();
1355  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1356  {
1357  // NOTE: push_back does not invalidate references to elements
1358  deque.push_back( T() );
1359  *it >> Pt::load() >>= deque.back();
1360  }
1361 }
1362 
1367 template <typename T, typename A>
1368 inline void operator <<=(SerializationInfo& si, const std::deque<T, A>& deque)
1369 {
1370  typename std::deque<T, A>::const_iterator it;
1371 
1372  for(it = deque.begin(); it != deque.end(); ++it)
1373  {
1374  si.addElement() << Pt::save() <<= *it;
1375  }
1376 
1377  si.setTypeName( Pt::LiteralPtr<char>("std::deque") );
1378  si.setSequence();
1379 }
1380 
1381 
1390 template <typename T, typename C, typename A>
1391 inline void operator >>=(const SerializationInfo& si, std::set<T, C, A>& set)
1392 {
1393  // typedef typename std::set<T, C, A>::iterator SetIterator;
1394  // std::pair<SetIterator, bool> pos;
1395 
1396  set.clear();
1397  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1398  {
1399  T t;
1400  *it >>= t;
1401  set.insert(t);
1402 
1403  // T t;
1404  // *it >>= Pt::load() >>= t;
1405  // pos = set.insert(t);
1406  // if( ! pos.second )
1407  // it->rebind(0);
1408 
1409  }
1410 }
1411 
1416 template <typename T, typename C, typename A>
1417 inline void operator <<=(SerializationInfo& si, const std::set<T, C, A>& set)
1418 {
1419  typename std::set<T, C, A>::const_iterator it;
1420 
1421  for(it = set.begin(); it != set.end(); ++it)
1422  {
1423  si.addElement() << Pt::save() <<= *it;
1424  }
1425 
1426  si.setTypeName( Pt::LiteralPtr<char>("std::set") );
1427  si.setSequence();
1428 }
1429 
1438 template <typename T, typename C, typename A>
1439 inline void operator >>=(const SerializationInfo& si, std::multiset<T, C, A>& multiset)
1440 {
1441  // typename std::multiset<T>::iterator pos;
1442 
1443  multiset.clear();
1444  for(Pt::SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1445  {
1446  T t;
1447  *it >>= t;
1448  multiset.insert(t);
1449 
1450  // T tmp;
1451  // *it >>= Pt::load() >>= tmp;
1452  // pos = multiset.insert(tmp);
1453 
1454  // T& t = const_cast<T&>(*pos);
1455  // it->rebind(&t);
1456  }
1457 }
1458 
1463 template <typename T, typename C, typename A>
1464 inline void operator <<=(SerializationInfo& si, const std::multiset<T, C, A>& multiset)
1465 {
1466  typename std::multiset<T, C, A>::const_iterator it;
1467 
1468  for(it = multiset.begin(); it != multiset.end(); ++it)
1469  {
1470  si.addElement() << Pt::save() <<= *it;
1471  }
1472 
1473  si.setTypeName( Pt::LiteralPtr<char>("std::multiset") );
1474  si.setSequence();
1475 }
1476 
1477 
1478 template <typename A, typename B>
1479 inline void operator >>=(const SerializationInfo& si, std::pair<A, B>& p)
1480 {
1481  si.getMember("first") >>= p.first;
1482  si.getMember("second") >>= p.second;
1483 }
1484 
1485 
1486 template <typename A, typename B>
1487 inline void operator <<=(SerializationInfo& si, const std::pair<A, B>& p)
1488 {
1489  si.setTypeName( Pt::LiteralPtr<char>("std::pair") );
1490  si.addMember( Pt::LiteralPtr<char>("first") ) <<= p.first;
1491  si.addMember( Pt::LiteralPtr<char>("second") ) <<= p.second;
1492 }
1493 
1498 template <typename K, typename V, typename P, typename A>
1499 inline void operator >>=(const SerializationInfo& si, std::map<K, V, P, A>& map)
1500 {
1501  typedef typename std::map<K, V, P, A>::iterator MapIterator;
1502  std::pair<MapIterator, bool> pos;
1503 
1504  map.clear();
1505  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1506  {
1507  K k;
1508 
1510  if( kv != it->end() )
1511  *kv >>= k;
1512 
1513  std::pair<K, V> elem( k, V() );
1514  pos = map.insert(elem);
1515 
1516  if( pos.second && ++kv != it->end() )
1517  *kv >> Pt::load() >>= pos.first->second;
1518  }
1519 }
1520 
1525 template <typename K, typename V, typename P, typename A>
1526 inline void operator <<=(SerializationInfo& si, const std::map<K, V, P, A>& map)
1527 {
1528  typename std::map<K, V, P, A>::const_iterator it;
1529 
1530  for(it = map.begin(); it != map.end(); ++it)
1531  {
1532  SerializationInfo& elem = si.addDictElement();
1533  elem.addDictKey() <<= it->first;
1534  elem.addDictValue() << Pt::save() <<= it->second;
1535  }
1536 
1537  si.setTypeName( Pt::LiteralPtr<char>("std::map") );
1538  si.setDict();
1539 }
1540 
1545 template <typename K, typename V, typename P, typename A>
1546 inline void operator >>=(const SerializationInfo& si, std::multimap<K, V, P, A>& multimap)
1547 {
1548  typename std::multimap<K, V, P, A>::iterator mit;
1549 
1550  multimap.clear();
1551  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1552  {
1553  K k;
1554 
1556  if( kv != it->end() )
1557  *kv >>= k;
1558 
1559  std::pair<K, V> elem( k, V() );
1560  mit = multimap.insert(elem);
1561 
1562  if( ++kv != it->end() )
1563  *kv >> Pt::load() >>= mit->second;
1564  }
1565 }
1566 
1571 template <typename T, typename C, typename P, typename A>
1572 inline void operator <<=(SerializationInfo& si, const std::multimap<T, C, P, A>& multimap)
1573 {
1574  typename std::multimap<T, C, P, A>::const_iterator it;
1575 
1576  for(it = multimap.begin(); it != multimap.end(); ++it)
1577  {
1578  SerializationInfo& elem = si.addDictElement();
1579  elem.addDictKey() <<= it->first;
1580  elem.addDictValue() << Pt::save() <<= it->second;
1581  }
1582 
1583  si.setTypeName( Pt::LiteralPtr<char>("std::multimap") );
1584  si.setDict();
1585 }
1586 
1587 } // namespace Pt
1588 
1589 #endif // Pt_SerializationInfo_h
void getUInt64(Pt::uint64_t &n) const
Get value as a 64-bit unsigned integer.
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
void setReference(const std::string &id)
Set to reference with ID to fixup.
Definition: SerializationInfo.h:565
const SerializationInfo * findMember(const std::string &name) const
Find a struct member.
Definition: SerializationInfo.h:504
void getChar(char c) const
Get value as a character.
Const forward iterator for child elements.
Definition: SerializationInfo.h:778
Type type() const
Returns the type identifier.
Definition: SerializationInfo.h:134
const char * name() const
Returns the instance name.
Definition: SerializationInfo.h:237
void finishSave()
Finish saving.
int_type int16_t
Signed 16-bit integer type.
Definition: Types.h:24
void setReference(const void *ref)
Set to reference for which to create an ID.
SerializationInfo & out() const
Returns the SerializationInfo to save to.
Definition: SerializationInfo.h:848
Loads referencable types.
Definition: SerializationInfo.h:911
void getUInt16(Pt::uint16_t &n) const
Get value as a 16-bit unsigned integer.
SerializationInfo()
Default constructor.
Definition: SerializationInfo.h:93
SerializationInfo * parent()
Returns the parent node.
Definition: SerializationInfo.h:206
void finishLoad() const
Finish loading.
std::size_t memberCount() const
Returns the number of members.
void getBool(bool &b) const
Get value as a bool.
bool isReference() const
Returns true if reference.
Definition: SerializationInfo.h:168
bool isStruct() const
Returns true if struct type.
Definition: SerializationInfo.h:153
Iterator end()
Returns an iterator to the end of child elements.
Definition: SerializationInfo.h:822
void setDouble(double f)
Set to double value.
Type
Type identifier.
Definition: SerializationInfo.h:65
const SerializationInfo & in() const
Returns the SerializationInfo to load from.
Definition: SerializationInfo.h:921
void getLongDouble(long double &d) const
Get value as a long double.
void save(SaveInfo &si, const T &type)
Saves referencable types.
Definition: SerializationInfo.h:898
void setUInt16(Pt::uint16_t n)
Set to 16-bit unsigned integer value.
void beginLoad(void *p, const std::type_info &ti) const
Begin loading.
void setInt64(Pt::int64_t l)
Set to 64-bit integer value.
void getInt8(Pt::int8_t &n) const
Get value as a 8-bit integer.
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition: SerializationInfo.h:449
SerializationInfo & addDictValue()
Add a dict value.
void setInt32(Pt::int32_t n)
Set to 32-bit integer value.
void getInt32(Pt::int32_t &i) const
Get value as a 32-bit integer.
uint32_t value() const
Returns the unicode value.
Definition: String.h:90
bool beginSave(const void *p)
Begin saving.
void getUInt8(Pt::uint8_t &n) const
Get value as a 8-bit unsigned integer.
const SerializationInfo * parent() const
Returns the parent node.
Definition: SerializationInfo.h:211
void setString(const std::string &str, const TextCodec< Pt::Char, char > &codec)
Set to string value.
Definition: SerializationInfo.h:299
bool isDict() const
Returns true if dictionary type.
Definition: SerializationInfo.h:158
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition: String.h:264
bool save(const T &type)
Returns true if type was saved, false if type was already saved.
Definition: SerializationInfo.h:854
void setLongDouble(long double d)
Set to long double value.
void getInt64(Pt::int64_t &l) const
Get value as a 64-bit integer.
void setFloat(float f)
Set to float value.
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36
void setUInt32(Pt::uint32_t n)
Set to 32-bit unsigned integer value.
void setUInt64(Pt::uint64_t n)
Set to 64-bit unsigned integer value.
void loadPointer(T *&fixme, unsigned mid=0) const
Load a reference during deserialization.
Definition: SerializationInfo.h:583
SerializationInfo(SerializationContext *context)
Construct with context.
Definition: SerializationInfo.h:109
Unicode character type.
Definition: String.h:66
size_type length() const
Returns the length of the string.
Definition: String.h:234
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
Saves referencable types.
Definition: SerializationInfo.h:838
const char * typeName() const
Returns the type name.
Definition: SerializationInfo.h:216
void setBool(bool b)
Set to bool value.
void getString(std::string &s, const TextCodec< Pt::Char, char > &codec) const
Get value as a string.
bool decompose(const T &type)
Returns true if the type could be decomposed with a surrogate.
Definition: SerializationInfo.h:720
void clear()
Clears all content.
void getDouble(double &f) const
Get value as a double.
void setInt8(Pt::int8_t n)
Set to 8-bit integer value.
void setDict()
Set to dictionary type.
int_type int8_t
Signed 8-bit integer type.
Definition: Types.h:12
void setChar(char c)
Set to character value.
bool compose(T &type) const
Returns true if the type could be composed with a surrogate.
Definition: SerializationInfo.h:708
bool isSequence() const
Returns true if sequence type.
Definition: SerializationInfo.h:163
SerializationInfo & addDictElement()
Add a dict element.
Represents a pointer to a literal.
Definition: LiteralPtr.h:39
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
void load(T &type) const
Loads the type.
Definition: SerializationInfo.h:927
Unicode capable basic_string.
Definition: String.h:42
void setString(const char *s)
Set to string value.
Void type.
Definition: Void.h:43
const SerializationInfo & getMember(const std::string &name) const
Get a struct member.
Definition: SerializationInfo.h:492
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
void setInt16(Pt::int16_t n)
Set to 16-bit integer value.
void removeMember(const std::string &name)
Remove a struct member.
Definition: SerializationInfo.h:467
Context for the serialization of types.
Definition: SerializationContext.h:20
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
void getFloat(float &f) const
Get value as a float.
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:58
Iterator begin()
Returns an iterator to the begin of child elements.
void loadReference(T &fixme, unsigned mid=0) const
Load a reference during deserialization.
Definition: SerializationInfo.h:575
SerializationInfo & addDictKey()
Add a dict key.
bool isVoid() const
Returns true if not set to a type.
Definition: SerializationInfo.h:139
Support for serialization to different formats.
Definition: Formatter.h:45
void setUInt8(Pt::uint8_t n)
Set to 8-bit unsigned integer value.
void getUInt32(Pt::uint32_t &n) const
Get value as a 32-bit unsigned integer.
const char * id() const
Returns the reference ID.
Definition: SerializationInfo.h:258
void load(const LoadInfo &li, T &type)
Loads referencable types.
Definition: SerializationInfo.h:969
Forward Iterator for child elements.
Definition: SerializationInfo.h:733
SerializationInfo * findMember(const std::string &name)
Find a struct member.
Definition: SerializationInfo.h:519
SerializationContext * context() const
Returns the used context.
Definition: SerializationInfo.h:185
void getInt16(Pt::int16_t &n) const
Get value as a 16-bit integer.
void setString(const Pt::String &s)
Set to string value.
Definition: SerializationInfo.h:304
SerializationInfo & addMember(const char *name)
Add a struct member.
Definition: SerializationInfo.h:454
void setTypeName(const std::string &type)
Sets the type name.
bool isScalar() const
Returns true if scalar type.
Definition: SerializationInfo.h:148