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 
350 
353  void getInt16(Pt::int16_t& n) const;
354 
358 
361  void getInt32(Pt::int32_t& i) const;
362 
366 
369  void getInt64(Pt::int64_t& l) const;
370 
374 
377  void getUInt8(Pt::uint8_t& n) const;
378 
382 
385  void getUInt16(Pt::uint16_t& n) const;
386 
390 
393  void getUInt32(Pt::uint32_t& n) const;
394 
398 
401  void getUInt64(Pt::uint64_t& n) const;
402 
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 
464 
467  void removeMember(const std::string& name)
468  { return this->removeMember( name.c_str() ); }
469 
472  void removeMember(const char* name);
473 
477 
481 
485 
489 
493 
496  const SerializationInfo& getMember(const std::string& name) const
497  { return this->getMember( name.c_str() ); }
498 
501  const SerializationInfo& getMember(const char* name) const;
502 
508  const SerializationInfo* findMember(const std::string& name) const
509  { return this->findMember( name.c_str() ); }
510 
516  const SerializationInfo* findMember(const char* name) const;
517 
523  SerializationInfo* findMember(const std::string& name)
524  { return this->findMember( name.c_str() ); }
525 
531  SerializationInfo* findMember(const char* name);
532 
535  std::size_t memberCount() const;
536 
539  SerializationInfo* sibling() const
540  { return _next; }
541 
544  void setSibling(SerializationInfo* si)
545  { _next = si; }
546 
550 
553  Iterator end();
554 
558 
561  ConstIterator end() const;
562 
565  void setReference(const void* ref);
566 
569  void setReference(const std::string& id)
570  { setReference( id.c_str(), id.length() ); }
571 
574  void setReference(const char* id, std::size_t idlen);
575 
578  template <typename T>
579  void loadReference(T& fixme, unsigned mid = 0) const
580  {
581  this->load(&fixme, FixupThunk<T>::fixupReference, mid);
582  }
583 
586  template <typename T>
587  void loadPointer(T*& fixme, unsigned mid = 0) const
588  {
589  this->load(&fixme, FixupThunk<T>::fixupPointer, mid);
590  }
591 
595 
598  void endFormat(Formatter& formatter);
599 
602  void format(Formatter& formatter) const;
603 
604  protected:
606  void setContextual(SerializationContext& ctx);
607 
609  template <typename T>
610  const BasicSerializationSurrogate<T>* getSurrogate() const;
611 
613  template <typename T>
614  friend const BasicSerializationSurrogate<T>* getSurrogate(SerializationInfo*);
615 
617  const SerializationSurrogate* getSurrogate(const std::type_info& ti) const;
618 
620  void load(void* fixme, FixupInfo::FixupHandler fh, unsigned mid) const;
621 
623  void clearValue();
624 
626  SerializationInfo& addChild();
627 
628  private:
630  {}
631 
632  SerializationInfo& operator=(const SerializationInfo&)
633  { return *this; }
634 
635  private:
636  struct Ref
637  {
638  void* address;
639  char* refId;
640  };
641 
642  struct BlobValue
643  {
644  char* data;
645  std::size_t length;
646  };
647 
648  struct StrValue
649  {
650  Pt::Char* str;
651  std::size_t length;
652  };
653 
654  struct Seq
655  {
656  SerializationInfo* first;
657  SerializationInfo* last;
658  std::size_t size;
659  };
660 
661  union Variant
662  {
663  bool b;
664  uint32_t ui32;
665  long long l;
666  unsigned long long ul;
667  long double f;
668  StrValue ustr;
669  BlobValue blob;
670  Ref ref;
671  Seq seq;
672  };
673 
674  private:
675  mutable Variant _value;
676  SerializationContext* _context;
677  SerializationInfo* _parent;
678  SerializationInfo* _next;
679  const char* _Name;
680  const char* _TypeName;
681  const char* _id;
682  mutable bool _bound; // TODO: join into bitfield
683  bool _isCompound; // TODO: join into bitfield
684  bool _isAlloc; // TODO: join into bitfield
685  Pt::uint8_t _type; // TODO: join into bitfield
686  Pt::uint8_t _flags;
687 
688  // TODO: possible type info layout
689  // 0 - public / private
690  // 1 - scalar / compound
691  // 2 - type id
692  // 3 - type id
693  // 4 - type id
694  // 5 - type id
695  // 6 - type id
696  // 7 - type id
697 };
698 
699 
700 template <typename T>
701 inline const BasicSerializationSurrogate<T>* SerializationInfo::getSurrogate() const
702 {
703  const SerializationSurrogate* surr = this->getSurrogate( typeid(T) );
704  if( ! surr )
705  return 0;
706 
707  return static_cast<const BasicSerializationSurrogate<T>*>(surr);
708 }
709 
710 
711 template <typename T>
712 inline bool SerializationInfo::compose(T& type) const
713 {
714  const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
715  if( ! surr )
716  return false;
717 
718  surr->compose(*this, type);
719  return true;
720 }
721 
722 
723 template <typename T>
724 inline bool SerializationInfo::decompose(const T& type)
725 {
726  const BasicSerializationSurrogate<T>* surr = this->getSurrogate<T>();
727  if( ! surr )
728  return false;
729 
730  surr->decompose(*this, type);
731  this->setTypeName( surr->typeName() );
732  return true;
733 }
734 
738 {
739  public:
740  Iterator()
741  : _si(0)
742  {}
743 
744  Iterator(const Iterator& other)
745  : _si(other._si)
746  {}
747 
748  explicit Iterator(SerializationInfo* si)
749  : _si(si)
750  {}
751 
752  Iterator& operator=(const Iterator& other)
753  {
754  _si = other._si;
755  return *this;
756  }
757 
758  Iterator& operator++()
759  {
760  _si = _si->sibling();
761  return *this;
762  }
763 
764  SerializationInfo& operator*() const
765  { return *_si; }
766 
767  SerializationInfo* operator->() const
768  { return _si; }
769 
770  bool operator!=(const Iterator& other) const
771  { return _si != other._si; }
772 
773  bool operator==(const Iterator& other) const
774  { return _si == other._si; }
775 
776  private:
777  SerializationInfo* _si;
778 };
779 
783 {
784  public:
785  ConstIterator()
786  : _si(0)
787  {}
788 
789  ConstIterator(const ConstIterator& other)
790  : _si(other._si)
791  {}
792 
793  explicit ConstIterator(const SerializationInfo* si)
794  : _si(si)
795  {}
796 
797  ConstIterator& operator=(const ConstIterator& other)
798  {
799  _si = other._si;
800  return *this;
801  }
802 
803  ConstIterator& operator++()
804  {
805  _si = _si->sibling();
806  return *this;
807  }
808 
809  const SerializationInfo& operator*() const
810  { return *_si; }
811 
812  const SerializationInfo* operator->() const
813  { return _si; }
814 
815  bool operator!=(const ConstIterator& other) const
816  { return _si != other._si; }
817 
818  bool operator==(const ConstIterator& other) const
819  { return _si == other._si; }
820 
821  private:
822  const SerializationInfo* _si;
823 };
824 
825 
827 {
829 }
830 
831 
833 {
835 }
836 
837 
842 class SaveInfo
843 {
844  public:
846  explicit SaveInfo(SerializationInfo& info)
847  : si(&info)
848  {}
849 
853  { return *si; }
854 
857  template <typename T>
858  bool save(const T& type)
859  {
860  bool first = si->beginSave( &type );
861  if(first)
862  {
863  *si <<= type;
864  si->finishSave();
865  }
866 
867  return first;
868  }
869 
870  SerializationInfo* si;
871 };
872 
874 struct Save
875 {};
876 
878 inline Save save()
879 {
880  return Save();
881 }
882 
884 inline SaveInfo operator <<(SerializationInfo& si, const Save&)
885 {
886  return SaveInfo(si);
887 }
888 
890 template <typename T>
891 inline void operator <<=(SaveInfo info, const T& type)
892 {
893  save( info, type );
894 }
895 
901 template <typename T>
902 inline void save(SaveInfo& si, const T& type)
903 {
904  if( ! si.save(type) )
905  {
906  si.out() <<= type;
907  }
908 }
909 
910 
915 class LoadInfo
916 {
917  public:
919  explicit LoadInfo(const SerializationInfo& info)
920  : si(&info)
921  {}
922 
925  const SerializationInfo& in() const
926  { return *si; }
927 
930  template <typename T>
931  void load(T& type) const
932  {
933  T* tp = &type;
934 
935  si->beginLoad( tp, typeid(T) );
936  *si >>= type;
937  si->finishLoad();
938  }
939 
940  private:
941  const SerializationInfo* si;
942 };
943 
945 struct Load
946 {};
947 
949 inline Load load()
950 {
951  return Load();
952 }
953 
955 inline LoadInfo operator >>(const SerializationInfo& si, const Load&)
956 {
957  return LoadInfo(si);
958 }
959 
961 template <typename T>
962 inline void operator >>=(const LoadInfo& li, T& type)
963 {
964  load(li, type);
965 }
966 
972 template <typename T>
973 inline void load(const LoadInfo& li, T& type)
974 {
975  li.load(type);
976 }
977 
978 
983 template <typename T>
984 inline void operator >>=(const SerializationInfo& si, T*& ptr)
985 {
986  si.loadPointer(ptr);
987 }
988 
993 template <typename T>
994 inline void operator <<=(SerializationInfo& si, const T* ptr)
995 {
996  si.setReference( ptr );
997 }
998 
1003 inline void operator >>=(const SerializationInfo& si, bool& n)
1004 {
1005  si.getBool(n);
1006 }
1007 
1012 inline void operator <<=(SerializationInfo& si, bool n)
1013 {
1014  si.setBool(n);
1015 }
1016 
1017 
1022 inline void operator >>=(const SerializationInfo& si, Pt::int8_t& n)
1023 {
1024  si.getInt8(n);
1025 }
1026 
1031 inline void operator <<=(SerializationInfo& si, Pt::int8_t n)
1032 {
1033  si.setInt8(n);
1034 }
1035 
1040 inline void operator >>=(const SerializationInfo& si, Pt::int16_t& n)
1041 {
1042  si.getInt16(n);
1043 }
1044 
1049 inline void operator <<=(SerializationInfo& si, Pt::int16_t n)
1050 {
1051  si.setInt16(n);
1052 }
1053 
1058 inline void operator >>=(const SerializationInfo& si, Pt::int32_t& n)
1059 {
1060  si.getInt32(n);
1061 }
1062 
1067 inline void operator <<=(SerializationInfo& si, Pt::int32_t n)
1068 {
1069  si.setInt32(n);
1070 }
1071 
1076 inline void operator >>=(const SerializationInfo& si, Pt::int64_t& n)
1077 {
1078  si.getInt64(n);
1079 }
1080 
1085 inline void operator <<=(SerializationInfo& si, Pt::int64_t n)
1086 {
1087  si.setInt64(n);
1088 }
1089 
1094 inline void operator >>=(const SerializationInfo& si, Pt::uint8_t& n)
1095 {
1096  si.getUInt8(n);
1097 }
1098 
1103 inline void operator <<=(SerializationInfo& si, Pt::uint8_t n)
1104 {
1105  si.setUInt8(n);
1106 }
1107 
1112 inline void operator >>=(const SerializationInfo& si, Pt::uint16_t& n)
1113 {
1114  si.getUInt16(n);
1115 }
1116 
1121 inline void operator <<=(SerializationInfo& si, Pt::uint16_t n)
1122 {
1123  si.setUInt16(n);
1124 }
1125 
1130 inline void operator >>=(const SerializationInfo& si, Pt::uint32_t& n)
1131 {
1132  si.getUInt32(n);
1133 }
1134 
1139 inline void operator <<=(SerializationInfo& si, Pt::uint32_t n)
1140 {
1141  si.setUInt32(n);
1142 }
1143 
1148 inline void operator >>=(const SerializationInfo& si, Pt::uint64_t& n)
1149 {
1150  si.getUInt64(n);
1151 }
1152 
1157 inline void operator <<=(SerializationInfo& si, Pt::uint64_t n)
1158 {
1159  si.setUInt64(n);
1160 }
1161 
1166 inline void operator >>=(const SerializationInfo& si, float& n)
1167 {
1168  si.getFloat(n);
1169 }
1170 
1175 inline void operator <<=(SerializationInfo& si, float n)
1176 {
1177  si.setFloat(n);
1178 }
1179 
1184 inline void operator >>=(const SerializationInfo& si, double& n)
1185 {
1186  si.getDouble(n);
1187 }
1188 
1193 inline void operator <<=(SerializationInfo& si, double n)
1194 {
1195  si.setDouble(n);
1196 }
1197 
1202 inline void operator >>=(const SerializationInfo& si, long double& n)
1203 {
1204  si.getLongDouble(n);
1205 }
1206 
1211 inline void operator <<=(SerializationInfo& si, long double n)
1212 {
1213  si.setLongDouble(n);
1214 }
1215 
1220 inline void operator >>=(const SerializationInfo& si, char& ch)
1221 {
1222  Pt::Char tmp;
1223  si.getChar(tmp);
1224  ch = static_cast<char>( tmp.value() );
1225 }
1226 
1231 inline void operator <<=(SerializationInfo& si, char ch)
1232 {
1233  si.setChar( Pt::Char(ch) );
1234 }
1235 
1240 inline void operator <<=(SerializationInfo& si, const char* str)
1241 {
1242  si.setString(str);
1243 }
1244 
1249 inline void operator >>=(const SerializationInfo& si, std::string& str)
1250 {
1251  si.getString(str);
1252 }
1253 
1258 inline void operator <<=(SerializationInfo& si, const std::string& str)
1259 {
1260  si.setString( str.c_str() );
1261 }
1262 
1267 inline void operator >>=(const SerializationInfo& si, Pt::String& str)
1268 {
1269  si.getString(str);
1270 }
1271 
1276 inline void operator <<=(SerializationInfo& si, const Pt::String& str)
1277 {
1278  si.setString(str);
1279 }
1280 
1285 template <typename T, typename A>
1286 inline void operator >>=(const SerializationInfo& si, std::vector<T, A>& vec)
1287 {
1288  T elem = T();
1289  vec.clear();
1290  vec.reserve( si.memberCount() );
1291 
1293  for(SerializationInfo::ConstIterator it = si.begin(); it != end; ++it)
1294  {
1295  vec.push_back(elem);
1296  *it >> Pt::load() >>= vec.back();
1297  }
1298 }
1299 
1304 template <typename T, typename A>
1305 inline void operator <<=(SerializationInfo& si, const std::vector<T, A>& vec)
1306 {
1307  typename std::vector<T, A>::const_iterator it;
1308 
1309  for(it = vec.begin(); it != vec.end(); ++it)
1310  {
1311  si.addElement() << Pt::save() <<= *it;
1312  }
1313 
1314  si.setTypeName( Pt::LiteralPtr<char>("std::vector") );
1315  si.setSequence();
1316 }
1317 
1322 template <typename T, typename A>
1323 inline void operator >>=(const SerializationInfo& si, std::list<T, A>& list)
1324 {
1325  list.clear();
1326  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1327  {
1328  list.resize( list.size() + 1 );
1329  *it >> Pt::load() >>= list.back();
1330  }
1331 }
1332 
1337 template <typename T, typename A>
1338 inline void operator <<=(SerializationInfo& si, const std::list<T, A>& list)
1339 {
1340  typename std::list<T, A>::const_iterator it;
1341 
1342  for(it = list.begin(); it != list.end(); ++it)
1343  {
1344  si.addElement() << Pt::save() <<= *it;
1345  }
1346 
1347  si.setTypeName( Pt::LiteralPtr<char>("std::list") );
1348  si.setSequence();
1349 }
1350 
1355 template <typename T, typename A>
1356 inline void operator >>=(const SerializationInfo& si, std::deque<T, A>& deque)
1357 {
1358  deque.clear();
1359  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1360  {
1361  // NOTE: push_back does not invalidate references to elements
1362  deque.push_back( T() );
1363  *it >> Pt::load() >>= deque.back();
1364  }
1365 }
1366 
1371 template <typename T, typename A>
1372 inline void operator <<=(SerializationInfo& si, const std::deque<T, A>& deque)
1373 {
1374  typename std::deque<T, A>::const_iterator it;
1375 
1376  for(it = deque.begin(); it != deque.end(); ++it)
1377  {
1378  si.addElement() << Pt::save() <<= *it;
1379  }
1380 
1381  si.setTypeName( Pt::LiteralPtr<char>("std::deque") );
1382  si.setSequence();
1383 }
1384 
1385 
1394 template <typename T, typename C, typename A>
1395 inline void operator >>=(const SerializationInfo& si, std::set<T, C, A>& set)
1396 {
1397  // typedef typename std::set<T, C, A>::iterator SetIterator;
1398  // std::pair<SetIterator, bool> pos;
1399 
1400  set.clear();
1401  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1402  {
1403  T t;
1404  *it >>= t;
1405  set.insert(t);
1406 
1407  // T t;
1408  // *it >>= Pt::load() >>= t;
1409  // pos = set.insert(t);
1410  // if( ! pos.second )
1411  // it->rebind(0);
1412 
1413  }
1414 }
1415 
1420 template <typename T, typename C, typename A>
1421 inline void operator <<=(SerializationInfo& si, const std::set<T, C, A>& set)
1422 {
1423  typename std::set<T, C, A>::const_iterator it;
1424 
1425  for(it = set.begin(); it != set.end(); ++it)
1426  {
1427  si.addElement() << Pt::save() <<= *it;
1428  }
1429 
1430  si.setTypeName( Pt::LiteralPtr<char>("std::set") );
1431  si.setSequence();
1432 }
1433 
1442 template <typename T, typename C, typename A>
1443 inline void operator >>=(const SerializationInfo& si, std::multiset<T, C, A>& multiset)
1444 {
1445  // typename std::multiset<T>::iterator pos;
1446 
1447  multiset.clear();
1448  for(Pt::SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1449  {
1450  T t;
1451  *it >>= t;
1452  multiset.insert(t);
1453 
1454  // T tmp;
1455  // *it >>= Pt::load() >>= tmp;
1456  // pos = multiset.insert(tmp);
1457 
1458  // T& t = const_cast<T&>(*pos);
1459  // it->rebind(&t);
1460  }
1461 }
1462 
1467 template <typename T, typename C, typename A>
1468 inline void operator <<=(SerializationInfo& si, const std::multiset<T, C, A>& multiset)
1469 {
1470  typename std::multiset<T, C, A>::const_iterator it;
1471 
1472  for(it = multiset.begin(); it != multiset.end(); ++it)
1473  {
1474  si.addElement() << Pt::save() <<= *it;
1475  }
1476 
1477  si.setTypeName( Pt::LiteralPtr<char>("std::multiset") );
1478  si.setSequence();
1479 }
1480 
1481 
1482 template <typename A, typename B>
1483 inline void operator >>=(const SerializationInfo& si, std::pair<A, B>& p)
1484 {
1485  si.getMember("first") >>= p.first;
1486  si.getMember("second") >>= p.second;
1487 }
1488 
1489 
1490 template <typename A, typename B>
1491 inline void operator <<=(SerializationInfo& si, const std::pair<A, B>& p)
1492 {
1493  si.setTypeName( Pt::LiteralPtr<char>("std::pair") );
1494  si.addMember( Pt::LiteralPtr<char>("first") ) <<= p.first;
1495  si.addMember( Pt::LiteralPtr<char>("second") ) <<= p.second;
1496 }
1497 
1502 template <typename K, typename V, typename P, typename A>
1503 inline void operator >>=(const SerializationInfo& si, std::map<K, V, P, A>& map)
1504 {
1505  typedef typename std::map<K, V, P, A>::iterator MapIterator;
1506  std::pair<MapIterator, bool> pos;
1507 
1508  map.clear();
1509  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1510  {
1511  K k;
1512 
1514  if( kv != it->end() )
1515  *kv >>= k;
1516 
1517  std::pair<K, V> elem( k, V() );
1518  pos = map.insert(elem);
1519 
1520  if( pos.second && ++kv != it->end() )
1521  *kv >> Pt::load() >>= pos.first->second;
1522  }
1523 }
1524 
1529 template <typename K, typename V, typename P, typename A>
1530 inline void operator <<=(SerializationInfo& si, const std::map<K, V, P, A>& map)
1531 {
1532  typename std::map<K, V, P, A>::const_iterator it;
1533 
1534  for(it = map.begin(); it != map.end(); ++it)
1535  {
1536  SerializationInfo& elem = si.addDictElement();
1537  elem.addDictKey() <<= it->first;
1538  elem.addDictValue() << Pt::save() <<= it->second;
1539  }
1540 
1541  si.setTypeName( Pt::LiteralPtr<char>("std::map") );
1542  si.setDict();
1543 }
1544 
1549 template <typename K, typename V, typename P, typename A>
1550 inline void operator >>=(const SerializationInfo& si, std::multimap<K, V, P, A>& multimap)
1551 {
1552  typename std::multimap<K, V, P, A>::iterator mit;
1553 
1554  multimap.clear();
1555  for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1556  {
1557  K k;
1558 
1560  if( kv != it->end() )
1561  *kv >>= k;
1562 
1563  std::pair<K, V> elem( k, V() );
1564  mit = multimap.insert(elem);
1565 
1566  if( ++kv != it->end() )
1567  *kv >> Pt::load() >>= mit->second;
1568  }
1569 }
1570 
1575 template <typename T, typename C, typename P, typename A>
1576 inline void operator <<=(SerializationInfo& si, const std::multimap<T, C, P, A>& multimap)
1577 {
1578  typename std::multimap<T, C, P, A>::const_iterator it;
1579 
1580  for(it = multimap.begin(); it != multimap.end(); ++it)
1581  {
1582  SerializationInfo& elem = si.addDictElement();
1583  elem.addDictKey() <<= it->first;
1584  elem.addDictValue() << Pt::save() <<= it->second;
1585  }
1586 
1587  si.setTypeName( Pt::LiteralPtr<char>("std::multimap") );
1588  si.setDict();
1589 }
1590 
1591 } // namespace Pt
1592 
1593 #endif // Pt_SerializationInfo_h
bool isStruct() const
Returns true if struct type.
Definition: SerializationInfo.h:153
Core module.
Definition: pt-gfx-images.dox:14
void setChar(const Pt::Char &c)
Set to character value.
void setStruct()
Set to struct type.
void setString(const Pt::String &s)
Set to string value.
Definition: SerializationInfo.h:304
Void type.
Definition: Void.h:44
void setUInt8(Pt::uint8_t n)
Set to 8-bit unsigned integer value.
void setString(const char *s, std::size_t len, const TextCodec< Pt::Char, char > &codec)
Set to string value.
bool isSequence() const
Returns true if sequence type.
Definition: SerializationInfo.h:163
Iterator end()
Returns an iterator to the end of child elements.
Definition: SerializationInfo.h:826
void beginLoad(void *p, const std::type_info &ti) const
Begin loading.
void load(T &type) const
Loads the type.
Definition: SerializationInfo.h:931
bool save(const T &type)
Returns true if type was saved, false if type was already saved.
Definition: SerializationInfo.h:858
Saves referencable types.
Definition: SerializationInfo.h:843
void load(const LoadInfo &li, T &type)
Loads referencable types.
Definition: SerializationInfo.h:973
void endFormat(Formatter &formatter)
End formatting.
void finishSave()
Finish saving.
SerializationContext * context() const
Returns the used context.
Definition: SerializationInfo.h:185
void setUInt16(Pt::uint16_t n)
Set to 16-bit unsigned integer value.
SerializationInfo & out() const
Returns the SerializationInfo to save to.
Definition: SerializationInfo.h:852
void setTypeName(const std::string &type)
Sets the type name.
void getUInt64(Pt::uint64_t &n) const
Get value as a 64-bit unsigned integer.
Loads referencable types.
Definition: SerializationInfo.h:916
size_type length() const
Returns the length of the string.
Definition: String.h:234
SerializationInfo & addDictValue()
Add a dict value.
void getInt8(Pt::int8_t &n) const
Get value as a 8-bit integer.
bool beginSave(const void *p)
Begin saving.
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition: String.h:264
void setReference(const void *ref)
Set to reference for which to create an ID.
void save(SaveInfo &si, const T &type)
Saves referencable types.
Definition: SerializationInfo.h:902
uint32_t value() const
Returns the unicode value.
Definition: String.h:94
void getUInt16(Pt::uint16_t &n) const
Get value as a 16-bit unsigned integer.
Iterator beginFormat(Formatter &formatter)
Begin formatting.
void setId(const char *id, std::size_t len)
Sets the reference ID.
void setInt64(Pt::int64_t l)
Set to 64-bit integer value.
Type
Type identifier.
Definition: SerializationInfo.h:65
void setString(const std::string &str, const TextCodec< Pt::Char, char > &codec)
Set to string value.
Definition: SerializationInfo.h:299
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition: SerializationInfo.h:449
SerializationInfo()
Default constructor.
Definition: SerializationInfo.h:93
Type type() const
Returns the type identifier.
Definition: SerializationInfo.h:134
const char * getBinary(std::size_t &length) const
Get value as a binary object.
void getChar(Pt::Char &c) const
Get value as a character.
SerializationInfo * parent()
Returns the parent node.
Definition: SerializationInfo.h:206
void setLongDouble(long double d)
Set to long double value.
void setInt32(Pt::int32_t n)
Set to 32-bit integer value.
const SerializationInfo & getMember(const char *name) const
Get a struct member.
const char * name() const
Returns the instance name.
Definition: SerializationInfo.h:237
void loadReference(T &fixme, unsigned mid=0) const
Load a reference during deserialization.
Definition: SerializationInfo.h:579
void setName(const char *name)
Sets the instance name.
void setReference(const char *id, std::size_t idlen)
Set to reference with ID to fixup.
SerializationInfo(SerializationContext *context)
Construct with context.
Definition: SerializationInfo.h:109
bool isVoid() const
Returns true if not set to a type.
Definition: SerializationInfo.h:139
void getString(std::string &s, const TextCodec< Pt::Char, char > &codec) const
Get value as a string.
bool isDict() const
Returns true if dictionary type.
Definition: SerializationInfo.h:158
SerializationInfo & addElement()
Add a sequence element.
void setTypeName(const char *type, std::size_t len)
Sets the type name.
void setDouble(double f)
Set to double value.
void getDouble(double &f) const
Get value as a double.
void setName(const LiteralPtr< char > &type)
Sets the instance name.
const SerializationInfo * findMember(const char *name) const
Find a struct member.
void setName(const std::string &name)
Sets the instance name.
void setBool(bool b)
Set to bool value.
Context for the serialization of types.
Definition: SerializationContext.h:21
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
const SerializationInfo * parent() const
Returns the parent node.
Definition: SerializationInfo.h:211
void clear()
Clears all content.
void getUInt8(Pt::uint8_t &n) const
Get value as a 8-bit unsigned integer.
Unicode character type.
Definition: String.h:67
void finishLoad() const
Finish loading.
void setBinary(const char *data, std::size_t length)
Set to binary value.
void setChar(char c)
Set to character value.
ConstIterator begin() const
Returns an iterator to the begin of child elements.
void getLongDouble(long double &d) const
Get value as a long double.
const SerializationInfo & getMember(const std::string &name) const
Get a struct member.
Definition: SerializationInfo.h:496
SerializationInfo & addMember(const char *name, std::size_t len)
Add a struct member.
SerializationInfo & addDictElement()
Add a dict element.
void loadPointer(T *&fixme, unsigned mid=0) const
Load a reference during deserialization.
Definition: SerializationInfo.h:587
Const forward iterator for child elements.
Definition: SerializationInfo.h:783
void setUInt64(Pt::uint64_t n)
Set to 64-bit unsigned integer value.
void setName(const char *type, std::size_t len)
Sets the instance name.
SerializationInfo & addMember(const LiteralPtr< char > &name)
Add a struct member.
void setString(const char *s)
Set to string value.
void getInt64(Pt::int64_t &l) const
Get value as a 64-bit integer.
const SerializationInfo * findMember(const std::string &name) const
Find a struct member.
Definition: SerializationInfo.h:508
const SerializationInfo & in() const
Returns the SerializationInfo to load from.
Definition: SerializationInfo.h:925
bool isReference() const
Returns true if reference.
Definition: SerializationInfo.h:168
void removeMember(const char *name)
Remove a struct member.
const char * typeName() const
Returns the type name.
Definition: SerializationInfo.h:216
void getString(std::string &s) const
Get value as a string.
void setFloat(float f)
Set to float value.
Forward Iterator for child elements.
Definition: SerializationInfo.h:738
void setUInt32(Pt::uint32_t n)
Set to 32-bit unsigned integer value.
void rebind(void *obj) const
Rebind to new address.
void setInt8(Pt::int8_t n)
Set to 8-bit integer value.
void setDict()
Set to dictionary type.
void setVoid()
Set to void type.
Represents a pointer to a literal.
Definition: LiteralPtr.h:40
void getInt16(Pt::int16_t &n) const
Get value as a 16-bit integer.
void setTypeName(const LiteralPtr< char > &type)
Sets the type name.
void setId(const std::string &id)
Sets the reference ID.
void setId(const char *id)
Sets the reference ID.
void setString(const Pt::Char *s, std::size_t len)
Set to string value.
bool decompose(const T &type)
Returns true if the type could be decomposed with a surrogate.
Definition: SerializationInfo.h:724
bool compose(T &type) const
Returns true if the type could be composed with a surrogate.
Definition: SerializationInfo.h:712
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
std::size_t memberCount() const
Returns the number of members.
void setTypeName(const char *type)
Sets the type name.
void format(Formatter &formatter) const
Format complete value or all members.
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
void getString(Pt::String &s) const
Get value as a string.
void getInt32(Pt::int32_t &i) const
Get value as a 32-bit integer.
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36
void removeMember(const std::string &name)
Remove a struct member.
Definition: SerializationInfo.h:467
Support for serialization to different formats.
Definition: Formatter.h:46
void removeMember(const SerializationInfo &si)
Remove a struct member.
Unicode capable basic_string.
Definition: String.h:43
SerializationInfo & addMember(const char *name)
Add a struct member.
Definition: SerializationInfo.h:454
SerializationInfo * findMember(const char *name)
Find a struct member.
void getBool(bool &b) const
Get value as a bool.
int_type int8_t
Signed 8-bit integer type.
Definition: Types.h:12
void setString(const std::string &s)
Set to string value.
const char * id() const
Returns the reference ID.
Definition: SerializationInfo.h:258
void getUInt32(Pt::uint32_t &n) const
Get value as a 32-bit unsigned integer.
void setReference(const std::string &id)
Set to reference with ID to fixup.
Definition: SerializationInfo.h:569
void setSequence()
Set to sequence type.
void setInt16(Pt::int16_t n)
Set to 16-bit integer value.
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:59
int_type int16_t
Signed 16-bit integer type.
Definition: Types.h:24
Iterator begin()
Returns an iterator to the begin of child elements.
void rebindFixup(void *obj) const
Rebind to new fixup address.
void getChar(char &c) const
Get value as a character.
bool isScalar() const
Returns true if scalar type.
Definition: SerializationInfo.h:148
SerializationInfo & addDictKey()
Add a dict key.
SerializationInfo * findMember(const std::string &name)
Find a struct member.
Definition: SerializationInfo.h:523
void getFloat(float &f) const
Get value as a float.
~SerializationInfo()
Destructor.