|
1 /* |
|
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef SERIALISER_H |
|
20 #define SERIALISER_H |
|
21 |
|
22 #include <iostream> |
|
23 #include <ostream> |
|
24 #include <vector> |
|
25 #include "symbiantypes.h" |
|
26 #include "serialisable.h" |
|
27 #include "ucmp.h" |
|
28 #include "cardinality.h" |
|
29 |
|
30 /** |
|
31 * @file SERIALISER.H |
|
32 * |
|
33 * @internalComponent |
|
34 * @released |
|
35 */ |
|
36 class Serialiser |
|
37 { |
|
38 public: |
|
39 Serialiser(std::ostream& stream) : iStream(stream) |
|
40 {} |
|
41 |
|
42 // integer types |
|
43 Serialiser& operator<<(TInt64& val) |
|
44 { iStream.write(reinterpret_cast<char*>(&val),8); return *this; } |
|
45 |
|
46 Serialiser& operator<<(TInt& val) |
|
47 { iStream.write((char*)&val,4); return *this; } |
|
48 |
|
49 Serialiser& operator<<(TInt16& val) |
|
50 { iStream.write((char*)&val,2); return *this; } |
|
51 |
|
52 Serialiser& operator<<(TInt8& val) |
|
53 { iStream.write((char*)&val,1); return *this; } |
|
54 |
|
55 Serialiser& operator<<(TUint64& val) |
|
56 { iStream.write(reinterpret_cast<char*>(&val),8); return *this; } |
|
57 |
|
58 Serialiser& operator<<(TUint32& val) |
|
59 { iStream.write((char*)&val,4); return *this; } |
|
60 |
|
61 Serialiser& operator<<(TUint16& val) |
|
62 { iStream.write((char*)&val,2); return *this; } |
|
63 |
|
64 Serialiser& operator<<(TUint8& val) |
|
65 { iStream.write((char*)&val,1); return *this; } |
|
66 |
|
67 Serialiser& operator<<(bool& val) |
|
68 { TUint32 v = ((TUint32)val); return *this << v; } |
|
69 |
|
70 // STL containers |
|
71 Serialiser& operator<<(std::string& val) |
|
72 { |
|
73 Cardinality card; |
|
74 TUint32 size = (val.size() << 1) + 0x01; |
|
75 card.SetSize(size); |
|
76 *this << card; |
|
77 iStream.write(val.c_str(),val.size()); |
|
78 |
|
79 return *this; |
|
80 } |
|
81 |
|
82 Serialiser& operator<<(std::wstring& val) |
|
83 { |
|
84 Cardinality card; |
|
85 TUint32 size = val.size() << 1; |
|
86 card.SetSize(size); |
|
87 *this << card; |
|
88 |
|
89 TUnicodeCompressor comp; |
|
90 TMemoryUnicodeSource src((TUint16*)val.c_str()); |
|
91 //The compressed unicode output could end up larger than the input, thus restricting the output to KMaxTInt. |
|
92 comp.CompressL(*this, src, KMaxTInt, val.size()); |
|
93 return *this; |
|
94 } |
|
95 |
|
96 template <class T> |
|
97 Serialiser& operator<<(std::vector<T>& val) |
|
98 { |
|
99 TUint32 size = val.size(); |
|
100 *this << size; |
|
101 for (TUint32 i = 0; i < size ; ++i) |
|
102 { |
|
103 *this << val[i]; |
|
104 } |
|
105 return *this; |
|
106 } |
|
107 |
|
108 // direct write access to buffer |
|
109 void write(const TUint8* aSrc, TUint32 aCount) |
|
110 { |
|
111 iStream.write((const char*)aSrc, aCount); |
|
112 } |
|
113 |
|
114 |
|
115 // serialisable objects |
|
116 Serialiser& operator<<(Serialisable& val) |
|
117 { val.Externalize(*this); return *this; } |
|
118 |
|
119 Serialiser& operator<<(Serialisable* val) |
|
120 { val->Externalize(*this); return *this; } |
|
121 |
|
122 private: |
|
123 std::ostream& iStream; |
|
124 }; |
|
125 |
|
126 |
|
127 #endif /* SERIALISER_H */ |