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
49namespace Pt {
50
52class Formatter;
53
59{
60 public:
61 class Iterator;
62 class ConstIterator;
63
65 enum Type {
66 Void = 0,
67 Context = 1,
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,
83 Binary = 17,
84 Struct = 18,
85 Sequence = 19,
86 Dict = 20,
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
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
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
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
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
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
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
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
700template <typename T>
701inline 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
711template <typename T>
712inline 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
723template <typename T>
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:
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
830
831
836
837
842class 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
871};
872
874struct Save
875{};
876
878inline Save save()
879{
880 return Save();
881}
882
884inline SaveInfo operator <<(SerializationInfo& si, const Save&)
885{
886 return SaveInfo(si);
887}
888
890template <typename T>
891inline void operator <<=(SaveInfo info, const T& type)
892{
893 save( info, type );
894}
895
901template <typename T>
902inline void save(SaveInfo& si, const T& type)
903{
904 if( ! si.save(type) )
905 {
906 si.out() <<= type;
907 }
908}
909
910
915class 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
945struct Load
946{};
947
949inline Load load()
950{
951 return Load();
952}
953
955inline LoadInfo operator >>(const SerializationInfo& si, const Load&)
956{
957 return LoadInfo(si);
958}
959
961template <typename T>
962inline void operator >>=(const LoadInfo& li, T& type)
963{
964 load(li, type);
965}
966
972template <typename T>
973inline void load(const LoadInfo& li, T& type)
974{
975 li.load(type);
976}
977
978
993template <typename T>
994inline void operator >>=(const SerializationInfo& si, T*& ptr)
995{
996 si.loadPointer(ptr);
997}
998
1008template <typename T>
1009inline void operator <<=(SerializationInfo& si, const T* ptr)
1010{
1011 si.setReference( ptr );
1012}
1013
1018inline void operator >>=(const SerializationInfo& si, bool& n)
1019{
1020 si.getBool(n);
1021}
1022
1027inline void operator <<=(SerializationInfo& si, bool n)
1028{
1029 si.setBool(n);
1030}
1031
1032
1037inline void operator >>=(const SerializationInfo& si, Pt::int8_t& n)
1038{
1039 si.getInt8(n);
1040}
1041
1046inline void operator <<=(SerializationInfo& si, Pt::int8_t n)
1047{
1048 si.setInt8(n);
1049}
1050
1055inline void operator >>=(const SerializationInfo& si, Pt::int16_t& n)
1056{
1057 si.getInt16(n);
1058}
1059
1064inline void operator <<=(SerializationInfo& si, Pt::int16_t n)
1065{
1066 si.setInt16(n);
1067}
1068
1073inline void operator >>=(const SerializationInfo& si, Pt::int32_t& n)
1074{
1075 si.getInt32(n);
1076}
1077
1082inline void operator <<=(SerializationInfo& si, Pt::int32_t n)
1083{
1084 si.setInt32(n);
1085}
1086
1091inline void operator >>=(const SerializationInfo& si, Pt::int64_t& n)
1092{
1093 si.getInt64(n);
1094}
1095
1100inline void operator <<=(SerializationInfo& si, Pt::int64_t n)
1101{
1102 si.setInt64(n);
1103}
1104
1109inline void operator >>=(const SerializationInfo& si, Pt::uint8_t& n)
1110{
1111 si.getUInt8(n);
1112}
1113
1118inline void operator <<=(SerializationInfo& si, Pt::uint8_t n)
1119{
1120 si.setUInt8(n);
1121}
1122
1127inline void operator >>=(const SerializationInfo& si, Pt::uint16_t& n)
1128{
1129 si.getUInt16(n);
1130}
1131
1136inline void operator <<=(SerializationInfo& si, Pt::uint16_t n)
1137{
1138 si.setUInt16(n);
1139}
1140
1145inline void operator >>=(const SerializationInfo& si, Pt::uint32_t& n)
1146{
1147 si.getUInt32(n);
1148}
1149
1154inline void operator <<=(SerializationInfo& si, Pt::uint32_t n)
1155{
1156 si.setUInt32(n);
1157}
1158
1163inline void operator >>=(const SerializationInfo& si, Pt::uint64_t& n)
1164{
1165 si.getUInt64(n);
1166}
1167
1172inline void operator <<=(SerializationInfo& si, Pt::uint64_t n)
1173{
1174 si.setUInt64(n);
1175}
1176
1181inline void operator >>=(const SerializationInfo& si, float& n)
1182{
1183 si.getFloat(n);
1184}
1185
1190inline void operator <<=(SerializationInfo& si, float n)
1191{
1192 si.setFloat(n);
1193}
1194
1199inline void operator >>=(const SerializationInfo& si, double& n)
1200{
1201 si.getDouble(n);
1202}
1203
1208inline void operator <<=(SerializationInfo& si, double n)
1209{
1210 si.setDouble(n);
1211}
1212
1217inline void operator >>=(const SerializationInfo& si, long double& n)
1218{
1219 si.getLongDouble(n);
1220}
1221
1226inline void operator <<=(SerializationInfo& si, long double n)
1227{
1228 si.setLongDouble(n);
1229}
1230
1235inline void operator >>=(const SerializationInfo& si, char& ch)
1236{
1237 Pt::Char tmp;
1238 si.getChar(tmp);
1239 ch = static_cast<char>( tmp.value() );
1240}
1241
1246inline void operator <<=(SerializationInfo& si, char ch)
1247{
1248 si.setChar( Pt::Char(ch) );
1249}
1250
1255inline void operator <<=(SerializationInfo& si, const char* str)
1256{
1257 si.setString(str);
1258}
1259
1264inline void operator >>=(const SerializationInfo& si, std::string& str)
1265{
1266 si.getString(str);
1267}
1268
1273inline void operator <<=(SerializationInfo& si, const std::string& str)
1274{
1275 si.setString( str.c_str() );
1276}
1277
1282inline void operator >>=(const SerializationInfo& si, Pt::String& str)
1283{
1284 si.getString(str);
1285}
1286
1291inline void operator <<=(SerializationInfo& si, const Pt::String& str)
1292{
1293 si.setString(str);
1294}
1295
1300template <typename T, typename A>
1301inline void operator >>=(const SerializationInfo& si, std::vector<T, A>& vec)
1302{
1303 T elem = T();
1304 vec.clear();
1305 vec.reserve( si.memberCount() );
1306
1308 for(SerializationInfo::ConstIterator it = si.begin(); it != end; ++it)
1309 {
1310 vec.push_back(elem);
1311 *it >> Pt::load() >>= vec.back();
1312 }
1313}
1314
1338template <typename T, typename A>
1339inline void operator <<=(SerializationInfo& si, const std::vector<T, A>& vec)
1340{
1341 typename std::vector<T, A>::const_iterator it;
1342
1343 for(it = vec.begin(); it != vec.end(); ++it)
1344 {
1345 si.addElement() << Pt::save() <<= *it;
1346 }
1347
1348 si.setTypeName( Pt::LiteralPtr<char>("std::vector") );
1349 si.setSequence();
1350}
1351
1356template <typename T, typename A>
1357inline void operator >>=(const SerializationInfo& si, std::list<T, A>& list)
1358{
1359 list.clear();
1360 for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1361 {
1362 list.resize( list.size() + 1 );
1363 *it >> Pt::load() >>= list.back();
1364 }
1365}
1366
1371template <typename T, typename A>
1372inline void operator <<=(SerializationInfo& si, const std::list<T, A>& list)
1373{
1374 typename std::list<T, A>::const_iterator it;
1375
1376 for(it = list.begin(); it != list.end(); ++it)
1377 {
1378 si.addElement() << Pt::save() <<= *it;
1379 }
1380
1381 si.setTypeName( Pt::LiteralPtr<char>("std::list") );
1382 si.setSequence();
1383}
1384
1389template <typename T, typename A>
1390inline void operator >>=(const SerializationInfo& si, std::deque<T, A>& deque)
1391{
1392 deque.clear();
1393 for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1394 {
1395 // NOTE: push_back does not invalidate references to elements
1396 deque.push_back( T() );
1397 *it >> Pt::load() >>= deque.back();
1398 }
1399}
1400
1405template <typename T, typename A>
1406inline void operator <<=(SerializationInfo& si, const std::deque<T, A>& deque)
1407{
1408 typename std::deque<T, A>::const_iterator it;
1409
1410 for(it = deque.begin(); it != deque.end(); ++it)
1411 {
1412 si.addElement() << Pt::save() <<= *it;
1413 }
1414
1415 si.setTypeName( Pt::LiteralPtr<char>("std::deque") );
1416 si.setSequence();
1417}
1418
1419
1428template <typename T, typename C, typename A>
1429inline void operator >>=(const SerializationInfo& si, std::set<T, C, A>& set)
1430{
1431 // typedef typename std::set<T, C, A>::iterator SetIterator;
1432 // std::pair<SetIterator, bool> pos;
1433
1434 set.clear();
1435 for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1436 {
1437 T t;
1438 *it >>= t;
1439 set.insert(t);
1440
1441 // T t;
1442 // *it >>= Pt::load() >>= t;
1443 // pos = set.insert(t);
1444 // if( ! pos.second )
1445 // it->rebind(0);
1446
1447 }
1448}
1449
1454template <typename T, typename C, typename A>
1455inline void operator <<=(SerializationInfo& si, const std::set<T, C, A>& set)
1456{
1457 typename std::set<T, C, A>::const_iterator it;
1458
1459 for(it = set.begin(); it != set.end(); ++it)
1460 {
1461 si.addElement() << Pt::save() <<= *it;
1462 }
1463
1464 si.setTypeName( Pt::LiteralPtr<char>("std::set") );
1465 si.setSequence();
1466}
1467
1476template <typename T, typename C, typename A>
1477inline void operator >>=(const SerializationInfo& si, std::multiset<T, C, A>& multiset)
1478{
1479 // typename std::multiset<T>::iterator pos;
1480
1481 multiset.clear();
1482 for(Pt::SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1483 {
1484 T t;
1485 *it >>= t;
1486 multiset.insert(t);
1487
1488 // T tmp;
1489 // *it >>= Pt::load() >>= tmp;
1490 // pos = multiset.insert(tmp);
1491
1492 // T& t = const_cast<T&>(*pos);
1493 // it->rebind(&t);
1494 }
1495}
1496
1501template <typename T, typename C, typename A>
1502inline void operator <<=(SerializationInfo& si, const std::multiset<T, C, A>& multiset)
1503{
1504 typename std::multiset<T, C, A>::const_iterator it;
1505
1506 for(it = multiset.begin(); it != multiset.end(); ++it)
1507 {
1508 si.addElement() << Pt::save() <<= *it;
1509 }
1510
1511 si.setTypeName( Pt::LiteralPtr<char>("std::multiset") );
1512 si.setSequence();
1513}
1514
1515
1516template <typename A, typename B>
1517inline void operator >>=(const SerializationInfo& si, std::pair<A, B>& p)
1518{
1519 si.getMember("first") >>= p.first;
1520 si.getMember("second") >>= p.second;
1521}
1522
1523
1524template <typename A, typename B>
1525inline void operator <<=(SerializationInfo& si, const std::pair<A, B>& p)
1526{
1527 si.setTypeName( Pt::LiteralPtr<char>("std::pair") );
1528 si.addMember( Pt::LiteralPtr<char>("first") ) <<= p.first;
1529 si.addMember( Pt::LiteralPtr<char>("second") ) <<= p.second;
1530}
1531
1536template <typename K, typename V, typename P, typename A>
1537inline void operator >>=(const SerializationInfo& si, std::map<K, V, P, A>& map)
1538{
1539 typedef typename std::map<K, V, P, A>::iterator MapIterator;
1540 std::pair<MapIterator, bool> pos;
1541
1542 map.clear();
1543 for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1544 {
1545 K k;
1546
1548 if( kv != it->end() )
1549 *kv >>= k;
1550
1551 std::pair<K, V> elem( k, V() );
1552 pos = map.insert(elem);
1553
1554 if( pos.second && ++kv != it->end() )
1555 *kv >> Pt::load() >>= pos.first->second;
1556 }
1557}
1558
1563template <typename K, typename V, typename P, typename A>
1564inline void operator <<=(SerializationInfo& si, const std::map<K, V, P, A>& map)
1565{
1566 typename std::map<K, V, P, A>::const_iterator it;
1567
1568 for(it = map.begin(); it != map.end(); ++it)
1569 {
1570 SerializationInfo& elem = si.addDictElement();
1571 elem.addDictKey() <<= it->first;
1572 elem.addDictValue() << Pt::save() <<= it->second;
1573 }
1574
1575 si.setTypeName( Pt::LiteralPtr<char>("std::map") );
1576 si.setDict();
1577}
1578
1583template <typename K, typename V, typename P, typename A>
1584inline void operator >>=(const SerializationInfo& si, std::multimap<K, V, P, A>& multimap)
1585{
1586 typename std::multimap<K, V, P, A>::iterator mit;
1587
1588 multimap.clear();
1589 for(SerializationInfo::ConstIterator it = si.begin(); it != si.end(); ++it)
1590 {
1591 K k;
1592
1594 if( kv != it->end() )
1595 *kv >>= k;
1596
1597 std::pair<K, V> elem( k, V() );
1598 mit = multimap.insert(elem);
1599
1600 if( ++kv != it->end() )
1601 *kv >> Pt::load() >>= mit->second;
1602 }
1603}
1604
1609template <typename T, typename C, typename P, typename A>
1610inline void operator <<=(SerializationInfo& si, const std::multimap<T, C, P, A>& multimap)
1611{
1612 typename std::multimap<T, C, P, A>::const_iterator it;
1613
1614 for(it = multimap.begin(); it != multimap.end(); ++it)
1615 {
1616 SerializationInfo& elem = si.addDictElement();
1617 elem.addDictKey() <<= it->first;
1618 elem.addDictValue() << Pt::save() <<= it->second;
1619 }
1620
1621 si.setTypeName( Pt::LiteralPtr<char>("std::multimap") );
1622 si.setDict();
1623}
1624
1625} // namespace Pt
1626
1627#endif // Pt_SerializationInfo_h
Support for serialization to different formats.
Definition Formatter.h:46
Represents a pointer to a literal.
Definition LiteralPtr.h:40
Loads referencable types.
Definition SerializationInfo.h:916
const SerializationInfo & in() const
Returns the SerializationInfo to load from.
Definition SerializationInfo.h:925
void load(T &type) const
Loads the type.
Definition SerializationInfo.h:931
Saves referencable types.
Definition SerializationInfo.h:843
SerializationInfo & out() const
Returns the SerializationInfo to save to.
Definition SerializationInfo.h:852
bool save(const T &type)
Returns true if type was saved, false if type was already saved.
Definition SerializationInfo.h:858
Context for the serialization of types.
Definition Api-SerializationContext.h:26
Const forward iterator for child elements.
Definition SerializationInfo.h:783
Forward Iterator for child elements.
Definition SerializationInfo.h:738
Represents arbitrary types during serialization.
Definition SerializationInfo.h:59
SerializationInfo & addMember(const char *name)
Add a struct member.
Definition SerializationInfo.h:454
SerializationInfo & addElement()
Add a sequence element.
void setChar(char c)
Set to character value.
void getUInt64(Pt::uint64_t &n) const
Get value as a 64-bit unsigned integer.
void setString(const char *s, std::size_t len, const TextCodec< Pt::Char, char > &codec)
Set to string value.
void endFormat(Formatter &formatter)
End formatting.
void setReference(const char *id, std::size_t idlen)
Set to reference with ID to fixup.
void setLongDouble(long double d)
Set to long double value.
void setInt8(Pt::int8_t n)
Set to 8-bit integer value.
const SerializationInfo * findMember(const std::string &name) const
Find a struct member.
Definition SerializationInfo.h:508
bool isDict() const
Returns true if dictionary type.
Definition SerializationInfo.h:158
Type
Type identifier.
Definition SerializationInfo.h:65
@ UInt64
64-bit unsigned integer
Definition SerializationInfo.h:79
@ UInt8
8-bit unsigned integer
Definition SerializationInfo.h:76
@ Int16
16-bit integer
Definition SerializationInfo.h:73
@ Sequence
Sequence compound type.
Definition SerializationInfo.h:85
@ Context
Contextual.
Definition SerializationInfo.h:67
@ Boolean
Boolean.
Definition SerializationInfo.h:69
@ Str
Str.
Definition SerializationInfo.h:71
@ UInt32
32-bit unsigned integer
Definition SerializationInfo.h:78
@ Dict
Dictionary compound type.
Definition SerializationInfo.h:86
@ Struct
Structural compound type.
Definition SerializationInfo.h:84
@ Int32
32-bit integer
Definition SerializationInfo.h:74
@ Void
Void.
Definition SerializationInfo.h:66
@ Int8
8-bit integer
Definition SerializationInfo.h:72
@ UInt16
16-bit unsigned integer
Definition SerializationInfo.h:77
@ DictElement
Dictionary element.
Definition SerializationInfo.h:87
@ Reference
Reference.
Definition SerializationInfo.h:68
@ LongDouble
Long double floating point.
Definition SerializationInfo.h:82
@ Float
Floating point.
Definition SerializationInfo.h:80
@ Int64
64-bit integer
Definition SerializationInfo.h:75
@ Char
Char.
Definition SerializationInfo.h:70
@ Binary
Binary.
Definition SerializationInfo.h:83
@ Double
Double floating point.
Definition SerializationInfo.h:81
const char * getBinary(std::size_t &length) const
Get value as a binary object.
void getUInt32(Pt::uint32_t &n) const
Get value as a 32-bit unsigned integer.
Iterator begin()
Returns an iterator to the begin of child elements.
void getInt64(Pt::int64_t &l) const
Get value as a 64-bit integer.
void setStruct()
Set to struct type.
void setId(const std::string &id)
Sets the reference ID.
void getString(std::string &s) const
Get value as a string.
void setReference(const void *ref)
Set to reference for which to create an ID.
void setId(const char *id, std::size_t len)
Sets the reference ID.
void format(Formatter &formatter) const
Format complete value or all members.
void setTypeName(const std::string &type)
Sets the type name.
SerializationInfo & addMember(const LiteralPtr< char > &name)
Add a struct member.
void setFloat(float f)
Set to float value.
void setDict()
Set to dictionary type.
void setInt16(Pt::int16_t n)
Set to 16-bit integer value.
void setString(const Pt::String &s)
Set to string value.
Definition SerializationInfo.h:304
void setDouble(double f)
Set to double value.
SerializationContext * context() const
Returns the used context.
Definition SerializationInfo.h:185
const char * typeName() const
Returns the type name.
Definition SerializationInfo.h:216
void getChar(char &c) const
Get value as a character.
void setBinary(const char *data, std::size_t length)
Set to binary value.
void getInt8(Pt::int8_t &n) const
Get value as a 8-bit integer.
bool isReference() const
Returns true if reference.
Definition SerializationInfo.h:168
SerializationInfo & addMember(const char *name, std::size_t len)
Add a struct member.
void getInt32(Pt::int32_t &i) const
Get value as a 32-bit integer.
SerializationInfo & addDictValue()
Add a dict value.
SerializationInfo(SerializationContext *context)
Construct with context.
Definition SerializationInfo.h:109
void removeMember(const SerializationInfo &si)
Remove a struct member.
SerializationInfo * findMember(const char *name)
Find a struct member.
void loadReference(T &fixme, unsigned mid=0) const
Load a reference during deserialization.
Definition SerializationInfo.h:579
void setInt64(Pt::int64_t l)
Set to 64-bit integer value.
void rebindFixup(void *obj) const
Rebind to new fixup address.
void getInt16(Pt::int16_t &n) const
Get value as a 16-bit integer.
const SerializationInfo * parent() const
Returns the parent node.
Definition SerializationInfo.h:211
Iterator end()
Returns an iterator to the end of child elements.
Definition SerializationInfo.h:826
bool isStruct() const
Returns true if struct type.
Definition SerializationInfo.h:153
void setString(const std::string &str, const TextCodec< Pt::Char, char > &codec)
Set to string value.
Definition SerializationInfo.h:299
void setBool(bool b)
Set to bool value.
void getDouble(double &f) const
Get value as a double.
void setString(const char *s)
Set to string value.
void getBool(bool &b) const
Get value as a bool.
const char * name() const
Returns the instance name.
Definition SerializationInfo.h:237
void setInt32(Pt::int32_t n)
Set to 32-bit integer value.
void beginLoad(void *p, const std::type_info &ti) const
Begin loading.
void finishSave()
Finish saving.
void setTypeName(const char *type)
Sets the type name.
SerializationInfo * parent()
Returns the parent node.
Definition SerializationInfo.h:206
void removeMember(const std::string &name)
Remove a struct member.
Definition SerializationInfo.h:467
void setName(const char *type, std::size_t len)
Sets the instance name.
bool isScalar() const
Returns true if scalar type.
Definition SerializationInfo.h:148
void getChar(Pt::Char &c) const
Get value as a character.
void setTypeName(const LiteralPtr< char > &type)
Sets the type name.
const char * id() const
Returns the reference ID.
Definition SerializationInfo.h:258
SerializationInfo & addDictKey()
Add a dict key.
std::size_t memberCount() const
Returns the number of members.
void setName(const std::string &name)
Sets the instance name.
void getString(std::string &s, const TextCodec< Pt::Char, char > &codec) const
Get value as a string.
void finishLoad() const
Finish loading.
void setChar(const Pt::Char &c)
Set to character value.
void setUInt64(Pt::uint64_t n)
Set to 64-bit unsigned integer value.
void getLongDouble(long double &d) const
Get value as a long double.
void setUInt32(Pt::uint32_t n)
Set to 32-bit unsigned integer value.
~SerializationInfo()
Destructor.
void setName(const LiteralPtr< char > &type)
Sets the instance name.
void setName(const char *name)
Sets the instance name.
SerializationInfo & addDictElement()
Add a dict element.
void getFloat(float &f) const
Get value as a float.
void setUInt16(Pt::uint16_t n)
Set to 16-bit unsigned integer value.
void setString(const std::string &s)
Set to string value.
void getUInt8(Pt::uint8_t &n) const
Get value as a 8-bit unsigned integer.
SerializationInfo()
Default constructor.
Definition SerializationInfo.h:93
const SerializationInfo & getMember(const char *name) const
Get a struct member.
void clear()
Clears all content.
bool compose(T &type) const
Returns true if the type could be composed with a surrogate.
Definition SerializationInfo.h:712
SerializationInfo * findMember(const std::string &name)
Find a struct member.
Definition SerializationInfo.h:523
void setString(const Pt::Char *s, std::size_t len)
Set to string value.
ConstIterator begin() const
Returns an iterator to the begin of child elements.
Iterator beginFormat(Formatter &formatter)
Begin formatting.
void setTypeName(const char *type, std::size_t len)
Sets the type name.
bool beginSave(const void *p)
Begin saving.
void setReference(const std::string &id)
Set to reference with ID to fixup.
Definition SerializationInfo.h:569
Type type() const
Returns the type identifier.
Definition SerializationInfo.h:134
bool isSequence() const
Returns true if sequence type.
Definition SerializationInfo.h:163
bool isVoid() const
Returns true if not set to a type.
Definition SerializationInfo.h:139
void getString(Pt::String &s) const
Get value as a string.
void setVoid()
Set to void type.
void setId(const char *id)
Sets the reference ID.
void setSequence()
Set to sequence type.
void setUInt8(Pt::uint8_t n)
Set to 8-bit unsigned integer value.
void loadPointer(T *&fixme, unsigned mid=0) const
Load a reference during deserialization.
Definition SerializationInfo.h:587
void getUInt16(Pt::uint16_t &n) const
Get value as a 16-bit unsigned integer.
void removeMember(const char *name)
Remove a struct member.
SerializationInfo & addMember(const std::string &name)
Add a struct member.
Definition SerializationInfo.h:449
bool decompose(const T &type)
Returns true if the type could be decomposed with a surrogate.
Definition SerializationInfo.h:724
const SerializationInfo & getMember(const std::string &name) const
Get a struct member.
Definition SerializationInfo.h:496
void rebind(void *obj) const
Rebind to new address.
const SerializationInfo * findMember(const char *name) const
Find a struct member.
Unicode capable basic_string.
Definition Api-String.h:67
size_type length() const
Returns the length of the string.
Definition Api-String.h:258
const Pt::Char * c_str() const
Returns a null terminated C string.
Definition Api-String.h:288
Converts between character encodings.
Definition Api-TextCodec.h:44
uint_type uint16_t
Unsigned 16-bit integer type.
Definition Api-Types.h:38
int_type int64_t
Signed 64-bit integer type.
Definition Api-Types.h:59
int_type int32_t
Signed 32-bit integer type.
Definition Api-Types.h:45
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
uint_type uint64_t
Unsigned 64-bit integer type.
Definition Api-Types.h:66
int_type int16_t
Signed 16-bit integer type.
Definition Api-Types.h:31
uint_type uint8_t
Unsigned 8-bit integer type.
Definition Api-Types.h:24
int_type int8_t
Signed 8-bit integer type.
Definition Api-Types.h:17
void load(const LoadInfo &li, T &type)
Loads referencable types.
Definition SerializationInfo.h:973
void save(SaveInfo &si, const T &type)
Saves referencable types.
Definition SerializationInfo.h:902
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67
uint32_t value() const
Returns the unicode value.
Definition String.h:94