1 /* |
|
2 * Copyright (c) 2003-2005 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: Implementation of class TLiwVariant. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 // INCLUDES |
|
25 #ifdef _DEBUG |
|
26 #include <e32svr.h> |
|
27 #endif |
|
28 #include "liwvariant.h" |
|
29 #include "liwgenericparam.h" |
|
30 |
|
31 // ============================= LOCAL FUNCTIONS =============================== |
|
32 |
|
33 using namespace LIW; |
|
34 namespace { |
|
35 |
|
36 // CONSTANTS |
|
37 /// TLiwVariant streaming version |
|
38 const TInt KVersion = 10; // Version 1.0 |
|
39 |
|
40 |
|
41 const TInt KBufSizeMid = 128; |
|
42 const TInt KBufSizeSmall = 64; |
|
43 const TInt KBufSizeLarge = 8192; |
|
44 |
|
45 |
|
46 // Debug helpers |
|
47 #ifdef _DEBUG |
|
48 |
|
49 enum TPanicCode |
|
50 { |
|
51 EPanicPostCond_Constructor = 1, |
|
52 EPanicPostCond_Reset, |
|
53 EPanicPostCond_Set_TInt32, |
|
54 EPanicPostCond_Set_TUid, |
|
55 EPanicPostCond_Set_TTime, |
|
56 EPanicPostCond_Set_TDesC, |
|
57 EPanicPostCond_CopyL, |
|
58 EPanicInvariant_InvalidDesCState, |
|
59 EPanicPostCond_Set_TDesC8, |
|
60 EPanicPreCond_DataSizeMismatch, |
|
61 EPanicPostCond_Set_TBool, |
|
62 EPanicPostCond_Set_TUint, |
|
63 EPanicPostCond_Set_TReal, |
|
64 EPanicPostCond_Set_TInt64 |
|
65 }; |
|
66 |
|
67 void Panic(TPanicCode aCode) |
|
68 { |
|
69 _LIT(KPanicText, "TLiwVariant"); |
|
70 User::Panic(KPanicText, aCode); |
|
71 } |
|
72 |
|
73 #endif // #ifdef _DEBUG |
|
74 |
|
75 } // namespace |
|
76 |
|
77 _LIT8(KDummy, "Dummy"); // needed for BC-preserving hack to store info about ownership of container |
|
78 |
|
79 /* |
|
80 * class CLiwContainer, class CLiwIterable, class CLiwList, class CLiwMap, |
|
81 * class CLiwDefaultList, class CLiwDefaultMap |
|
82 */ |
|
83 |
|
84 EXPORT_C void CLiwContainer::PushL() |
|
85 { |
|
86 CleanupClosePushL(*this); |
|
87 } |
|
88 |
|
89 EXPORT_C void CLiwContainer::ExternalizeL(RWriteStream&) const |
|
90 { |
|
91 User::Leave(KErrNotSupported); |
|
92 } |
|
93 |
|
94 EXPORT_C TInt CLiwContainer::Size() const |
|
95 { |
|
96 return KErrNotSupported; |
|
97 } |
|
98 |
|
99 EXPORT_C CLiwContainer::~CLiwContainer() |
|
100 { |
|
101 /* |
|
102 * Since CLiwContainer is based on reference count mechanism, it does not go with CleanupStack::PushL() |
|
103 * |
|
104 * LIW takes care of managing the memory in heap for CLiwContainer using the reference count logic |
|
105 * |
|
106 * The container should not be destroyed until the reference count is ZERO. |
|
107 * If the reference count is not equal to ZERO, it means there are some more owners of that container |
|
108 * |
|
109 * This panics if the reference count is not equal to ZERO, in DEBUG Mode only |
|
110 * |
|
111 * Hence it is recommended to use CleanupClosePushL for Container objects when using CleanupStack |
|
112 */ |
|
113 |
|
114 __ASSERT_DEBUG(iRefCount == 0, User::Panic(_L("E32USER-CBase: 33"), 1)); |
|
115 } |
|
116 |
|
117 EXPORT_C void CLiwContainer::Close() |
|
118 { |
|
119 DecRef(); |
|
120 } |
|
121 |
|
122 |
|
123 EXPORT_C TBool CLiwIterable::operator==(CLiwIterable& aOther) |
|
124 { |
|
125 if (this == &aOther) |
|
126 return ETrue; |
|
127 |
|
128 Reset(); |
|
129 aOther.Reset(); |
|
130 |
|
131 TBool rval = ETrue; |
|
132 TLiwVariant variant1, variant2; |
|
133 |
|
134 TRAPD(error, { |
|
135 while (NextL(variant1)) |
|
136 { |
|
137 if (!aOther.NextL(variant2) || (variant1 != variant2)) |
|
138 rval = EFalse; |
|
139 } |
|
140 }); |
|
141 |
|
142 if (rval && (error == KErrNone)) |
|
143 { |
|
144 TRAP(error, (rval = !(aOther.NextL(variant2)))); |
|
145 } |
|
146 |
|
147 variant1.Reset(); |
|
148 variant2.Reset(); |
|
149 |
|
150 return (error == KErrNone) ? rval : EFalse; |
|
151 } |
|
152 |
|
153 EXPORT_C TBool CLiwList::operator==(const CLiwList& aOther) const |
|
154 { |
|
155 TInt count = Count(); |
|
156 if (count != aOther.Count()) |
|
157 return EFalse; |
|
158 |
|
159 TInt error = KErrNone; |
|
160 TBool retval = ETrue; |
|
161 TLiwVariant variant1, variant2; |
|
162 for (TInt index = 0; index < count; index++) { |
|
163 TRAP(error, { |
|
164 AtL(index, variant1); aOther.AtL(index, variant2); |
|
165 }); |
|
166 if ((error != KErrNone) || (!(variant1 == variant2))) |
|
167 { |
|
168 retval = EFalse; |
|
169 break; |
|
170 } |
|
171 } |
|
172 variant1.Reset(); |
|
173 variant2.Reset(); |
|
174 return retval; |
|
175 } |
|
176 |
|
177 EXPORT_C TBool CLiwMap::operator==(const CLiwMap& aOther) const |
|
178 { |
|
179 TInt count = Count(); |
|
180 if (count != aOther.Count()) |
|
181 return EFalse; |
|
182 |
|
183 TInt error = KErrNone; |
|
184 TBool retval = ETrue; |
|
185 TLiwVariant variant1, variant2; |
|
186 TBuf8<KBufSizeMid> buf1, buf2; |
|
187 for (TInt index = 0; index < count; index++) |
|
188 { |
|
189 TRAP(error, { |
|
190 AtL(index, buf1); aOther.AtL(index, buf2); |
|
191 retval = (FindL(buf1, variant1) && aOther.FindL(buf2, variant2)); |
|
192 }); |
|
193 if (error != KErrNone) |
|
194 retval = EFalse; |
|
195 if (!retval || (!(variant1 == variant2))) |
|
196 { |
|
197 retval = EFalse; |
|
198 break; |
|
199 } |
|
200 } |
|
201 variant1.Reset(); |
|
202 variant2.Reset(); |
|
203 return retval; |
|
204 } |
|
205 |
|
206 EXPORT_C CLiwDefaultList* CLiwDefaultList::NewL() |
|
207 { |
|
208 CLiwGenericParamList* gl = CLiwGenericParamList::NewLC(); |
|
209 CLiwDefaultList* tempList = new (ELeave) CLiwDefaultList(gl); |
|
210 CleanupStack::Pop(gl); // gl |
|
211 return tempList; |
|
212 } |
|
213 |
|
214 EXPORT_C CLiwDefaultList* CLiwDefaultList::NewLC() |
|
215 { |
|
216 CLiwGenericParamList* gl = CLiwGenericParamList::NewLC(); |
|
217 CLiwDefaultList* tempList = new (ELeave) CLiwDefaultList(gl); |
|
218 CleanupStack::Pop(gl); // gl |
|
219 CleanupClosePushL(*tempList); |
|
220 return tempList; |
|
221 } |
|
222 |
|
223 EXPORT_C CLiwDefaultList* CLiwDefaultList::NewLC(RReadStream& aStream) |
|
224 { |
|
225 CLiwDefaultList* tempList = CLiwDefaultList::NewL(); |
|
226 CleanupClosePushL(*tempList); |
|
227 tempList->iList->InternalizeL(aStream); |
|
228 return tempList; |
|
229 } |
|
230 |
|
231 EXPORT_C void CLiwDefaultList::AppendL(const TLiwVariant& aValue) |
|
232 { |
|
233 iList->AppendL(TLiwGenericParam(EGenericParamUnspecified, aValue)); |
|
234 } |
|
235 |
|
236 EXPORT_C TBool CLiwDefaultList::AtL(TInt aIndex, TLiwVariant& aValue) const |
|
237 { |
|
238 if(0 <= aIndex && aIndex < iList->Count()) |
|
239 { |
|
240 aValue.SetL((*iList)[aIndex].Value()); |
|
241 return ETrue; |
|
242 } |
|
243 else |
|
244 { |
|
245 return EFalse; |
|
246 } |
|
247 } |
|
248 |
|
249 EXPORT_C void CLiwDefaultList::Remove(TInt aIndex) |
|
250 { |
|
251 if(0 <= aIndex && aIndex < iList->Count()) |
|
252 { |
|
253 iList->iParameters[aIndex].Destroy(); |
|
254 iList->iParameters.Remove(aIndex); |
|
255 } |
|
256 } |
|
257 |
|
258 EXPORT_C TInt CLiwDefaultList::Count() const |
|
259 { |
|
260 return iList->Count(); |
|
261 } |
|
262 |
|
263 EXPORT_C void CLiwDefaultList::ExternalizeL(RWriteStream& aStream) const |
|
264 { |
|
265 iList->ExternalizeL(aStream); |
|
266 } |
|
267 |
|
268 EXPORT_C TInt CLiwDefaultList::Size() const |
|
269 { |
|
270 return iList->Size(); |
|
271 } |
|
272 |
|
273 EXPORT_C CLiwDefaultList::~CLiwDefaultList() |
|
274 { |
|
275 delete iList; |
|
276 } |
|
277 |
|
278 EXPORT_C CLiwDefaultMap* CLiwDefaultMap::NewL() |
|
279 { |
|
280 CLiwGenericParamList* gl = CLiwGenericParamList::NewLC(); |
|
281 CLiwDefaultMap* tempMap = new (ELeave) CLiwDefaultMap(gl); |
|
282 CleanupStack::Pop(gl); // gl |
|
283 return tempMap; |
|
284 } |
|
285 |
|
286 EXPORT_C CLiwDefaultMap* CLiwDefaultMap::NewLC() |
|
287 { |
|
288 CLiwGenericParamList* gl = CLiwGenericParamList::NewLC(); |
|
289 CLiwDefaultMap* tempMap = new (ELeave) CLiwDefaultMap(gl); |
|
290 CleanupStack::Pop(gl); // gl |
|
291 CleanupClosePushL(*tempMap); |
|
292 return tempMap; |
|
293 } |
|
294 |
|
295 EXPORT_C CLiwDefaultMap* CLiwDefaultMap::NewLC(RReadStream& aStream) |
|
296 { |
|
297 CLiwDefaultMap* tempMap = CLiwDefaultMap::NewL(); |
|
298 CleanupClosePushL(*tempMap); |
|
299 tempMap->iMap->InternalizeL(aStream); |
|
300 return tempMap; |
|
301 } |
|
302 |
|
303 EXPORT_C void CLiwDefaultMap::InsertL(const TDesC8& aKey, const TLiwVariant& aValue) |
|
304 { |
|
305 Remove(aKey); |
|
306 TLiwGenericParam mp; |
|
307 mp.SetNameAndValueL(aKey, aValue); |
|
308 iMap->AppendL(mp); |
|
309 mp.Reset(); |
|
310 } |
|
311 |
|
312 EXPORT_C TBool CLiwDefaultMap::FindL(const TDesC8& aKey, TLiwVariant& aValue) const |
|
313 { |
|
314 TInt pos = 0; |
|
315 const TLiwGenericParam* tempParam = iMap->FindFirst(pos, aKey); |
|
316 if (tempParam) |
|
317 { |
|
318 aValue.SetL(tempParam->Value()); |
|
319 return ETrue; |
|
320 } |
|
321 else |
|
322 return EFalse; |
|
323 } |
|
324 |
|
325 EXPORT_C TInt CLiwDefaultMap::Count() const |
|
326 { |
|
327 return iMap->Count(); |
|
328 } |
|
329 |
|
330 EXPORT_C TBool CLiwDefaultMap::AtL(TInt aIndex, TDes8& aKey) const |
|
331 { |
|
332 if(0 <= aIndex && aIndex < iMap->Count()) |
|
333 { |
|
334 aKey = ((*iMap)[aIndex]).Name(); |
|
335 return ETrue; |
|
336 } |
|
337 else |
|
338 { |
|
339 return EFalse; |
|
340 } |
|
341 } |
|
342 |
|
343 EXPORT_C const TDesC8& CLiwDefaultMap::AtL(TInt aIndex) const |
|
344 { |
|
345 if(0 <= aIndex && aIndex < iMap->Count()) |
|
346 { |
|
347 return ((*iMap)[aIndex]).Name(); |
|
348 } |
|
349 else |
|
350 { |
|
351 User::LeaveIfError(KErrArgument); |
|
352 return KNullDesC8; |
|
353 } |
|
354 } |
|
355 |
|
356 EXPORT_C void CLiwDefaultMap::Remove(const TDesC8& aKey) |
|
357 { |
|
358 TInt pos = 0; |
|
359 iMap->FindFirst(pos, aKey); |
|
360 if (pos != KErrNotFound) |
|
361 { |
|
362 iMap->iParameters[pos].Destroy(); |
|
363 iMap->iParameters.Remove(pos); |
|
364 } |
|
365 } |
|
366 |
|
367 EXPORT_C void CLiwDefaultMap::ExternalizeL(RWriteStream& aStream) const |
|
368 { |
|
369 iMap->ExternalizeL(aStream); |
|
370 } |
|
371 |
|
372 EXPORT_C TInt CLiwDefaultMap::Size() const |
|
373 { |
|
374 return iMap->Size(); |
|
375 } |
|
376 |
|
377 EXPORT_C CLiwDefaultMap::~CLiwDefaultMap() |
|
378 { |
|
379 delete iMap; |
|
380 } |
|
381 |
|
382 // ============================ MEMBER FUNCTIONS =============================== |
|
383 |
|
384 #ifdef _DEBUG |
|
385 void TLiwVariant::__DbgTestInvariant() const |
|
386 { |
|
387 if (iTypeId==EVariantTypeDesC && iData.iBufC) |
|
388 { |
|
389 __ASSERT_ALWAYS(iData.iBufC->Ptr() == iPtrC.Ptr(), |
|
390 Panic(EPanicInvariant_InvalidDesCState)); |
|
391 |
|
392 } |
|
393 } |
|
394 #endif // #ifdef _DEBUG |
|
395 |
|
396 |
|
397 EXPORT_C void TLiwVariant::PushL() |
|
398 { |
|
399 CleanupStack::PushL( TCleanupItem( TLiwVariant::VariantCleanup , this) ); |
|
400 } |
|
401 |
|
402 EXPORT_C void TLiwVariant::VariantCleanup( TAny* aObj ) |
|
403 { |
|
404 static_cast<TLiwVariant*>(aObj)->Destroy(); |
|
405 } |
|
406 |
|
407 inline void TLiwVariant::SInt64::InternalizeL(RReadStream& aStream) |
|
408 { |
|
409 TInt32 low = aStream.ReadInt32L(); |
|
410 iHigh = aStream.ReadInt32L(); |
|
411 iLow = low; |
|
412 } |
|
413 |
|
414 inline void TLiwVariant::SInt64::ExternalizeL(RWriteStream& aStream) const |
|
415 { |
|
416 aStream.WriteInt32L(iLow); |
|
417 aStream.WriteInt32L(iHigh); |
|
418 } |
|
419 |
|
420 inline void TLiwVariant::SInt64::Set(const TInt64& aTInt64) |
|
421 { |
|
422 iLow = I64LOW(aTInt64); |
|
423 iHigh = I64HIGH(aTInt64); |
|
424 } |
|
425 |
|
426 inline TLiwVariant::SInt64::operator TInt64() const |
|
427 { |
|
428 return MAKE_TINT64(iHigh,iLow); |
|
429 } |
|
430 |
|
431 inline TBool TLiwVariant::SInt64::operator==(const SInt64& aRhs) const |
|
432 { |
|
433 return (iHigh==aRhs.iHigh && iLow==aRhs.iLow); |
|
434 } |
|
435 |
|
436 |
|
437 EXPORT_C TLiwVariant::TLiwVariant(const TLiwVariant& aSrc) : |
|
438 iTypeId(aSrc.iTypeId), iData(aSrc.iData), iPtrC(), iPtrC8() |
|
439 { |
|
440 if (iTypeId == EVariantTypeDesC) |
|
441 { |
|
442 iPtrC.Set(aSrc.iPtrC); |
|
443 // Do not take ownership of data |
|
444 iData.iBufC = NULL; |
|
445 } |
|
446 else if ( iTypeId == EVariantTypeDesC8 ) |
|
447 { |
|
448 iPtrC8.Set( aSrc.iPtrC8 ); |
|
449 // Do not take ownership of data |
|
450 iData.iBufC8 = NULL; |
|
451 } |
|
452 } |
|
453 |
|
454 EXPORT_C TLiwVariant& TLiwVariant::operator=(const TLiwVariant& aSrc) |
|
455 { |
|
456 // Check self-assignment first. |
|
457 if (this == &aSrc) |
|
458 { |
|
459 return *this; |
|
460 } |
|
461 Reset(); |
|
462 iTypeId = aSrc.iTypeId; |
|
463 iData = aSrc.iData; |
|
464 if (iTypeId == EVariantTypeDesC) |
|
465 { |
|
466 iPtrC.Set(aSrc.iPtrC); |
|
467 // Do not take ownership of data |
|
468 iData.iBufC = NULL; |
|
469 } |
|
470 else if ( iTypeId == EVariantTypeDesC8 ) |
|
471 { |
|
472 iPtrC8.Set( aSrc.iPtrC8 ); |
|
473 // Do not take ownership of data |
|
474 iData.iBufC8 = NULL; |
|
475 } |
|
476 return *this; |
|
477 } |
|
478 |
|
479 EXPORT_C TBool TLiwVariant::Get(TInt32& aValue) const |
|
480 { |
|
481 __TEST_INVARIANT; |
|
482 if (iTypeId == EVariantTypeTInt32) |
|
483 { |
|
484 aValue = iData.iInt32; |
|
485 return ETrue; |
|
486 } |
|
487 else if (iTypeId == EVariantTypeTReal) |
|
488 { |
|
489 aValue = (TInt32)iData.iReal; |
|
490 return ETrue; |
|
491 } |
|
492 else if (iTypeId == EVariantTypeDesC8) //string to real conversion |
|
493 { |
|
494 TLex8 parseString(iPtrC8); |
|
495 TInt err = parseString.Val(aValue); |
|
496 if(err == KErrNone) |
|
497 return ETrue; |
|
498 } |
|
499 else if (iTypeId == EVariantTypeDesC) //string to real conversion |
|
500 { |
|
501 TLex16 parseString(iPtrC); |
|
502 TInt err = parseString.Val(aValue); |
|
503 if(err == KErrNone) |
|
504 return ETrue; |
|
505 } |
|
506 else if (iTypeId == EVariantTypeTUint) //TUint to int conversion |
|
507 { |
|
508 aValue = (TInt32)iData.iUint; |
|
509 return ETrue; |
|
510 } |
|
511 else if (iTypeId == EVariantTypeTInt64) //TInt64 to TInt32 conversion |
|
512 { |
|
513 aValue = (TInt32)iData.iLong; //Warning : Results in loss of data sometimes |
|
514 return ETrue; |
|
515 } |
|
516 return EFalse; |
|
517 } |
|
518 |
|
519 EXPORT_C TBool TLiwVariant::Get(TInt64& aValue) const |
|
520 { |
|
521 __TEST_INVARIANT; |
|
522 if (iTypeId == EVariantTypeTInt64) |
|
523 { |
|
524 aValue = iData.iLong; |
|
525 return ETrue; |
|
526 } |
|
527 else if (iTypeId == EVariantTypeTInt32) |
|
528 { |
|
529 aValue = iData.iInt32; |
|
530 return ETrue; |
|
531 } |
|
532 else if (iTypeId == EVariantTypeTReal) |
|
533 { |
|
534 aValue = (TInt32)iData.iReal; |
|
535 return ETrue; |
|
536 } |
|
537 else if (iTypeId == EVariantTypeDesC8) //string to real conversion |
|
538 { |
|
539 TLex8 parseString(iPtrC8); |
|
540 TInt err = parseString.Val(aValue); |
|
541 if(err == KErrNone) |
|
542 return ETrue; |
|
543 } |
|
544 else if (iTypeId == EVariantTypeDesC) //string to real conversion |
|
545 { |
|
546 TLex16 parseString(iPtrC); |
|
547 TInt err = parseString.Val(aValue); |
|
548 if(err == KErrNone) |
|
549 return ETrue; |
|
550 } |
|
551 else if (iTypeId == EVariantTypeTUint) //TUint to int conversion |
|
552 { |
|
553 aValue = (TInt32)iData.iUint; |
|
554 return ETrue; |
|
555 } |
|
556 return EFalse; |
|
557 } |
|
558 |
|
559 EXPORT_C TBool TLiwVariant::Get(TReal& aValue) const |
|
560 { |
|
561 __TEST_INVARIANT; |
|
562 if (iTypeId == EVariantTypeTReal) |
|
563 { |
|
564 aValue = iData.iReal; |
|
565 return ETrue; |
|
566 } |
|
567 else if(iTypeId == EVariantTypeTInt32) |
|
568 { |
|
569 aValue = iData.iInt32; |
|
570 return ETrue; |
|
571 } |
|
572 else if (iTypeId == EVariantTypeDesC8) //string to integer conversion |
|
573 { |
|
574 TLex8 parseString(iPtrC8); |
|
575 TInt err = parseString.Val(aValue); |
|
576 if(err == KErrNone) |
|
577 return ETrue; |
|
578 } |
|
579 else if (iTypeId == EVariantTypeDesC) //string to integer conversion |
|
580 { |
|
581 TLex16 parseString(iPtrC); |
|
582 TInt err = parseString.Val(aValue); |
|
583 if(err == KErrNone) |
|
584 return ETrue; |
|
585 } |
|
586 else if (iTypeId == EVariantTypeTUint) //TUint to real conversion |
|
587 { |
|
588 aValue = iData.iUint; |
|
589 return ETrue; |
|
590 } |
|
591 else if(iTypeId == EVariantTypeTInt64) |
|
592 { |
|
593 aValue = iData.iLong; //Warning : Results in loss of data |
|
594 return ETrue; |
|
595 } |
|
596 return EFalse; |
|
597 } |
|
598 EXPORT_C TBool TLiwVariant::Get(TBool& aValue) const |
|
599 { |
|
600 __TEST_INVARIANT; |
|
601 if (iTypeId == EVariantTypeTBool) |
|
602 { |
|
603 aValue = iData.iBool; |
|
604 return ETrue; |
|
605 } |
|
606 return EFalse; |
|
607 } |
|
608 |
|
609 EXPORT_C TBool TLiwVariant::Get(TUint& aValue) const |
|
610 { |
|
611 __TEST_INVARIANT; |
|
612 if (iTypeId == EVariantTypeTUint) |
|
613 { |
|
614 aValue = iData.iUint; |
|
615 return ETrue; |
|
616 } |
|
617 else if (iTypeId == EVariantTypeTReal) //real to TUint conversion |
|
618 { |
|
619 aValue = static_cast<TUint>(iData.iReal); |
|
620 return ETrue; |
|
621 } |
|
622 else if(iTypeId == EVariantTypeTInt32) // TInt32 to TUint conversion |
|
623 { |
|
624 aValue = static_cast<TUint>(iData.iInt32); |
|
625 return ETrue; |
|
626 } |
|
627 else if (iTypeId == EVariantTypeDesC8) //desc8 to TUint conversion |
|
628 { |
|
629 TLex8 parseString(iPtrC8); |
|
630 TInt err = parseString.Val(aValue); |
|
631 if(err == KErrNone) |
|
632 return ETrue; |
|
633 } |
|
634 else if (iTypeId == EVariantTypeDesC) //desc16 to TUint conversion |
|
635 { |
|
636 TLex16 parseString(iPtrC); |
|
637 TInt err = parseString.Val(aValue); |
|
638 if(err == KErrNone) |
|
639 return ETrue; |
|
640 } |
|
641 else if(iTypeId == EVariantTypeTInt64) // TInt32 to TUint conversion |
|
642 { |
|
643 aValue = static_cast<TUint>(iData.iLong); //Warning : Results in loss of data |
|
644 return ETrue; |
|
645 } |
|
646 return EFalse; |
|
647 } |
|
648 |
|
649 EXPORT_C TBool TLiwVariant::Get(TUid& aValue) const |
|
650 { |
|
651 __TEST_INVARIANT; |
|
652 if (iTypeId == EVariantTypeTUid) |
|
653 { |
|
654 aValue = TUid::Uid(iData.iInt32); |
|
655 return ETrue; |
|
656 } |
|
657 return EFalse; |
|
658 } |
|
659 |
|
660 EXPORT_C TBool TLiwVariant::Get(TPtrC& aValue) const |
|
661 { |
|
662 __TEST_INVARIANT; |
|
663 if (iTypeId == EVariantTypeDesC) |
|
664 { |
|
665 aValue.Set(iPtrC); |
|
666 return ETrue; |
|
667 } |
|
668 return EFalse; |
|
669 } |
|
670 |
|
671 EXPORT_C TBool TLiwVariant::Get(TTime& aValue) const |
|
672 { |
|
673 __TEST_INVARIANT; |
|
674 if (iTypeId == EVariantTypeTTime) |
|
675 { |
|
676 aValue = TTime(iData.iInt64); |
|
677 return ETrue; |
|
678 } |
|
679 return EFalse; |
|
680 } |
|
681 |
|
682 |
|
683 EXPORT_C TBool TLiwVariant::Get(TPtrC8& aValue) const |
|
684 { |
|
685 __TEST_INVARIANT; |
|
686 if (iTypeId == EVariantTypeDesC8) |
|
687 { |
|
688 aValue.Set(iPtrC8); |
|
689 return ETrue; |
|
690 } |
|
691 |
|
692 return EFalse; |
|
693 } |
|
694 |
|
695 EXPORT_C TBool TLiwVariant::Get(TDes& aValue) const |
|
696 { |
|
697 __TEST_INVARIANT; |
|
698 if (iTypeId == EVariantTypeDesC) |
|
699 { |
|
700 aValue.Copy(iPtrC); |
|
701 return ETrue; |
|
702 } |
|
703 else if (iTypeId == EVariantTypeDesC8) |
|
704 { |
|
705 aValue.Copy(iPtrC8); |
|
706 return ETrue; |
|
707 } |
|
708 else if (iTypeId == EVariantTypeTReal) //real to TDes conversion |
|
709 { |
|
710 const TRealFormat realFormat; |
|
711 aValue.Num(iData.iReal,realFormat); |
|
712 return ETrue; |
|
713 } |
|
714 else if(iTypeId == EVariantTypeTInt32) // TInt32 to TDes conversion |
|
715 { |
|
716 aValue.Num(iData.iInt32); |
|
717 return ETrue; |
|
718 } |
|
719 else if(iTypeId == EVariantTypeTUint) // TInt32 to TDes conversion |
|
720 { |
|
721 aValue.Num(iData.iUint,EDecimal); |
|
722 return ETrue; |
|
723 } |
|
724 else if(iTypeId == EVariantTypeTInt64) // TInt32 to TDes conversion |
|
725 { |
|
726 aValue.Num(iData.iLong); |
|
727 return ETrue; |
|
728 } |
|
729 return EFalse; |
|
730 } |
|
731 |
|
732 |
|
733 EXPORT_C TBool TLiwVariant::Get(TDes8& aValue) const |
|
734 { |
|
735 __TEST_INVARIANT; |
|
736 if (iTypeId == EVariantTypeDesC8) |
|
737 { |
|
738 aValue.Copy(iPtrC8); |
|
739 return ETrue; |
|
740 } |
|
741 else if (iTypeId == EVariantTypeDesC) |
|
742 { |
|
743 aValue.Copy(iPtrC); |
|
744 return ETrue; |
|
745 } |
|
746 else if (iTypeId == EVariantTypeTReal) //real to TDes8 conversion |
|
747 { |
|
748 const TRealFormat realFormat; |
|
749 aValue.Num(iData.iReal,realFormat); |
|
750 return ETrue; |
|
751 } |
|
752 else if(iTypeId == EVariantTypeTInt32) // TInt32 to TDes8 conversion |
|
753 { |
|
754 aValue.Num(iData.iInt32); |
|
755 return ETrue; |
|
756 } |
|
757 else if(iTypeId == EVariantTypeTUint) // TInt32 to TDes conversion |
|
758 { |
|
759 aValue.Num(iData.iUint,EDecimal); |
|
760 return ETrue; |
|
761 } |
|
762 else if(iTypeId == EVariantTypeTInt64) // TInt32 to TDes8 conversion |
|
763 { |
|
764 aValue.Num(iData.iLong); |
|
765 return ETrue; |
|
766 } |
|
767 return EFalse; |
|
768 } |
|
769 |
|
770 |
|
771 EXPORT_C TBool TLiwVariant::Get(RFile& aValue) const |
|
772 { |
|
773 __TEST_INVARIANT; |
|
774 if (iTypeId == EVariantTypeFileHandle) |
|
775 { |
|
776 aValue = *((RFile*)&iData.iInt64); |
|
777 return ETrue; |
|
778 } |
|
779 |
|
780 return EFalse; |
|
781 } |
|
782 |
|
783 EXPORT_C TBool TLiwVariant::Get(CLiwList& aValue) const |
|
784 { |
|
785 __TEST_INVARIANT; |
|
786 if (iTypeId == EVariantTypeList) |
|
787 { |
|
788 TInt pos = 0; |
|
789 TLiwVariant tempVarient; |
|
790 tempVarient.PushL(); |
|
791 // Overwrite the list |
|
792 if(0 != aValue.Count()) |
|
793 { |
|
794 // make the list Empty |
|
795 for (pos = aValue.Count() - 1; pos >= 0 ; pos--) |
|
796 aValue.Remove(pos); |
|
797 } |
|
798 // Copy List varient by varient |
|
799 for (pos = 0; pos < iData.iList->Count(); pos++) |
|
800 { |
|
801 iData.iList->AtL(pos, tempVarient); |
|
802 aValue.AppendL(tempVarient); |
|
803 } |
|
804 CleanupStack::Pop(&tempVarient); |
|
805 tempVarient.Reset(); |
|
806 return ETrue; |
|
807 } |
|
808 |
|
809 return EFalse; |
|
810 } |
|
811 |
|
812 EXPORT_C TBool TLiwVariant::Get(CLiwMap& aValue) const |
|
813 { |
|
814 __TEST_INVARIANT; |
|
815 if (iTypeId == EVariantTypeMap) |
|
816 { |
|
817 TInt pos = 0; |
|
818 TLiwVariant tempVarient; |
|
819 tempVarient.PushL(); |
|
820 // Overwrite the Map |
|
821 if(0 != aValue.Count()) |
|
822 { |
|
823 // make the Map Empty |
|
824 for (pos = aValue.Count() - 1; pos >= 0 ; pos--) |
|
825 { |
|
826 TBuf8<KBufSizeSmall> mapKey; |
|
827 aValue.AtL(pos, mapKey); |
|
828 aValue.Remove(mapKey); |
|
829 } |
|
830 } |
|
831 // Copy Map varient by varient |
|
832 for (pos = 0; pos < iData.iMap->Count(); pos++) |
|
833 { |
|
834 TBuf8<KBufSizeSmall> mapKey; |
|
835 iData.iMap->AtL(pos, mapKey); |
|
836 iData.iMap->FindL(mapKey, tempVarient); |
|
837 aValue.InsertL(mapKey, tempVarient); |
|
838 } |
|
839 CleanupStack::Pop(&tempVarient); |
|
840 tempVarient.Reset(); |
|
841 return ETrue; |
|
842 } |
|
843 |
|
844 return EFalse; |
|
845 } |
|
846 EXPORT_C TReal TLiwVariant::AsTReal() const |
|
847 { |
|
848 TReal value = 0; |
|
849 Get(value); |
|
850 return value; |
|
851 } |
|
852 |
|
853 EXPORT_C TInt32 TLiwVariant::AsTInt32() const |
|
854 { |
|
855 TInt32 value = 0; |
|
856 Get(value); |
|
857 return value; |
|
858 } |
|
859 |
|
860 EXPORT_C TInt64 TLiwVariant::AsTInt64() const |
|
861 { |
|
862 TInt64 value = 0; |
|
863 Get(value); |
|
864 return value; |
|
865 } |
|
866 |
|
867 EXPORT_C CLiwBuffer* TLiwVariant::AsBuffer() const |
|
868 { |
|
869 __TEST_INVARIANT; |
|
870 if (iTypeId == EVariantTypeBuffer) |
|
871 { |
|
872 return iData.iBuffer; |
|
873 } |
|
874 |
|
875 return NULL; |
|
876 } |
|
877 |
|
878 EXPORT_C TBool TLiwVariant::AsTBool() const |
|
879 { |
|
880 TBool value = 0; |
|
881 Get(value); |
|
882 return value; |
|
883 } |
|
884 |
|
885 EXPORT_C TBool TLiwVariant::AsTUint() const |
|
886 { |
|
887 TUint value = 0; |
|
888 Get(value); |
|
889 return value; |
|
890 } |
|
891 |
|
892 EXPORT_C TUid TLiwVariant::AsTUid() const |
|
893 { |
|
894 __TEST_INVARIANT; |
|
895 TUid value = {0}; |
|
896 Get(value); |
|
897 return value; |
|
898 } |
|
899 |
|
900 EXPORT_C TPtrC TLiwVariant::AsDes() const |
|
901 { |
|
902 __TEST_INVARIANT; |
|
903 TPtrC value; |
|
904 Get(value); |
|
905 return value; |
|
906 } |
|
907 |
|
908 EXPORT_C TTime TLiwVariant::AsTTime() const |
|
909 { |
|
910 __TEST_INVARIANT; |
|
911 TTime value(Time::NullTTime()); |
|
912 Get(value); |
|
913 return value; |
|
914 } |
|
915 |
|
916 |
|
917 EXPORT_C TPtrC8 TLiwVariant::AsData() const |
|
918 { |
|
919 __TEST_INVARIANT; |
|
920 TPtrC8 value; |
|
921 Get(value); |
|
922 return value; |
|
923 } |
|
924 |
|
925 |
|
926 EXPORT_C RFile TLiwVariant::AsFileHandle() const |
|
927 { |
|
928 __TEST_INVARIANT; |
|
929 RFile value; |
|
930 Get(value); |
|
931 return value; |
|
932 } |
|
933 |
|
934 EXPORT_C const CLiwList* TLiwVariant::AsList() const |
|
935 { |
|
936 __TEST_INVARIANT; |
|
937 return (iTypeId == EVariantTypeList) ? iData.iList : NULL; |
|
938 } |
|
939 |
|
940 EXPORT_C const CLiwMap* TLiwVariant::AsMap() const |
|
941 { |
|
942 __TEST_INVARIANT; |
|
943 return (iTypeId == EVariantTypeMap) ? iData.iMap : NULL; |
|
944 } |
|
945 |
|
946 EXPORT_C MLiwInterface* TLiwVariant::AsInterface() const |
|
947 { |
|
948 __TEST_INVARIANT; |
|
949 return (iTypeId == EVariantTypeInterface) ? iData.iSession : NULL; |
|
950 } |
|
951 |
|
952 EXPORT_C CLiwIterable* TLiwVariant::AsIterable() const |
|
953 { |
|
954 __TEST_INVARIANT; |
|
955 return (iTypeId == EVariantTypeIterable) ? iData.iIterable : NULL; |
|
956 } |
|
957 |
|
958 EXPORT_C void TLiwVariant::Reset() |
|
959 { |
|
960 __TEST_INVARIANT; |
|
961 |
|
962 if (iTypeId == EVariantTypeDesC) |
|
963 { |
|
964 // Delete any owned buffer |
|
965 delete iData.iBufC; |
|
966 iData.iBufC = NULL; |
|
967 } |
|
968 else if (iTypeId == EVariantTypeDesC8) |
|
969 { |
|
970 delete iData.iBufC8; |
|
971 iData.iBufC8 = NULL; |
|
972 } |
|
973 else if (iTypeId == EVariantTypeList) |
|
974 { |
|
975 if (iPtrC8.Compare(KDummy) == 0) |
|
976 { |
|
977 iData.iList->DecRef(); |
|
978 iData.iList = NULL; |
|
979 iPtrC8.Set(TPtrC8()); |
|
980 } |
|
981 } |
|
982 else if (iTypeId == EVariantTypeMap) |
|
983 { |
|
984 if (iPtrC8.Compare(KDummy) == 0) |
|
985 { |
|
986 iData.iMap->DecRef(); |
|
987 iData.iMap = NULL; |
|
988 iPtrC8.Set(TPtrC8()); |
|
989 } |
|
990 } |
|
991 else if (iTypeId == EVariantTypeIterable) |
|
992 { |
|
993 if (iPtrC8.Compare(KDummy) == 0) |
|
994 { |
|
995 iData.iIterable->DecRef(); |
|
996 iData.iIterable = NULL; |
|
997 iPtrC8.Set(TPtrC8()); |
|
998 } |
|
999 } |
|
1000 else if (iTypeId == EVariantTypeBuffer) |
|
1001 { |
|
1002 if (iPtrC8.Compare(KDummy) == 0) |
|
1003 { |
|
1004 iData.iBuffer->DecRef(); |
|
1005 iData.iBuffer = NULL; |
|
1006 iPtrC8.Set(TPtrC8()); |
|
1007 } |
|
1008 } |
|
1009 // No need to clear other data, because Get methods wont't do anything if type |
|
1010 // is Null. |
|
1011 iTypeId = EVariantTypeNull; |
|
1012 |
|
1013 __ASSERT_DEBUG(IsEmpty(), Panic(EPanicPostCond_Reset)); |
|
1014 __TEST_INVARIANT; |
|
1015 } |
|
1016 |
|
1017 EXPORT_C void TLiwVariant::Set(TInt32 aValue) |
|
1018 { |
|
1019 __TEST_INVARIANT; |
|
1020 |
|
1021 Reset(); |
|
1022 iTypeId = EVariantTypeTInt32; |
|
1023 iData.iInt32 = aValue; |
|
1024 |
|
1025 __ASSERT_DEBUG(this->AsTInt32()==aValue, Panic(EPanicPostCond_Set_TInt32)); |
|
1026 __TEST_INVARIANT; |
|
1027 } |
|
1028 |
|
1029 EXPORT_C void TLiwVariant::Set(TInt64 aValue) |
|
1030 { |
|
1031 __TEST_INVARIANT; |
|
1032 |
|
1033 Reset(); |
|
1034 iTypeId = EVariantTypeTInt64; |
|
1035 iData.iLong = aValue; |
|
1036 |
|
1037 __ASSERT_DEBUG(this->AsTInt64()==aValue, Panic(EPanicPostCond_Set_TInt64)); |
|
1038 __TEST_INVARIANT; |
|
1039 } |
|
1040 |
|
1041 EXPORT_C void TLiwVariant::Set(TReal aValue) |
|
1042 { |
|
1043 __TEST_INVARIANT; |
|
1044 |
|
1045 Reset(); |
|
1046 iTypeId = EVariantTypeTReal; |
|
1047 iData.iReal = aValue; |
|
1048 |
|
1049 __ASSERT_DEBUG(this->AsTReal()==aValue, Panic(EPanicPostCond_Set_TReal)); |
|
1050 __TEST_INVARIANT; |
|
1051 } |
|
1052 EXPORT_C void TLiwVariant::Set(const CLiwBuffer* aValue) |
|
1053 { |
|
1054 __TEST_INVARIANT; |
|
1055 |
|
1056 Reset(); |
|
1057 iTypeId = EVariantTypeBuffer; |
|
1058 iData.iBuffer = (CLiwBuffer*)aValue; |
|
1059 |
|
1060 __TEST_INVARIANT; |
|
1061 } |
|
1062 |
|
1063 EXPORT_C void TLiwVariant::Set(TBool aValue) |
|
1064 { |
|
1065 __TEST_INVARIANT; |
|
1066 |
|
1067 Reset(); |
|
1068 iTypeId = EVariantTypeTBool; |
|
1069 iData.iBool = aValue; |
|
1070 |
|
1071 __ASSERT_DEBUG(this->AsTBool()==aValue, Panic(EPanicPostCond_Set_TBool)); |
|
1072 __TEST_INVARIANT; |
|
1073 } |
|
1074 |
|
1075 EXPORT_C void TLiwVariant::Set(TUint aValue) |
|
1076 { |
|
1077 __TEST_INVARIANT; |
|
1078 |
|
1079 Reset(); |
|
1080 iTypeId = EVariantTypeTUint; |
|
1081 iData.iUint = aValue; |
|
1082 |
|
1083 __ASSERT_DEBUG(this->AsTUint()==aValue, Panic(EPanicPostCond_Set_TUint)); |
|
1084 __TEST_INVARIANT; |
|
1085 } |
|
1086 |
|
1087 EXPORT_C void TLiwVariant::Set(const TUid& aValue) |
|
1088 { |
|
1089 __TEST_INVARIANT; |
|
1090 |
|
1091 Reset(); |
|
1092 iTypeId = EVariantTypeTUid; |
|
1093 iData.iInt32 = aValue.iUid; |
|
1094 |
|
1095 __ASSERT_DEBUG(this->AsTUid()==aValue, Panic(EPanicPostCond_Set_TUid)); |
|
1096 __TEST_INVARIANT; |
|
1097 } |
|
1098 |
|
1099 EXPORT_C void TLiwVariant::Set(const TTime& aValue) |
|
1100 { |
|
1101 __TEST_INVARIANT; |
|
1102 |
|
1103 Reset(); |
|
1104 iTypeId = EVariantTypeTTime; |
|
1105 iData.iInt64.Set(aValue.Int64()); |
|
1106 |
|
1107 __ASSERT_DEBUG(this->AsTTime()==aValue, Panic(EPanicPostCond_Set_TTime)); |
|
1108 __TEST_INVARIANT; |
|
1109 } |
|
1110 |
|
1111 EXPORT_C void TLiwVariant::Set(const TDesC& aValue) |
|
1112 { |
|
1113 __TEST_INVARIANT; |
|
1114 |
|
1115 Reset(); |
|
1116 iTypeId = EVariantTypeDesC; |
|
1117 iData.iBufC = NULL; // not owned |
|
1118 iPtrC.Set(aValue); |
|
1119 |
|
1120 __ASSERT_DEBUG(this->AsDes()==aValue, Panic(EPanicPostCond_Set_TDesC)); |
|
1121 __TEST_INVARIANT; |
|
1122 } |
|
1123 |
|
1124 |
|
1125 |
|
1126 EXPORT_C void TLiwVariant::Set(const TDesC8& aValue) |
|
1127 { |
|
1128 __TEST_INVARIANT; |
|
1129 |
|
1130 Reset(); |
|
1131 iTypeId = EVariantTypeDesC8; |
|
1132 iData.iBufC8 = NULL; // not owned |
|
1133 iPtrC8.Set(aValue); |
|
1134 |
|
1135 __ASSERT_DEBUG(this->AsData()==aValue, Panic(EPanicPostCond_Set_TDesC)); |
|
1136 __TEST_INVARIANT; |
|
1137 } |
|
1138 |
|
1139 |
|
1140 |
|
1141 EXPORT_C void TLiwVariant::Set(const RFile& aValue) |
|
1142 { |
|
1143 __TEST_INVARIANT; |
|
1144 __ASSERT_DEBUG(sizeof(SInt64) == sizeof(RFile), Panic(EPanicPreCond_DataSizeMismatch)); |
|
1145 |
|
1146 Reset(); |
|
1147 iTypeId = EVariantTypeFileHandle; |
|
1148 *((RFile*)&iData.iInt64) = aValue; |
|
1149 |
|
1150 __TEST_INVARIANT; |
|
1151 } |
|
1152 |
|
1153 EXPORT_C void TLiwVariant::Set(const CLiwList* aValue) |
|
1154 { |
|
1155 __TEST_INVARIANT; |
|
1156 |
|
1157 Reset(); |
|
1158 iTypeId = EVariantTypeList; |
|
1159 iData.iList = (CLiwList*)aValue; |
|
1160 |
|
1161 __TEST_INVARIANT; |
|
1162 } |
|
1163 |
|
1164 EXPORT_C void TLiwVariant::Set(const CLiwMap* aValue) |
|
1165 { |
|
1166 __TEST_INVARIANT; |
|
1167 |
|
1168 Reset(); |
|
1169 iTypeId = EVariantTypeMap; |
|
1170 iData.iMap = (CLiwMap*)aValue; |
|
1171 |
|
1172 __TEST_INVARIANT; |
|
1173 } |
|
1174 |
|
1175 EXPORT_C void TLiwVariant::Set(const MLiwInterface* aValue) |
|
1176 { |
|
1177 __TEST_INVARIANT; |
|
1178 |
|
1179 Reset(); |
|
1180 iTypeId = EVariantTypeInterface; |
|
1181 iData.iSession = (MLiwInterface*)aValue; |
|
1182 |
|
1183 __TEST_INVARIANT; |
|
1184 } |
|
1185 |
|
1186 EXPORT_C void TLiwVariant::Set(const CLiwIterable* aValue) |
|
1187 { |
|
1188 __TEST_INVARIANT; |
|
1189 |
|
1190 Reset(); |
|
1191 iTypeId = EVariantTypeIterable; |
|
1192 iData.iIterable = (CLiwIterable*)aValue; |
|
1193 |
|
1194 __TEST_INVARIANT; |
|
1195 } |
|
1196 |
|
1197 EXPORT_C void TLiwVariant::SetL(const TLiwVariant& aValue) |
|
1198 { |
|
1199 __TEST_INVARIANT; |
|
1200 |
|
1201 if (aValue.iTypeId == EVariantTypeDesC) |
|
1202 { |
|
1203 // Take an own copy of the string value |
|
1204 HBufC* buf = aValue.iPtrC.AllocL(); |
|
1205 Reset(); |
|
1206 iTypeId = EVariantTypeDesC; |
|
1207 iData.iBufC = buf; |
|
1208 iPtrC.Set(*iData.iBufC); |
|
1209 } |
|
1210 else if (aValue.iTypeId == EVariantTypeDesC8) |
|
1211 { |
|
1212 // Take an own copy of the data |
|
1213 HBufC8* buf = aValue.iPtrC8.AllocL(); |
|
1214 Reset(); |
|
1215 iTypeId = EVariantTypeDesC8; |
|
1216 iData.iBufC8 = buf; |
|
1217 iPtrC8.Set(*iData.iBufC8); |
|
1218 } |
|
1219 else if (aValue.iTypeId == EVariantTypeList) |
|
1220 { |
|
1221 // Take an own copy of the data by increasing the reference count |
|
1222 Reset(); |
|
1223 iTypeId = EVariantTypeList; |
|
1224 iData.iList = aValue.iData.iList; |
|
1225 iData.iList->IncRef(); |
|
1226 iPtrC8.Set(KDummy); // hack: mark owned |
|
1227 } |
|
1228 else if (aValue.iTypeId == EVariantTypeMap) |
|
1229 { |
|
1230 // Take an own copy of the data by increasing the reference count |
|
1231 Reset(); |
|
1232 iTypeId = EVariantTypeMap; |
|
1233 iData.iMap = aValue.iData.iMap; |
|
1234 iData.iMap->IncRef(); |
|
1235 iPtrC8.Set(KDummy); // hack: mark owned |
|
1236 } |
|
1237 else if (aValue.iTypeId == EVariantTypeIterable) |
|
1238 { |
|
1239 // Take an own copy of the data by increasing the reference count |
|
1240 Reset(); |
|
1241 iTypeId = EVariantTypeIterable; |
|
1242 iData.iIterable = aValue.iData.iIterable; |
|
1243 iData.iIterable->IncRef(); |
|
1244 iPtrC8.Set(KDummy); // hack: mark owned |
|
1245 } |
|
1246 else if (aValue.iTypeId == EVariantTypeBuffer) |
|
1247 { |
|
1248 Reset(); |
|
1249 iTypeId = EVariantTypeBuffer; |
|
1250 iData.iBuffer = aValue.iData.iBuffer; |
|
1251 iData.iBuffer->IncRef(); |
|
1252 iPtrC8.Set(KDummy); // hack: mark owned |
|
1253 } |
|
1254 else |
|
1255 { |
|
1256 Reset(); |
|
1257 iTypeId = aValue.iTypeId; |
|
1258 // Copy the data union as one block |
|
1259 iData = aValue.iData; |
|
1260 } |
|
1261 |
|
1262 //__ASSERT_DEBUG(*this == aValue, Panic(EPanicPostCond_CopyL)); |
|
1263 __TEST_INVARIANT; |
|
1264 } |
|
1265 |
|
1266 void TLiwVariant::Destroy() |
|
1267 { |
|
1268 __TEST_INVARIANT; |
|
1269 |
|
1270 if (iTypeId == EVariantTypeDesC) |
|
1271 { |
|
1272 // Delete any owned buffer |
|
1273 delete iData.iBufC; |
|
1274 iData.iBufC = NULL; |
|
1275 } |
|
1276 else if (iTypeId == EVariantTypeDesC8) |
|
1277 { |
|
1278 delete iData.iBufC8; |
|
1279 iData.iBufC8 = NULL; |
|
1280 } |
|
1281 else if (iTypeId == EVariantTypeList) |
|
1282 { |
|
1283 if (iPtrC8.Compare(KDummy) == 0) |
|
1284 { |
|
1285 iData.iList->DecRef(); |
|
1286 iData.iList = NULL; |
|
1287 iPtrC8.Set(TPtrC8()); |
|
1288 } |
|
1289 } |
|
1290 else if (iTypeId == EVariantTypeMap) |
|
1291 { |
|
1292 if (iPtrC8.Compare(KDummy) == 0) |
|
1293 { |
|
1294 iData.iMap->DecRef(); |
|
1295 iData.iMap = NULL; |
|
1296 iPtrC8.Set(TPtrC8()); |
|
1297 } |
|
1298 } |
|
1299 else if (iTypeId == EVariantTypeIterable) |
|
1300 { |
|
1301 if (iPtrC8.Compare(KDummy) == 0) |
|
1302 { |
|
1303 iData.iIterable->DecRef(); |
|
1304 iData.iIterable = NULL; |
|
1305 iPtrC8.Set(TPtrC8()); |
|
1306 } |
|
1307 } |
|
1308 else if (iTypeId == EVariantTypeBuffer) |
|
1309 { |
|
1310 if (iPtrC8.Compare(KDummy) == 0) |
|
1311 { |
|
1312 iData.iBuffer->DecRef(); |
|
1313 iData.iBuffer = NULL; |
|
1314 iPtrC8.Set(TPtrC8()); |
|
1315 } |
|
1316 } |
|
1317 } |
|
1318 |
|
1319 void TLiwVariant::InternalizeL(RReadStream& aStream) |
|
1320 { |
|
1321 __TEST_INVARIANT; |
|
1322 |
|
1323 aStream.ReadInt8L(); // version |
|
1324 // if older version adapt to changes (who knows if |
|
1325 // parameters would be also persistent...) |
|
1326 |
|
1327 const TUint8 typeId = aStream.ReadUint8L(); |
|
1328 switch (typeId) |
|
1329 { |
|
1330 case EVariantTypeNull: |
|
1331 { |
|
1332 Reset(); |
|
1333 break; |
|
1334 } |
|
1335 case EVariantTypeTInt32: // FALLTHROUGH |
|
1336 case EVariantTypeTUid: |
|
1337 case EVariantTypeTBool: |
|
1338 case EVariantTypeTUint: |
|
1339 { |
|
1340 TInt32 value = aStream.ReadInt32L(); |
|
1341 Reset(); |
|
1342 iTypeId = typeId; |
|
1343 iData.iInt32 = value; |
|
1344 break; |
|
1345 } |
|
1346 case EVariantTypeTInt64: |
|
1347 { |
|
1348 TInt64 value = aStream.ReadReal64L(); |
|
1349 Reset(); |
|
1350 iTypeId = typeId; |
|
1351 iData.iLong = value; |
|
1352 break; |
|
1353 } |
|
1354 case EVariantTypeFileHandle: // FALLTHROUGH |
|
1355 case EVariantTypeTTime: |
|
1356 case EVariantTypeTReal: |
|
1357 { |
|
1358 SInt64 value; |
|
1359 value.InternalizeL(aStream); |
|
1360 Reset(); |
|
1361 iTypeId = typeId; |
|
1362 iData.iInt64 = value; |
|
1363 break; |
|
1364 } |
|
1365 case EVariantTypeDesC: |
|
1366 { |
|
1367 const TInt len = aStream.ReadInt32L(); |
|
1368 HBufC* buf = HBufC::NewL(aStream,len); |
|
1369 Reset(); |
|
1370 iTypeId = typeId; |
|
1371 iData.iBufC = buf; |
|
1372 iPtrC.Set(*iData.iBufC); |
|
1373 break; |
|
1374 } |
|
1375 case EVariantTypeDesC8: |
|
1376 { |
|
1377 const TInt len = aStream.ReadInt32L(); |
|
1378 HBufC8* buf = HBufC8::NewL(aStream,len); |
|
1379 Reset(); |
|
1380 iTypeId = typeId; |
|
1381 iData.iBufC8 = buf; |
|
1382 iPtrC8.Set(*iData.iBufC8); |
|
1383 break; |
|
1384 } |
|
1385 case EVariantTypeList: |
|
1386 { |
|
1387 CLiwList* list = CLiwDefaultList::NewLC(aStream); |
|
1388 Reset(); |
|
1389 iTypeId = EVariantTypeList; |
|
1390 iData.iList = list; |
|
1391 iPtrC8.Set(KDummy); // hack: mark owned |
|
1392 CleanupStack::Pop(list); // list |
|
1393 break; |
|
1394 } |
|
1395 case EVariantTypeMap: |
|
1396 { |
|
1397 CLiwMap* map = CLiwDefaultMap::NewLC(aStream); |
|
1398 Reset(); |
|
1399 iTypeId = EVariantTypeMap; |
|
1400 iData.iMap = map; |
|
1401 iPtrC8.Set(KDummy); // hack: mark owned |
|
1402 CleanupStack::Pop(map); // map |
|
1403 break; |
|
1404 } |
|
1405 default: |
|
1406 { |
|
1407 // Corrupted data stream. |
|
1408 #ifdef _DEBUG |
|
1409 RDebug::Print(_L("***ERROR TLiwVariant::InternalizeL")); |
|
1410 #endif |
|
1411 User::Leave(KErrCorrupt); |
|
1412 return; |
|
1413 } |
|
1414 } |
|
1415 |
|
1416 __TEST_INVARIANT; |
|
1417 } |
|
1418 |
|
1419 void TLiwVariant::ExternalizeL(RWriteStream& aStream) const |
|
1420 { |
|
1421 __TEST_INVARIANT; |
|
1422 |
|
1423 aStream.WriteInt8L(KVersion); |
|
1424 // if older version adapt to changes (who knows if parameters would be also persistent...) |
|
1425 |
|
1426 aStream.WriteUint8L(iTypeId); |
|
1427 switch (iTypeId) |
|
1428 { |
|
1429 case EVariantTypeTInt32: // FALLTHROUGH |
|
1430 case EVariantTypeTUid: |
|
1431 case EVariantTypeTBool: |
|
1432 case EVariantTypeTUint: |
|
1433 { |
|
1434 aStream.WriteInt32L(iData.iInt32); |
|
1435 break; |
|
1436 } |
|
1437 case EVariantTypeTInt64: |
|
1438 { |
|
1439 aStream.WriteReal64L(iData.iLong); |
|
1440 break; |
|
1441 } |
|
1442 case EVariantTypeFileHandle: // FALLTHROUGH |
|
1443 case EVariantTypeTTime: |
|
1444 case EVariantTypeTReal: |
|
1445 { |
|
1446 iData.iInt64.ExternalizeL(aStream); |
|
1447 break; |
|
1448 } |
|
1449 case EVariantTypeDesC: |
|
1450 { |
|
1451 aStream.WriteInt32L(iPtrC.Length()); |
|
1452 aStream << iPtrC; |
|
1453 break; |
|
1454 } |
|
1455 case EVariantTypeDesC8: |
|
1456 { |
|
1457 aStream.WriteInt32L(iPtrC8.Length()); |
|
1458 aStream << iPtrC8; |
|
1459 break; |
|
1460 } |
|
1461 case EVariantTypeList: |
|
1462 { |
|
1463 iData.iList->ExternalizeL(aStream); |
|
1464 break; |
|
1465 } |
|
1466 case EVariantTypeMap: |
|
1467 { |
|
1468 iData.iMap->ExternalizeL(aStream); |
|
1469 break; |
|
1470 } |
|
1471 case EVariantTypeInterface: |
|
1472 { |
|
1473 _LIT(KMsg, "TLiwVariant: type Interface not externizable"); |
|
1474 User::Panic(KMsg, 1); |
|
1475 break; |
|
1476 } |
|
1477 default: |
|
1478 break; |
|
1479 } |
|
1480 |
|
1481 __TEST_INVARIANT; |
|
1482 } |
|
1483 |
|
1484 TInt TLiwVariant::Size() const |
|
1485 { |
|
1486 __TEST_INVARIANT; |
|
1487 |
|
1488 TInt size = sizeof (TInt8); // version |
|
1489 size += sizeof (TUint8); // iTypeId |
|
1490 switch (iTypeId) |
|
1491 { |
|
1492 case EVariantTypeTInt32: // FALLTHROUGH |
|
1493 case EVariantTypeTUid: |
|
1494 case EVariantTypeTBool: |
|
1495 case EVariantTypeTUint: |
|
1496 { |
|
1497 size += sizeof (TInt32); |
|
1498 break; |
|
1499 } |
|
1500 case EVariantTypeFileHandle: // FALLTHROUGH |
|
1501 case EVariantTypeTTime: |
|
1502 case EVariantTypeTReal: |
|
1503 case EVariantTypeTInt64: |
|
1504 { |
|
1505 size += sizeof (TInt64); |
|
1506 break; |
|
1507 } |
|
1508 case EVariantTypeDesC: |
|
1509 { |
|
1510 size += sizeof (TInt32); // length |
|
1511 //size += 1; // the externalization method used adds a header byte |
|
1512 if(iPtrC.Size() >= KBufSizeLarge) |
|
1513 { |
|
1514 size += 4; |
|
1515 } |
|
1516 else if(iPtrC.Size() >= KBufSizeSmall) |
|
1517 { |
|
1518 size += 2; |
|
1519 } |
|
1520 else |
|
1521 { |
|
1522 size += 1; |
|
1523 } |
|
1524 size += iPtrC.Size(); |
|
1525 size += 1; // extra one byte for Unicode marker |
|
1526 break; |
|
1527 } |
|
1528 case EVariantTypeDesC8: |
|
1529 { |
|
1530 size += sizeof (TInt32); // length |
|
1531 //size += 1; // the externalization method used adds a header byte |
|
1532 if(iPtrC8.Size() >= KBufSizeLarge) |
|
1533 { |
|
1534 size += 4; |
|
1535 } |
|
1536 |
|
1537 else if(iPtrC8.Size() >= KBufSizeSmall) |
|
1538 { |
|
1539 size += 2; |
|
1540 } |
|
1541 else |
|
1542 { |
|
1543 size += 1; |
|
1544 } |
|
1545 size += iPtrC8.Size(); |
|
1546 break; |
|
1547 } |
|
1548 case EVariantTypeList: |
|
1549 { |
|
1550 size += iData.iList->Size(); |
|
1551 break; |
|
1552 } |
|
1553 case EVariantTypeMap: |
|
1554 { |
|
1555 size += iData.iMap->Size(); |
|
1556 break; |
|
1557 } |
|
1558 default: |
|
1559 break; |
|
1560 } |
|
1561 return size; |
|
1562 } |
|
1563 |
|
1564 // ============================ EXTERNAL FUNCTIONS =============================== |
|
1565 |
|
1566 EXPORT_C TBool operator==(const TLiwVariant& aLhs, const TLiwVariant& aRhs) |
|
1567 { |
|
1568 if (aLhs.iTypeId == aRhs.iTypeId) |
|
1569 { |
|
1570 switch (aLhs.iTypeId) |
|
1571 { |
|
1572 case EVariantTypeNull: |
|
1573 { |
|
1574 // Null equals Null |
|
1575 return ETrue; |
|
1576 } |
|
1577 case EVariantTypeTInt32: // FALLTHROUGH |
|
1578 case EVariantTypeTUid: |
|
1579 case EVariantTypeTBool: |
|
1580 case EVariantTypeTUint: |
|
1581 { |
|
1582 return (aLhs.iData.iInt32 == aRhs.iData.iInt32); |
|
1583 } |
|
1584 case EVariantTypeTInt64: |
|
1585 { |
|
1586 return (aLhs.iData.iLong == aRhs.iData.iLong); |
|
1587 } |
|
1588 case EVariantTypeDesC: |
|
1589 { |
|
1590 return (aLhs.iPtrC == aRhs.iPtrC); |
|
1591 } |
|
1592 case EVariantTypeDesC8: |
|
1593 { |
|
1594 return (aLhs.iPtrC8 == aRhs.iPtrC8); |
|
1595 } |
|
1596 case EVariantTypeFileHandle: // FALLTHROUGH |
|
1597 case EVariantTypeTTime: |
|
1598 case EVariantTypeTReal: |
|
1599 { |
|
1600 return (aLhs.iData.iInt64 == aRhs.iData.iInt64); |
|
1601 } |
|
1602 case EVariantTypeList: |
|
1603 { |
|
1604 return (*(aLhs.iData.iList) == *(aRhs.iData.iList)); |
|
1605 } |
|
1606 case EVariantTypeMap: |
|
1607 { |
|
1608 return (*(aLhs.iData.iMap) == *(aRhs.iData.iMap)); |
|
1609 } |
|
1610 case EVariantTypeInterface: |
|
1611 { |
|
1612 return (aLhs.iData.iSession == aRhs.iData.iSession); |
|
1613 } |
|
1614 case EVariantTypeIterable: |
|
1615 { |
|
1616 return (*(aLhs.iData.iIterable) == *(aRhs.iData.iIterable)); |
|
1617 } |
|
1618 case EVariantTypeBuffer: |
|
1619 { |
|
1620 return (aLhs.iData.iBuffer == aRhs.iData.iBuffer); |
|
1621 } |
|
1622 default: |
|
1623 { |
|
1624 break; |
|
1625 } |
|
1626 } |
|
1627 } |
|
1628 return EFalse; |
|
1629 } |
|
1630 |
|
1631 #ifdef _DEBUG |
|
1632 EXPORT_C void Dump(const TLiwVariant& aVariant) |
|
1633 { |
|
1634 switch (aVariant.TypeId()) |
|
1635 { |
|
1636 case EVariantTypeNull: |
|
1637 { |
|
1638 RDebug::Print(_L(" TLiwVariant::Dump = Null")); |
|
1639 break; |
|
1640 } |
|
1641 case EVariantTypeTInt32: |
|
1642 { |
|
1643 RDebug::Print(_L(" TLiwVariant::Dump(TInt32) = %d"), aVariant.AsTInt32()); |
|
1644 break; |
|
1645 } |
|
1646 case EVariantTypeTInt64: |
|
1647 { |
|
1648 RDebug::Print(_L(" TLiwVariant::Dump(TInt64) = %d"), aVariant.AsTInt64()); |
|
1649 break; |
|
1650 } |
|
1651 case EVariantTypeTReal: |
|
1652 { |
|
1653 RDebug::Print(_L(" TLiwVariant::Dump(TReal) = %d"), aVariant.AsTReal()); |
|
1654 break; |
|
1655 } |
|
1656 case EVariantTypeTBool: |
|
1657 { |
|
1658 RDebug::Print(_L(" TLiwVariant::Dump(TBool) = %d"), aVariant.AsTBool()); |
|
1659 break; |
|
1660 } |
|
1661 case EVariantTypeTUint: |
|
1662 { |
|
1663 RDebug::Print(_L(" TLiwVariant::Dump(TUint) = %d"), aVariant.AsTUint()); |
|
1664 break; |
|
1665 } |
|
1666 |
|
1667 case EVariantTypeTUid: |
|
1668 { |
|
1669 const TUidName& uidName = aVariant.AsTUid().Name(); |
|
1670 RDebug::Print(_L(" TLiwVariant::Dump(TUid) = %S"), &uidName); |
|
1671 break; |
|
1672 } |
|
1673 case EVariantTypeDesC: |
|
1674 { |
|
1675 TPtrC des = aVariant.AsDes(); |
|
1676 RDebug::Print(_L(" TLiwVariant::Dump(TBufC) = %S"), &des); |
|
1677 break; |
|
1678 } |
|
1679 case EVariantTypeDesC8: |
|
1680 { |
|
1681 TPtrC8 des = aVariant.AsData(); |
|
1682 RDebug::Print(_L(" TLiwVariant::Dump(TBufC8) = %S"), &des); |
|
1683 break; |
|
1684 } |
|
1685 case EVariantTypeTTime: |
|
1686 { |
|
1687 TDateTime dt = aVariant.AsTTime().DateTime(); |
|
1688 RDebug::Print(_L(" TLiwVariant::Dump(TTime): day=%d,mon=%d,year=%d,hh=%d,mm=%d,ss=%d"), |
|
1689 dt.Day()+1, dt.Month()+1, dt.Year(), |
|
1690 dt.Hour(),dt.Minute(), dt.Second()); |
|
1691 break; |
|
1692 } |
|
1693 case EVariantTypeFileHandle: |
|
1694 { |
|
1695 RDebug::Print(_L(" TLiwVariant::Dump(RFile): Value is file handle.")); |
|
1696 break; |
|
1697 } |
|
1698 case EVariantTypeList: |
|
1699 { |
|
1700 const CLiwList* tempList = aVariant.AsList(); |
|
1701 TInt count = tempList->Count(); |
|
1702 TLiwVariant tempVariant; |
|
1703 tempVariant.PushL(); |
|
1704 RDebug::Print(_L(" TLiwVariant::Dump(List[%d]):"), count); |
|
1705 for (TInt index = 0; index < count; index++) |
|
1706 { |
|
1707 TRAPD(error, {tempList->AtL(index, tempVariant);}); |
|
1708 if(error != KErrNone) |
|
1709 RDebug::Print(_L("TLiwVariant::Dump ERROR: %d"), error); |
|
1710 Dump(tempVariant); |
|
1711 } |
|
1712 CleanupStack::Pop(&tempVariant); |
|
1713 tempVariant.Reset(); |
|
1714 RDebug::Print(_L(" TLiwVariant::Dump List END")); |
|
1715 break; |
|
1716 } |
|
1717 case EVariantTypeIterable: |
|
1718 { |
|
1719 CLiwIterable* tempItr = aVariant.AsIterable(); |
|
1720 TLiwVariant tempVariant; |
|
1721 RDebug::Print(_L(" TLiwVariant::Dump(Iterable):")); |
|
1722 tempItr->Reset(); |
|
1723 TRAPD(errno, { |
|
1724 while (tempItr->NextL(tempVariant)) { |
|
1725 Dump(tempVariant); |
|
1726 } |
|
1727 }); |
|
1728 if(errno != KErrNone) |
|
1729 RDebug::Print(_L("TLiwVariant::Dump ERROR: %d"), errno); |
|
1730 tempVariant.Reset(); |
|
1731 RDebug::Print(_L(" TLiwVariant::Dump Iterable END")); |
|
1732 break; |
|
1733 } |
|
1734 case EVariantTypeMap: |
|
1735 { |
|
1736 const CLiwMap* tempMap = aVariant.AsMap(); |
|
1737 TInt count = tempMap->Count(); |
|
1738 TLiwVariant tempVariant; |
|
1739 RDebug::Print(_L(" TLiwVariant::Dump(Map):")); |
|
1740 for (TInt index = 0; index < count; index++) { |
|
1741 TBuf8<KBufSizeMid> key; |
|
1742 TRAPD(error, {tempMap->AtL(index, key);}); |
|
1743 RDebug::Print(_L(" key=%S, value= "), &key); |
|
1744 TRAP(error, {tempMap->FindL(key, tempVariant);}); |
|
1745 Dump(tempVariant); |
|
1746 } |
|
1747 tempVariant.Reset(); |
|
1748 RDebug::Print(_L(" TLiwVariant::Dump Map END")); |
|
1749 break; |
|
1750 } |
|
1751 case EVariantTypeInterface: |
|
1752 { |
|
1753 RDebug::Print(_L(" TLiwVariant::Dump(Interface): Value is interface pointer.")); |
|
1754 break; |
|
1755 } |
|
1756 default: |
|
1757 { |
|
1758 RDebug::Print(_L(" *** TLiwVariant::Dump(Unknown) ***")); |
|
1759 break; |
|
1760 } |
|
1761 } |
|
1762 } |
|
1763 #else |
|
1764 EXPORT_C void Dump(const TLiwVariant& /*aVariant*/) |
|
1765 { |
|
1766 } |
|
1767 |
|
1768 #endif // ifdef _DEBUG |
|