sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
template <class T>
|
sl@0
|
20 |
inline REncodeWriteStream& operator<<(REncodeWriteStream& aStream,const T& anObject)
|
sl@0
|
21 |
{
|
sl@0
|
22 |
const std::string &name = anObject.Name();
|
sl@0
|
23 |
|
sl@0
|
24 |
if(!aStream.Quiet())
|
sl@0
|
25 |
{
|
sl@0
|
26 |
// Generate human readable progress
|
sl@0
|
27 |
REncodeWriteStream tmp(prog);
|
sl@0
|
28 |
tmp << anObject;
|
sl@0
|
29 |
}
|
sl@0
|
30 |
|
sl@0
|
31 |
if(aStream.HumanReadable())
|
sl@0
|
32 |
{
|
sl@0
|
33 |
if(anObject.CommentOnlyInHumanMode() && !aStream.Verbose())
|
sl@0
|
34 |
{
|
sl@0
|
35 |
return aStream;
|
sl@0
|
36 |
}
|
sl@0
|
37 |
|
sl@0
|
38 |
aStream.WriteIndent();
|
sl@0
|
39 |
if(anObject.CommentOnlyInHumanMode())
|
sl@0
|
40 |
{
|
sl@0
|
41 |
aStream.WriteByte('#');
|
sl@0
|
42 |
}
|
sl@0
|
43 |
aStream.WriteBin(name.data(), name.size());
|
sl@0
|
44 |
aStream.WriteSpace();
|
sl@0
|
45 |
EncodeHuman(aStream, anObject.Value());
|
sl@0
|
46 |
aStream.WriteLineEnd();
|
sl@0
|
47 |
}
|
sl@0
|
48 |
else
|
sl@0
|
49 |
{
|
sl@0
|
50 |
// Just write as binary "store" format
|
sl@0
|
51 |
aStream.StoreWriteStream() << anObject.Value();
|
sl@0
|
52 |
}
|
sl@0
|
53 |
return aStream;
|
sl@0
|
54 |
}
|
sl@0
|
55 |
|
sl@0
|
56 |
template <typename T>
|
sl@0
|
57 |
REncodeWriteStream& operator<<(REncodeWriteStream& aStream, const EncDecEnum<T> &aEncDecEnum)
|
sl@0
|
58 |
{
|
sl@0
|
59 |
const std::string &name = aEncDecEnum.Name();
|
sl@0
|
60 |
|
sl@0
|
61 |
if(!aStream.Quiet())
|
sl@0
|
62 |
{
|
sl@0
|
63 |
// Generate human readable progress
|
sl@0
|
64 |
REncodeWriteStream tmp(prog);
|
sl@0
|
65 |
tmp << aEncDecEnum;
|
sl@0
|
66 |
}
|
sl@0
|
67 |
|
sl@0
|
68 |
if(aStream.HumanReadable())
|
sl@0
|
69 |
{
|
sl@0
|
70 |
if(aEncDecEnum.CommentOnlyInHumanMode() && !aStream.Verbose())
|
sl@0
|
71 |
{
|
sl@0
|
72 |
return aStream;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
aStream.WriteIndent();
|
sl@0
|
75 |
if(aEncDecEnum.CommentOnlyInHumanMode())
|
sl@0
|
76 |
{
|
sl@0
|
77 |
aStream.WriteByte('#');
|
sl@0
|
78 |
}
|
sl@0
|
79 |
aStream.WriteBin(name.data(), name.size());
|
sl@0
|
80 |
aStream.WriteSpace();
|
sl@0
|
81 |
const char *enumName = aEncDecEnum.ValueName();
|
sl@0
|
82 |
if(enumName)
|
sl@0
|
83 |
{
|
sl@0
|
84 |
aStream.WriteByte('"');
|
sl@0
|
85 |
aStream.WriteBin(enumName, strlen(enumName));
|
sl@0
|
86 |
aStream.WriteByte('"');
|
sl@0
|
87 |
if(aStream.Verbose())
|
sl@0
|
88 |
{
|
sl@0
|
89 |
// In verbose mode so include numeric value as a comment
|
sl@0
|
90 |
aStream.WriteCStr(" # ");
|
sl@0
|
91 |
EncodeHuman(aStream, TUint32(aEncDecEnum.Value()));
|
sl@0
|
92 |
}
|
sl@0
|
93 |
}
|
sl@0
|
94 |
else
|
sl@0
|
95 |
{
|
sl@0
|
96 |
// Symbolic value not available, so write as numeric
|
sl@0
|
97 |
EncodeHuman(aStream, TUint32(aEncDecEnum.Value()));
|
sl@0
|
98 |
}
|
sl@0
|
99 |
aStream.WriteLineEnd();
|
sl@0
|
100 |
}
|
sl@0
|
101 |
else
|
sl@0
|
102 |
{
|
sl@0
|
103 |
// Just write as binary "store" format
|
sl@0
|
104 |
aStream.StoreWriteStream() << aEncDecEnum.Value();
|
sl@0
|
105 |
}
|
sl@0
|
106 |
return aStream;
|
sl@0
|
107 |
}
|
sl@0
|
108 |
|
sl@0
|
109 |
|
sl@0
|
110 |
template <class T>
|
sl@0
|
111 |
inline RDecodeReadStream& operator>>(RDecodeReadStream& aStream,T& anObject)
|
sl@0
|
112 |
{
|
sl@0
|
113 |
const std::string &name = anObject.Name();
|
sl@0
|
114 |
if(aStream.HumanReadable())
|
sl@0
|
115 |
{
|
sl@0
|
116 |
if(anObject.CommentOnlyInHumanMode())
|
sl@0
|
117 |
{
|
sl@0
|
118 |
return aStream; // Not accepted in human mode, so just return...
|
sl@0
|
119 |
}
|
sl@0
|
120 |
// Need to convert to ascii
|
sl@0
|
121 |
aStream.CheckName(name);
|
sl@0
|
122 |
DecodeHuman(aStream, anObject.Value());
|
sl@0
|
123 |
}
|
sl@0
|
124 |
else
|
sl@0
|
125 |
{
|
sl@0
|
126 |
// Just read binary
|
sl@0
|
127 |
aStream.iReadStream >> anObject.Value();
|
sl@0
|
128 |
}
|
sl@0
|
129 |
|
sl@0
|
130 |
// Generate human readable progress
|
sl@0
|
131 |
// prog.WriteIndent();
|
sl@0
|
132 |
REncodeWriteStream tmp(prog);
|
sl@0
|
133 |
tmp << anObject;
|
sl@0
|
134 |
|
sl@0
|
135 |
return aStream;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
|
sl@0
|
138 |
template <typename T>
|
sl@0
|
139 |
RDecodeReadStream& operator>>(RDecodeReadStream& aStream, EncDecEnum<T> &aEncDecEnum)
|
sl@0
|
140 |
{
|
sl@0
|
141 |
const std::string &name = aEncDecEnum.Name();
|
sl@0
|
142 |
if(aStream.HumanReadable())
|
sl@0
|
143 |
{
|
sl@0
|
144 |
if(aEncDecEnum.CommentOnlyInHumanMode())
|
sl@0
|
145 |
{
|
sl@0
|
146 |
return aStream; // Not accepted in human mode, so just return...
|
sl@0
|
147 |
}
|
sl@0
|
148 |
// Need to convert to ascii
|
sl@0
|
149 |
aStream.CheckName(name);
|
sl@0
|
150 |
|
sl@0
|
151 |
aStream.ReadNextToken();
|
sl@0
|
152 |
aEncDecEnum.SetValue(aStream.Token().c_str());
|
sl@0
|
153 |
}
|
sl@0
|
154 |
else
|
sl@0
|
155 |
{
|
sl@0
|
156 |
// Just read binary
|
sl@0
|
157 |
aStream.iReadStream >> aEncDecEnum.Value();
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
// Generate human readable progress
|
sl@0
|
161 |
// prog.WriteIndent();
|
sl@0
|
162 |
REncodeWriteStream tmp(prog);
|
sl@0
|
163 |
tmp << aEncDecEnum;
|
sl@0
|
164 |
|
sl@0
|
165 |
return aStream;
|
sl@0
|
166 |
}
|
sl@0
|
167 |
|
sl@0
|
168 |
|
sl@0
|
169 |
|
sl@0
|
170 |
template<typename T>
|
sl@0
|
171 |
EncDecEnum<T>::EncDecEnum(const char *aName, const EnumEntry *aEnumEntries, bool aCommentOnlyInHumanMode)
|
sl@0
|
172 |
: iName(aName), iEnumEntries(aEnumEntries), iCommentOnlyInHumanMode(aCommentOnlyInHumanMode), iValue(0)
|
sl@0
|
173 |
{
|
sl@0
|
174 |
if(iEnumEntries == 0)
|
sl@0
|
175 |
{
|
sl@0
|
176 |
FatalError(); // programing error
|
sl@0
|
177 |
}
|
sl@0
|
178 |
}
|
sl@0
|
179 |
|
sl@0
|
180 |
|
sl@0
|
181 |
|
sl@0
|
182 |
template<typename T>
|
sl@0
|
183 |
void EncDecEnum<T>::SetValue(const T &aValue)
|
sl@0
|
184 |
{
|
sl@0
|
185 |
// Search enum entires which one which matches
|
sl@0
|
186 |
if(ValueToName(aValue) == 0)
|
sl@0
|
187 |
{
|
sl@0
|
188 |
dbg << Log::Endl();
|
sl@0
|
189 |
dbg << Log::Indent() << "Unknown value for enum " << iName << " value " << TUint32(aValue) << Log::Endl();
|
sl@0
|
190 |
// FatalError();
|
sl@0
|
191 |
}
|
sl@0
|
192 |
|
sl@0
|
193 |
iValue = aValue;
|
sl@0
|
194 |
return;
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
template<typename T>
|
sl@0
|
198 |
void EncDecEnum<T>::SetValue(const char *aName)
|
sl@0
|
199 |
{
|
sl@0
|
200 |
// Search enum entires which one which matches
|
sl@0
|
201 |
const EnumEntry *p = iEnumEntries;
|
sl@0
|
202 |
while(p && p->iName)
|
sl@0
|
203 |
{
|
sl@0
|
204 |
// We ignore case when matching enums
|
sl@0
|
205 |
if(strcasecmp(aName, p->iName) == 0)
|
sl@0
|
206 |
{
|
sl@0
|
207 |
// Found it
|
sl@0
|
208 |
iValue = p->iValue;
|
sl@0
|
209 |
return;
|
sl@0
|
210 |
}
|
sl@0
|
211 |
++p;
|
sl@0
|
212 |
}
|
sl@0
|
213 |
|
sl@0
|
214 |
dbg << Log::Endl();
|
sl@0
|
215 |
dbg << Log::Indent() << "Unknown value for enum " << iName << " value '" << aName << "'" << Log::Endl();
|
sl@0
|
216 |
std::string str(aName);
|
sl@0
|
217 |
iValue = ReadUnsignedNumber(str,sizeof(T));
|
sl@0
|
218 |
}
|
sl@0
|
219 |
|
sl@0
|
220 |
template<typename T>
|
sl@0
|
221 |
const char *EncDecEnum<T>::ValueToName(const T &aValue) const
|
sl@0
|
222 |
{
|
sl@0
|
223 |
if(iEnumEntries == 0) FatalError();
|
sl@0
|
224 |
const EnumEntry *p = iEnumEntries;
|
sl@0
|
225 |
while(p && p->iName)
|
sl@0
|
226 |
{
|
sl@0
|
227 |
if(p->iValue == aValue)
|
sl@0
|
228 |
{
|
sl@0
|
229 |
return p->iName;
|
sl@0
|
230 |
}
|
sl@0
|
231 |
++p;
|
sl@0
|
232 |
}
|
sl@0
|
233 |
return 0;
|
sl@0
|
234 |
}
|
sl@0
|
235 |
|
sl@0
|
236 |
|
sl@0
|
237 |
// end of file
|
sl@0
|
238 |
|