|
1 /* |
|
2 * Copyright (c) 2006 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: Common utility functions |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // ---------------------------------------------------------------------------- |
|
20 // Copies elements from one array of objects of type T to another |
|
21 // ---------------------------------------------------------------------------- |
|
22 // |
|
23 template<typename T> |
|
24 inline void CopyArrayL(const TArray<T>& aSrc, RArray<T>& aDest) |
|
25 { |
|
26 aDest.Reset(); |
|
27 for (TInt i=0; i < aSrc.Count(); ++i) |
|
28 { |
|
29 aDest.AppendL(aSrc[i]); |
|
30 } |
|
31 } |
|
32 |
|
33 // ---------------------------------------------------------------------------- |
|
34 // Copies elements from one array of descriptors to another |
|
35 // ---------------------------------------------------------------------------- |
|
36 // |
|
37 inline void CopyArrayL(const MDesCArray& aSrc, CDesCArray& aDest) |
|
38 { |
|
39 MPXUser::CopyArrayL(aSrc, aDest); |
|
40 } |
|
41 |
|
42 // ---------------------------------------------------------------------------- |
|
43 // Transfer a buffer from server |
|
44 // ---------------------------------------------------------------------------- |
|
45 // |
|
46 inline void TransferBufferFromServerL(const RMPXSession& aSess, |
|
47 TInt aCmd, |
|
48 TInt aSize, |
|
49 CBufBase*& aBuffer) |
|
50 { |
|
51 delete aBuffer; |
|
52 aBuffer = NULL; |
|
53 if (aSize==0) |
|
54 { |
|
55 User::Leave(KErrArgument); |
|
56 } |
|
57 |
|
58 CBufBase* buffer = MPXUser::CreateBufferLC(aSize); |
|
59 TPtr8 ptr(buffer->Ptr(0)); |
|
60 aSess.SendReceiveL(aCmd,TIpcArgs(&ptr)); |
|
61 CleanupStack::Pop(buffer); |
|
62 aBuffer = buffer; |
|
63 } |
|
64 |
|
65 // ---------------------------------------------------------------------------- |
|
66 // Internalize a pointer array from stream |
|
67 // ---------------------------------------------------------------------------- |
|
68 // |
|
69 template<typename T> |
|
70 inline void InternalizeL(RPointerArray<T>& aArray, RReadStream& aStream) |
|
71 { |
|
72 TInt n=aStream.ReadInt32L(); |
|
73 for (TInt i=0;i<n;++i) |
|
74 { |
|
75 T* entry = new (ELeave) T; |
|
76 CleanupStack::PushL(entry); |
|
77 aStream >> *entry; |
|
78 aArray.AppendL(entry); |
|
79 CleanupStack::Pop(entry); |
|
80 } |
|
81 } |
|
82 |
|
83 // ---------------------------------------------------------------------------- |
|
84 // Internalize a pointer array from stream |
|
85 // ---------------------------------------------------------------------------- |
|
86 // |
|
87 template<typename T> |
|
88 inline void InternalizeCObjectArrayL( |
|
89 RPointerArray<T>& aArray, |
|
90 RReadStream& aStream) |
|
91 { |
|
92 TInt n=aStream.ReadInt32L(); |
|
93 for (TInt i=0;i<n;++i) |
|
94 { |
|
95 T* entry = T::NewL(); |
|
96 CleanupStack::PushL(entry); |
|
97 aStream >> *entry; |
|
98 aArray.AppendL(entry); |
|
99 CleanupStack::Pop(entry); |
|
100 } |
|
101 } |
|
102 |
|
103 // ---------------------------------------------------------------------------- |
|
104 // Internalize an array from stream |
|
105 // ---------------------------------------------------------------------------- |
|
106 // |
|
107 template<typename T> |
|
108 inline void InternalizeL(RArray<T>& aArray, RReadStream& aStream) |
|
109 { |
|
110 TInt n=aStream.ReadInt32L(); |
|
111 for (TInt i=0;i<n;++i) |
|
112 { |
|
113 TPckgBuf<T> item; |
|
114 aStream.ReadL(item); |
|
115 aArray.AppendL(item()); |
|
116 } |
|
117 } |
|
118 |
|
119 // ---------------------------------------------------------------------------- |
|
120 // Internalize a descriptor array from stream |
|
121 // ---------------------------------------------------------------------------- |
|
122 // |
|
123 inline void InternalizeL(CDesCArray& aArray, RReadStream& aStream) |
|
124 { |
|
125 TInt n=aStream.ReadInt32L(); |
|
126 for (TInt i=0;i<n;++i) |
|
127 { |
|
128 TInt length=aStream.ReadInt32L(); |
|
129 HBufC* item = HBufC::NewLC(length); |
|
130 TPtr ptr = item->Des(); |
|
131 aStream.ReadL(ptr, length); |
|
132 aArray.AppendL(*item); |
|
133 CleanupStack::PopAndDestroy(item); |
|
134 } |
|
135 } |
|
136 |
|
137 // ---------------------------------------------------------------------------- |
|
138 // Exernalize a pointer array to stream |
|
139 // ---------------------------------------------------------------------------- |
|
140 // |
|
141 template<typename T> |
|
142 inline void ExternalizeL(const TArray<T*>& aArray, RWriteStream& aStream) |
|
143 { |
|
144 TInt len = aArray.Count(); |
|
145 aStream.WriteInt32L(len); |
|
146 for(TInt i = 0;i<len;i++) |
|
147 { |
|
148 T &entry = *aArray[i]; |
|
149 aStream << entry; |
|
150 } |
|
151 } |
|
152 |
|
153 // ---------------------------------------------------------------------------- |
|
154 // Externalize an array to stream |
|
155 // ---------------------------------------------------------------------------- |
|
156 // |
|
157 template<typename T> |
|
158 inline void ExternalizeL(const TArray<T>& aArray, RWriteStream& aStream) |
|
159 { |
|
160 TInt n=aArray.Count(); |
|
161 aStream.WriteInt32L(n); |
|
162 for (TInt i=0;i<n;++i) |
|
163 { |
|
164 TPckgBuf<T> item(aArray[i]); |
|
165 aStream.WriteL(item); |
|
166 } |
|
167 } |
|
168 |
|
169 // ---------------------------------------------------------------------------- |
|
170 // Externalize a descriptor array to stream |
|
171 // ---------------------------------------------------------------------------- |
|
172 // |
|
173 inline void ExternalizeL(const MDesCArray& aArray, RWriteStream& aStream) |
|
174 { |
|
175 TInt n=aArray.MdcaCount(); |
|
176 aStream.WriteInt32L(n); |
|
177 for (TInt i=0;i<n;++i) |
|
178 { |
|
179 TInt length=aArray.MdcaPoint(i).Length(); |
|
180 aStream.WriteInt32L(length); |
|
181 aStream.WriteL(aArray.MdcaPoint(i)); |
|
182 } |
|
183 } |
|
184 |
|
185 // ---------------------------------------------------------------------------- |
|
186 // Helper function to create an array from buf. |
|
187 // ---------------------------------------------------------------------------- |
|
188 // |
|
189 template<typename T> |
|
190 inline void CreateFromBufferL(const CBufBase &aBuf, CArrayFix<T>*& aArray) |
|
191 { |
|
192 delete aArray; |
|
193 aArray = NULL; |
|
194 RBufReadStream rs(aBuf); |
|
195 CleanupClosePushL(rs); |
|
196 CArrayFix<T>* array=new(ELeave)CArrayFixFlat<T>(1); //magic number |
|
197 CleanupStack::PushL(array); |
|
198 rs >> *array; |
|
199 CleanupStack::Pop(array); |
|
200 aArray = array; |
|
201 CleanupStack::PopAndDestroy(&rs); |
|
202 } |
|
203 |
|
204 // ---------------------------------------------------------------------------- |
|
205 // Helper function to create a descriptor array from buf. |
|
206 // ---------------------------------------------------------------------------- |
|
207 // |
|
208 inline void CreateFromBufferL(const CBufBase &aBuf, CDesCArray*& aArray) |
|
209 { |
|
210 delete aArray; |
|
211 aArray = NULL; |
|
212 RBufReadStream rs(aBuf); |
|
213 CleanupClosePushL(rs); |
|
214 CDesCArray* array=new(ELeave)CDesCArrayFlat(1); //magic number |
|
215 CleanupStack::PushL(array); |
|
216 InternalizeL(*array, rs); |
|
217 CleanupStack::Pop(array); |
|
218 aArray = array; |
|
219 CleanupStack::PopAndDestroy(&rs); |
|
220 } |
|
221 |
|
222 // ---------------------------------------------------------------------------- |
|
223 // Helper function to create an array from buf. |
|
224 // ---------------------------------------------------------------------------- |
|
225 // |
|
226 template<typename T> |
|
227 inline void CreateFromBufferL(const CBufBase &aBuf, RPointerArray<T>& aArray) |
|
228 { |
|
229 aArray.ResetAndDestroy(); |
|
230 RBufReadStream rs(aBuf); |
|
231 CleanupClosePushL(rs); |
|
232 InternalizeL(aArray, rs); |
|
233 CleanupStack::PopAndDestroy(&rs); |
|
234 } |
|
235 |
|
236 // ---------------------------------------------------------------------------- |
|
237 // Helper function to create an array from buf. |
|
238 // ---------------------------------------------------------------------------- |
|
239 // |
|
240 template<typename T> |
|
241 inline void CreateFromBufferL(const CBufBase &aBuf, RArray<T>& aArray) |
|
242 { |
|
243 aArray.Reset(); |
|
244 RBufReadStream rs(aBuf); |
|
245 CleanupClosePushL(rs); |
|
246 InternalizeL(aArray, rs); |
|
247 CleanupStack::PopAndDestroy(&rs); |
|
248 } |
|
249 |
|
250 // ---------------------------------------------------------------------------- |
|
251 // Retrieves an object of from buffer |
|
252 // ---------------------------------------------------------------------------- |
|
253 // |
|
254 template<typename T> |
|
255 inline void CreateFromBufferL(const CBufBase &aBuf, T*& aObject) |
|
256 { |
|
257 delete aObject; |
|
258 aObject = NULL; |
|
259 RBufReadStream rs(aBuf); |
|
260 CleanupClosePushL(rs); |
|
261 aObject=new(ELeave)T; |
|
262 CleanupStack::PushL(aObject); |
|
263 rs>>*aObject; |
|
264 CleanupStack::Pop(aObject); |
|
265 CleanupStack::PopAndDestroy(&rs); |
|
266 } |
|
267 |
|
268 // ---------------------------------------------------------------------------- |
|
269 // Retrieves an object of from buffer |
|
270 // Note: class T must implement T::NewL(RReadStream& aStream) constructor |
|
271 // ---------------------------------------------------------------------------- |
|
272 // |
|
273 template<typename T> |
|
274 inline void NewFromBufferL(const CBufBase &aBuf, T*& aObject) |
|
275 { |
|
276 delete aObject; |
|
277 aObject = NULL; |
|
278 RBufReadStream rs(aBuf); |
|
279 CleanupClosePushL(rs); |
|
280 aObject=T::NewL(); |
|
281 CleanupStack::PushL(aObject); |
|
282 rs>>*aObject; |
|
283 CleanupStack::Pop(aObject); |
|
284 CleanupStack::PopAndDestroy(&rs); |
|
285 } |
|
286 |
|
287 // ---------------------------------------------------------------------------- |
|
288 // Retrieves an object of from message buffer |
|
289 // ---------------------------------------------------------------------------- |
|
290 // |
|
291 template<typename T> |
|
292 inline void CreateFromMessageL(const RMessage2& aMessage, |
|
293 TInt aMsgSlot, |
|
294 T*& aObject) |
|
295 { |
|
296 CBufBase* buffer(NULL); |
|
297 MPXUser::CreateBufferL(aMessage, aMsgSlot, buffer); |
|
298 CleanupStack::PushL(buffer); |
|
299 CreateFromBufferL<T>(*buffer, aObject); |
|
300 CleanupStack::PopAndDestroy(buffer); |
|
301 } |
|
302 |
|
303 // ---------------------------------------------------------------------------- |
|
304 // Retrieves an object of from message buffer |
|
305 // ---------------------------------------------------------------------------- |
|
306 // |
|
307 template<typename T> |
|
308 inline void NewFromMessageL(const RMessage2& aMessage, |
|
309 TInt aMsgSlot, |
|
310 T*& aObject) |
|
311 { |
|
312 CBufBase* buffer(NULL); |
|
313 MPXUser::CreateBufferL(aMessage, aMsgSlot, buffer); |
|
314 CleanupStack::PushL(buffer); |
|
315 NewFromBufferL<T>(*buffer, aObject); |
|
316 CleanupStack::PopAndDestroy(buffer); |
|
317 } |
|
318 |
|
319 // ---------------------------------------------------------------------------- |
|
320 // Copy an object into the buffer |
|
321 // ---------------------------------------------------------------------------- |
|
322 // |
|
323 template<typename T> |
|
324 inline void CreateBufferL(const T& aObj, CBufBase*& aBuffer) |
|
325 { |
|
326 delete aBuffer; |
|
327 aBuffer = NULL; |
|
328 CBufBase* buffer = CBufFlat::NewL(KMPXBufGranularity); |
|
329 CleanupStack::PushL(buffer); |
|
330 RBufWriteStream ws(*buffer); |
|
331 CleanupClosePushL(ws); |
|
332 ws<<aObj; |
|
333 ws.CommitL(); |
|
334 CleanupStack::PopAndDestroy(&ws); |
|
335 buffer->Compress(); |
|
336 CleanupStack::Pop(buffer); |
|
337 aBuffer = buffer; |
|
338 } |
|
339 |
|
340 // ---------------------------------------------------------------------------- |
|
341 // Create the buffer from an array |
|
342 // ---------------------------------------------------------------------------- |
|
343 // |
|
344 template<typename T> |
|
345 inline void CreateBufferL(const TArray<T>& aArray, CBufBase*& aBuffer) |
|
346 { |
|
347 delete aBuffer; |
|
348 aBuffer = NULL; |
|
349 TInt size=aArray.Count()*sizeof(TPckgBuf<T>)+sizeof(TInt); |
|
350 CBufBase* buffer = MPXUser::CreateBufferLC(size); |
|
351 RBufWriteStream ws(*buffer); |
|
352 CleanupClosePushL(ws); |
|
353 ExternalizeL(aArray, ws); |
|
354 ws.CommitL(); |
|
355 CleanupStack::PopAndDestroy(&ws); |
|
356 buffer->Compress(); |
|
357 CleanupStack::Pop(buffer); |
|
358 aBuffer = buffer; |
|
359 } |
|
360 |
|
361 // ---------------------------------------------------------------------------- |
|
362 // Create the buffer from an array of object |
|
363 // ---------------------------------------------------------------------------- |
|
364 // |
|
365 template<typename T> |
|
366 inline void CreateBufferL(const TArray<T*>& aArray, CBufBase*& aBuffer) |
|
367 { |
|
368 delete aBuffer; |
|
369 aBuffer = NULL; |
|
370 CBufBase* buffer = CBufFlat::NewL(KMPXBufGranularity); |
|
371 CleanupStack::PushL(buffer); |
|
372 RBufWriteStream ws(*buffer); |
|
373 CleanupClosePushL(ws); |
|
374 ExternalizeL(aArray, ws); |
|
375 ws.CommitL(); |
|
376 CleanupStack::PopAndDestroy(&ws); |
|
377 buffer->Compress(); |
|
378 CleanupStack::Pop(buffer); |
|
379 aBuffer = buffer; |
|
380 } |
|
381 |
|
382 // ---------------------------------------------------------------------------- |
|
383 // Copy an array from the server |
|
384 // ---------------------------------------------------------------------------- |
|
385 // |
|
386 template<typename T> |
|
387 void ArrayFromServerL(const RMPXSession& aSess, |
|
388 TInt aCmd, |
|
389 TInt aSize, |
|
390 CArrayFixBase*& aArray) |
|
391 { |
|
392 delete aArray; |
|
393 aArray = NULL; |
|
394 |
|
395 CBufBase* buffer(NULL); |
|
396 TransferBufferFromServerL(aSess, aCmd, aSize, buffer); |
|
397 if (!buffer) |
|
398 { |
|
399 CleanupStack::PushL(buffer); |
|
400 CArrayFix<T>* array = NULL; |
|
401 CreateFromBufferL<T>(*buffer, array); |
|
402 aArray = array; |
|
403 CleanupStack::PopAndDestroy(buffer); |
|
404 } |
|
405 } |
|
406 |
|
407 // ---------------------------------------------------------------------------- |
|
408 // Copy an array from the server |
|
409 // ---------------------------------------------------------------------------- |
|
410 // |
|
411 template<typename T> |
|
412 void ArrayFromServerL(const RMPXSession& aSess, |
|
413 TInt aCmd, |
|
414 TInt aSize, |
|
415 RPointerArray<T>& aArray) |
|
416 { |
|
417 CBufBase* buffer(NULL); |
|
418 TransferBufferFromServerL(aSess, aCmd, aSize, buffer); |
|
419 CleanupStack::PushL(buffer); |
|
420 CreateFromBufferL<T>(*buffer, aArray); |
|
421 CleanupStack::PopAndDestroy(buffer); |
|
422 } |
|
423 |
|
424 // ---------------------------------------------------------------------------- |
|
425 // Copy an array from the server |
|
426 // ---------------------------------------------------------------------------- |
|
427 // |
|
428 template<typename T> |
|
429 void ArrayFromServerL(const RMPXSession& aSess, |
|
430 TInt aCmd, |
|
431 TInt aSize, |
|
432 RArray<T>& aArray) |
|
433 { |
|
434 CBufBase* buffer(NULL); |
|
435 TransferBufferFromServerL(aSess, aCmd, aSize, buffer); |
|
436 CleanupStack::PushL(buffer); |
|
437 CreateFromBufferL<T>(*buffer, aArray); |
|
438 CleanupStack::PopAndDestroy(buffer); |
|
439 } |
|
440 |
|
441 // ---------------------------------------------------------------------------- |
|
442 // Packages an object of type T into a descriptor and writes back to the |
|
443 // client space in message slot aMsgSlot of aMessage. |
|
444 // ---------------------------------------------------------------------------- |
|
445 // |
|
446 template<typename T> void WriteL(const RMessage2& aMessage, |
|
447 TInt aMsgSlot,T aValue) |
|
448 { |
|
449 TPckgC<T> value(aValue); |
|
450 aMessage.WriteL(aMsgSlot,value); |
|
451 } |
|
452 |
|
453 // ---------------------------------------------------------------------------- |
|
454 // Default constructor |
|
455 // ---------------------------------------------------------------------------- |
|
456 // |
|
457 template <class T> |
|
458 inline RMPXPointerArray<T>::RMPXPointerArray() |
|
459 { |
|
460 } |
|
461 |
|
462 // ---------------------------------------------------------------------------- |
|
463 // Closes the array and frees all resources; |
|
464 // ---------------------------------------------------------------------------- |
|
465 // |
|
466 template <class T> |
|
467 inline void RMPXPointerArray<T>::Close() |
|
468 { |
|
469 this->ResetAndDestroy(); |
|
470 RPointerArray<T>::Close(); |
|
471 } |
|
472 |
|
473 // ---------------------------------------------------------------------------- |
|
474 // Destructor |
|
475 // ---------------------------------------------------------------------------- |
|
476 // |
|
477 template <class T> |
|
478 inline RMPXPointerArray<T>::~RMPXPointerArray() |
|
479 { |
|
480 Close(); |
|
481 } |
|
482 |
|
483 template<typename T> |
|
484 inline void DeleteL(const T* aItem, RPointerArray<T>& aArray) |
|
485 { |
|
486 TInt index = aArray.FindL(aItem); |
|
487 aArray.Remove(index); |
|
488 delete aItem; |
|
489 } |
|
490 // End of file |