|
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 "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 TAiwGenericParam and CAiwGenericParamList. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 // INCLUDES |
|
23 #include <s32mem.h> |
|
24 #include "AiwGenericParam.h" |
|
25 |
|
26 namespace { |
|
27 |
|
28 // CONSTANTS |
|
29 // Protect against possible data format changes |
|
30 const TInt KVersion = 10; // Version 1.0 |
|
31 |
|
32 enum TPanicCode |
|
33 { |
|
34 EPanicPostCond_CopyLC = 1, |
|
35 EPanicPostCond_AppendL, |
|
36 EPanicPostCond_Reset |
|
37 }; |
|
38 |
|
39 |
|
40 // ============================= LOCAL FUNCTIONS =============================== |
|
41 |
|
42 #ifdef _DEBUG |
|
43 void Panic(TPanicCode aCode) |
|
44 { |
|
45 _LIT(KPanicText, "GenericParam"); |
|
46 User::Panic(KPanicText, aCode); |
|
47 } |
|
48 #endif |
|
49 |
|
50 } |
|
51 |
|
52 |
|
53 const TInt KRFsSlot = 2; |
|
54 const TInt KRFileSlot = 3; |
|
55 |
|
56 |
|
57 // ============================ MEMBER FUNCTIONS =============================== |
|
58 |
|
59 /** |
|
60 * Releases any dynamic data owned by this parameter. |
|
61 */ |
|
62 inline void TAiwGenericParam::Destroy() |
|
63 { |
|
64 iValue.Destroy(); |
|
65 } |
|
66 |
|
67 /** |
|
68 * Copies aParam to this parameter. Takes an own copy of the value held in |
|
69 * aParam. |
|
70 */ |
|
71 void TAiwGenericParam::CopyLC(const TAiwGenericParam& aParam) |
|
72 { |
|
73 // Take a copy of value |
|
74 iValue.SetL(aParam.iValue); |
|
75 // Copy semantic id |
|
76 iSemanticId = aParam.iSemanticId; |
|
77 CleanupStack::PushL(*this); |
|
78 |
|
79 __ASSERT_DEBUG(*this==aParam, Panic(EPanicPostCond_CopyLC)); |
|
80 } |
|
81 |
|
82 /** |
|
83 * Returns a cleanup item that will call Destroy to this object when |
|
84 * PopAndDestroy'ed. |
|
85 */ |
|
86 TAiwGenericParam::operator TCleanupItem() |
|
87 { |
|
88 return TCleanupItem(&CleanupDestroy,this); |
|
89 } |
|
90 |
|
91 /** |
|
92 * Callback for operator TCleanupItem(). |
|
93 */ |
|
94 void TAiwGenericParam::CleanupDestroy(TAny* aObj) |
|
95 { |
|
96 static_cast<TAiwGenericParam*>(aObj)->Destroy(); |
|
97 } |
|
98 |
|
99 /** |
|
100 * Internalizes this parameter from aStream. |
|
101 */ |
|
102 void TAiwGenericParam::InternalizeL(RReadStream& aStream) |
|
103 { |
|
104 /*const TInt version =*/ aStream.ReadInt8L(); |
|
105 const TGenericParamId id = |
|
106 static_cast<TGenericParamId>(aStream.ReadInt32L()); |
|
107 iValue.InternalizeL(aStream); |
|
108 iSemanticId = id; |
|
109 } |
|
110 |
|
111 /** |
|
112 * Externalizes this parameter to aStream. |
|
113 */ |
|
114 void TAiwGenericParam::ExternalizeL(RWriteStream& aStream) const |
|
115 { |
|
116 aStream.WriteInt8L(KVersion); |
|
117 aStream.WriteInt32L(iSemanticId); |
|
118 iValue.ExternalizeL(aStream); |
|
119 } |
|
120 |
|
121 /** |
|
122 * Returns the maximum externalized size of this parameter. |
|
123 */ |
|
124 TInt TAiwGenericParam::Size() const |
|
125 { |
|
126 TInt size = sizeof (TInt8); // version |
|
127 size += sizeof (TInt32); // semantic ID |
|
128 size += iValue.Size(); |
|
129 return size; |
|
130 } |
|
131 |
|
132 EXPORT_C TBool operator==(const TAiwGenericParam& aLhs, |
|
133 const TAiwGenericParam& aRhs) |
|
134 { |
|
135 return |
|
136 ((aLhs.SemanticId() == aRhs.SemanticId()) && |
|
137 (aLhs.Value() == aRhs.Value())); |
|
138 } |
|
139 |
|
140 |
|
141 //////////////////////////////////////////////////////////// |
|
142 // CAiwGenericParamList |
|
143 |
|
144 inline CAiwGenericParamList::CAiwGenericParamList() |
|
145 { |
|
146 } |
|
147 |
|
148 EXPORT_C CAiwGenericParamList* CAiwGenericParamList::NewL() |
|
149 { |
|
150 CAiwGenericParamList *self = NewLC(); |
|
151 CleanupStack::Pop(); |
|
152 return self; |
|
153 } |
|
154 |
|
155 EXPORT_C CAiwGenericParamList* CAiwGenericParamList::NewL(RReadStream& aReadStream) |
|
156 { |
|
157 CAiwGenericParamList *self = NewLC(aReadStream); |
|
158 CleanupStack::Pop(); |
|
159 return self; |
|
160 } |
|
161 |
|
162 EXPORT_C CAiwGenericParamList* CAiwGenericParamList::NewLC() |
|
163 { |
|
164 CAiwGenericParamList *self = new( ELeave ) CAiwGenericParamList(); |
|
165 CleanupStack::PushL( self ); |
|
166 return self; |
|
167 } |
|
168 |
|
169 EXPORT_C CAiwGenericParamList* CAiwGenericParamList::NewLC(RReadStream& aReadStream) |
|
170 { |
|
171 CAiwGenericParamList *self = new( ELeave ) CAiwGenericParamList(); |
|
172 CleanupStack::PushL( self ); |
|
173 self->AppendL(aReadStream); |
|
174 return self; |
|
175 } |
|
176 |
|
177 CAiwGenericParamList::~CAiwGenericParamList() |
|
178 { |
|
179 for (TInt i = iParameters.Count()-1; i >= 0; --i) |
|
180 { |
|
181 // Can use Destroy because the parameters cannot be accessed anymore |
|
182 iParameters[i].Destroy(); |
|
183 } |
|
184 iParameters.Close(); |
|
185 } |
|
186 |
|
187 EXPORT_C TInt CAiwGenericParamList::Count() const |
|
188 { |
|
189 return iParameters.Count(); |
|
190 } |
|
191 |
|
192 EXPORT_C TInt CAiwGenericParamList::Count(TGenericParamId aSemanticId, |
|
193 TVariantTypeId aDataType) const |
|
194 { |
|
195 TInt result = 0; |
|
196 const TInt count = iParameters.Count(); |
|
197 |
|
198 for (TInt i = 0; i < count; i++) |
|
199 { |
|
200 const TAiwGenericParam& param = iParameters[i]; |
|
201 if (param.SemanticId() == aSemanticId) |
|
202 { |
|
203 if (aDataType==EVariantTypeAny || param.Value().TypeId()==aDataType) |
|
204 { |
|
205 result++; |
|
206 } |
|
207 } |
|
208 } |
|
209 return result; |
|
210 } |
|
211 |
|
212 |
|
213 EXPORT_C const TAiwGenericParam& CAiwGenericParamList::operator[](TInt aIndex) const |
|
214 { |
|
215 return iParameters[aIndex]; |
|
216 } |
|
217 |
|
218 EXPORT_C void CAiwGenericParamList::AppendL(const TAiwGenericParam& aParam) |
|
219 { |
|
220 TAiwGenericParam newParam; |
|
221 newParam.CopyLC(aParam); |
|
222 User::LeaveIfError(iParameters.Append(newParam)); |
|
223 CleanupStack::Pop(&newParam); |
|
224 |
|
225 __ASSERT_DEBUG((*this)[Count()-1]==aParam, Panic(EPanicPostCond_AppendL)); |
|
226 } |
|
227 |
|
228 |
|
229 EXPORT_C TBool CAiwGenericParamList::Remove(TInt aSemanticId) |
|
230 { |
|
231 const TInt count = iParameters.Count(); |
|
232 for (TInt i = 0; i < count; ++i) |
|
233 { |
|
234 const TAiwGenericParam& param = iParameters[i]; |
|
235 if (param.SemanticId() == aSemanticId) |
|
236 { |
|
237 iParameters.Remove(i); |
|
238 return ETrue; |
|
239 } |
|
240 } |
|
241 |
|
242 return EFalse; |
|
243 } |
|
244 |
|
245 |
|
246 EXPORT_C void CAiwGenericParamList::Reset() |
|
247 { |
|
248 for (TInt i = iParameters.Count()-1; i >= 0; --i) |
|
249 { |
|
250 // Can use Destroy because the parameters cannot be accessed anymore |
|
251 iParameters[i].Destroy(); |
|
252 iParameters.Remove(i); |
|
253 } |
|
254 |
|
255 __ASSERT_DEBUG(Count()==0, Panic(EPanicPostCond_Reset)); |
|
256 } |
|
257 |
|
258 EXPORT_C const TAiwGenericParam* CAiwGenericParamList::FindFirst( |
|
259 TInt &aIndex, |
|
260 TGenericParamId aSemanticId, |
|
261 TVariantTypeId aDataType) const |
|
262 { |
|
263 if (aIndex >= 0) |
|
264 { |
|
265 const TInt count = iParameters.Count(); |
|
266 for (TInt i = aIndex; i < count; ++i) |
|
267 { |
|
268 const TAiwGenericParam& param = iParameters[i]; |
|
269 if (param.SemanticId() == aSemanticId) |
|
270 { |
|
271 if (aDataType==EVariantTypeAny || param.Value().TypeId()==aDataType) |
|
272 { |
|
273 aIndex = i; |
|
274 return ¶m; |
|
275 } |
|
276 } |
|
277 } |
|
278 } |
|
279 aIndex = KErrNotFound; |
|
280 return NULL; |
|
281 } |
|
282 |
|
283 EXPORT_C const TAiwGenericParam* CAiwGenericParamList::FindNext( |
|
284 TInt &aIndex, |
|
285 TGenericParamId aSemanticId, |
|
286 TVariantTypeId aDataType) const |
|
287 { |
|
288 if (aIndex < 0) |
|
289 { |
|
290 aIndex = KErrNotFound; |
|
291 return NULL; |
|
292 } |
|
293 |
|
294 TInt index = aIndex+1; |
|
295 const TAiwGenericParam* result = FindFirst(index, aSemanticId, aDataType); |
|
296 aIndex = index; |
|
297 return result; |
|
298 } |
|
299 |
|
300 EXPORT_C void CAiwGenericParamList::ExternalizeL(RWriteStream& aStream) const |
|
301 { |
|
302 aStream.WriteInt8L(KVersion); |
|
303 const TInt count = iParameters.Count(); |
|
304 aStream.WriteInt32L(count); |
|
305 for (TInt i = 0; i < count; ++i) |
|
306 { |
|
307 iParameters[i].ExternalizeL(aStream); |
|
308 } |
|
309 } |
|
310 |
|
311 EXPORT_C TInt CAiwGenericParamList::Size() const |
|
312 { |
|
313 TInt size = sizeof (TInt8); // version |
|
314 size += sizeof (TInt32); // param count |
|
315 const TInt count = iParameters.Count(); |
|
316 for (TInt i = 0; i < count; ++i) |
|
317 { |
|
318 size += iParameters[i].Size(); |
|
319 } |
|
320 return size; |
|
321 } |
|
322 |
|
323 /** |
|
324 * Appends parameters to this list from aStream. |
|
325 */ |
|
326 void CAiwGenericParamList::AppendL(RReadStream& aReadStream) |
|
327 { |
|
328 /*const TInt version =*/ aReadStream.ReadInt8L(); |
|
329 |
|
330 TInt count = aReadStream.ReadInt32L(); |
|
331 while (count--) |
|
332 { |
|
333 TAiwGenericParam param; |
|
334 CleanupStack::PushL(param); |
|
335 param.InternalizeL(aReadStream); |
|
336 User::LeaveIfError(iParameters.Append(param)); |
|
337 CleanupStack::Pop(¶m); |
|
338 } |
|
339 } |
|
340 |
|
341 |
|
342 |
|
343 EXPORT_C void CAiwGenericParamList::InternalizeL(RReadStream& aStream) |
|
344 { |
|
345 AppendL(aStream); |
|
346 } |
|
347 |
|
348 |
|
349 EXPORT_C HBufC8* CAiwGenericParamList::PackForServerL(TIpcArgs& aArgs) |
|
350 { |
|
351 TInt i; |
|
352 TInt rfileCount = 0; |
|
353 RFile fhandle; |
|
354 |
|
355 HBufC8* buf = HBufC8::NewLC(Size() + iParameters.Count()); |
|
356 TPtr8 des = buf->Des(); |
|
357 RDesWriteStream outstrm(des); |
|
358 //outstrm.Open(des); |
|
359 CleanupClosePushL(outstrm); |
|
360 |
|
361 outstrm.WriteInt16L(iParameters.Count()); |
|
362 |
|
363 for (i = 0; i < iParameters.Count(); i++) |
|
364 { |
|
365 if (iParameters[i].Value().TypeId() == EVariantTypeFileHandle) |
|
366 { |
|
367 if (rfileCount == 0) |
|
368 { |
|
369 outstrm.WriteInt8L(iParameters[i].Value().TypeId()); |
|
370 outstrm.WriteInt32L(iParameters[i].SemanticId()); |
|
371 if (iParameters[i].Value().Get(fhandle)) |
|
372 { |
|
373 rfileCount++; |
|
374 } |
|
375 } |
|
376 } |
|
377 else |
|
378 { |
|
379 outstrm.WriteInt8L(iParameters[i].Value().TypeId()); |
|
380 iParameters[i].ExternalizeL( outstrm ); |
|
381 } |
|
382 } |
|
383 |
|
384 outstrm.CommitL(); |
|
385 CleanupStack::PopAndDestroy(&outstrm); |
|
386 |
|
387 aArgs.Set(0, buf); |
|
388 aArgs.Set(1, des.MaxLength()); |
|
389 |
|
390 if (rfileCount) |
|
391 { |
|
392 fhandle.TransferToServer(aArgs, KRFsSlot, KRFileSlot); |
|
393 } |
|
394 |
|
395 CleanupStack::Pop(); // buf |
|
396 |
|
397 return buf; |
|
398 } |
|
399 |
|
400 |
|
401 EXPORT_C void CAiwGenericParamList::UnpackFromClientL(const RMessage2& aMsg) |
|
402 { |
|
403 TInt8 type; |
|
404 TInt i; |
|
405 TAiwGenericParam param; |
|
406 TGenericParamId handleId = EGenericParamUnspecified; |
|
407 TInt numFileHandles = 0; |
|
408 |
|
409 HBufC8* buf = HBufC8::NewLC(aMsg.Int1()); |
|
410 TPtr8 ptr(buf->Des()); |
|
411 aMsg.ReadL(0, ptr); |
|
412 RDesReadStream instrm(buf->Des()); |
|
413 |
|
414 const TInt16 count = instrm.ReadInt16L(); |
|
415 for (i = 0; i < count; i++) |
|
416 { |
|
417 type = instrm.ReadInt8L(); |
|
418 if (type == EVariantTypeFileHandle) |
|
419 { |
|
420 if (numFileHandles == 0) |
|
421 { |
|
422 handleId = (TGenericParamId)instrm.ReadInt32L(); |
|
423 numFileHandles++; |
|
424 } |
|
425 } |
|
426 else |
|
427 { |
|
428 param.InternalizeL(instrm); |
|
429 AppendL(param); |
|
430 param.Reset(); |
|
431 } |
|
432 } |
|
433 |
|
434 if (numFileHandles) |
|
435 { |
|
436 RFile file; |
|
437 file.AdoptFromClient(aMsg, KRFsSlot, KRFileSlot); |
|
438 TAiwVariant variant(file); |
|
439 AppendL(TAiwGenericParam(handleId, variant)); |
|
440 } |
|
441 |
|
442 CleanupStack::PopAndDestroy(); // buf |
|
443 } |
|
444 |
|
445 |
|
446 EXPORT_C void CAiwGenericParamList::AppendL(const CAiwGenericParamList& aList) |
|
447 { |
|
448 for (TInt i = 0; i < aList.iParameters.Count(); i++) |
|
449 { |
|
450 AppendL(aList.iParameters[i]); |
|
451 } |
|
452 } |
|
453 |
|
454 // End of file |