Update contrib.
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Implementation of utility functions for copying and streaming RArray/RpointerArray.
26 #ifndef STREAMINGARRAY_INL
27 #define STREAMINGARRAY_INL
30 #include <scs/streamingarray.h>
31 #include <scs/nullstream.h>
32 #include <scs/streamingarray.h>
34 // Traits classes - internally required by RArray/RPointerArray functions
40 static T* CopyL(const T& aRight) { return T::NewL(aRight);}
41 static T* ReadFromStreamL(RReadStream& aStream) { return T::NewL(aStream); }
42 static T* ReadFromStreamLC(RReadStream& aStream) { return T::NewLC(aStream); }
43 static void WriteToStreamL(const T& aItem, RWriteStream& aStream) { aStream << aItem; }
46 // Specialisation for HBufC
48 class TTraits<HBufC16>
51 static HBufC16* CopyL(const HBufC16& aOther) { return aOther.AllocL();}
52 static HBufC16* ReadFromStreamL(RReadStream& aStream) { return HBufC16::NewL(aStream, KMaxTInt); }
53 static HBufC16* ReadFromStreamLC(RReadStream& aStream) { return HBufC16::NewLC(aStream, KMaxTInt); }
54 static void WriteToStreamL(const HBufC16& aItem, RWriteStream& aStream) { aStream << aItem; }
61 static HBufC8* CopyL(const HBufC8& aOther) { return aOther.AllocL();}
62 static HBufC8* ReadFromStreamL(RReadStream& aStream) { return HBufC8::NewL(aStream, KMaxTInt); }
63 static HBufC8* ReadFromStreamLC(RReadStream& aStream) { return HBufC8::NewLC(aStream, KMaxTInt); }
64 static void WriteToStreamL(const HBufC8& aItem, RWriteStream& aStream) { aStream << aItem; }
67 // Size calculator - internally required by RPointerArray functions
70 TInt GetObjectBufferSizeL(const T& aObject)
73 RNullWriteStream nullstream;
74 CleanupClosePushL(nullstream);
75 nullstream << aObject;
77 size = nullstream.BytesWritten();
78 CleanupStack::PopAndDestroy(&nullstream);
83 TInt ExternalizedBufferSizeL(const RPointerArray<T>& aArray)
85 Calculates how much buffer is required for a pointers array to be externalized.
86 @param aArray The pointer array whose buffer size will be calculated.
87 @return Returns the number of bytes occupied by the elements of the pointer array.
90 TInt totalSize(1); // 1-byte is reserved for the array count
91 TInt count = aArray.Count();
93 for (TInt i = 0; i < count; ++i)
95 totalSize += GetObjectBufferSizeL(*aArray[i]);
101 // RPointerArray utilities
104 void CopyPointerArrayL(RPointerArray<T>& aTarget, const RPointerArray<T>& aSource)
106 TInt arrayCount = aSource.Count();
107 for (TInt i = 0; i < arrayCount; ++i)
109 T* item = TTraits<T>::CopyL(*aSource[i]);
110 CleanupStack::PushL(item);
111 User::LeaveIfError(aTarget.Append(item));
112 CleanupStack::Pop(item);
117 HBufC8* ExternalizePointersArrayLC(const RPointerArray<T>& aArray, TBool aAddLength)
119 TInt arraySize = ExternalizedBufferSizeL(aArray);
120 HBufC8 *arrayBuf = HBufC8::NewLC(arraySize + sizeof(TInt32));
121 TPtr8 arrayPtr(arrayBuf->Des());
123 RDesWriteStream stream(arrayPtr);
125 ExternalizePointersArrayL(aArray, stream, aAddLength);
132 void ExternalizePointersArrayL(const RPointerArray<T>& aArray, RWriteStream& aStream, TBool aAddLength)
134 TInt arrayCount = aArray.Count();
136 aStream << TCardinality(arrayCount);
137 for (TInt i = 0; i < arrayCount; ++i)
139 TTraits<T>::WriteToStreamL(*aArray[i], aStream);
145 void InternalizePointersArrayL(RPointerArray<T>& aArray, RReadStream& aStream, TBool aAddLength)
154 for (TInt i = 0; i < count; ++i)
156 T* item = TTraits<T>::ReadFromStreamL(aStream);
157 CleanupStack::PushL(item);
158 User::LeaveIfError(aArray.Append(item));
159 CleanupStack::Pop(item);
164 // Size calculator - internally required by RArray/RPointerArray functions
167 TInt ExternalizedBufferSizeL(const RArray<T>& aArray)
169 Calculates how much buffer is required for a fixed length array to be externalized.
170 @param aArray The array whose buffer size will be calculated.
171 @return Returns the number of bytes of the array's dynamic memory.
174 TInt totalSize(1); // 1-byte is reserved for the array count
175 TInt count = aArray.Count();
177 for (TInt i = 0; i < count; ++i)
179 totalSize += GetObjectBufferSizeL(TPckgC<T>(aArray[i]));
188 void CopyFixedLengthArrayL(RArray<T>& aTarget, const RArray<T>& aSource)
190 TInt arrayCount = aSource.Count();
191 for (TInt i = 0; i < arrayCount; ++i)
193 User::LeaveIfError(aTarget.Append(aSource[i]));
198 HBufC8* ExternalizeFixedLengthArrayL(const RArray<T>& aArray)
200 TInt arraySize = ExternalizedBufferSizeL(aArray, arraySize);
201 HBufC8 *arrayBuf = HBufC8::NewLC(arraySize + sizeof(TInt32));
202 TPtr8 arrayPtr(arrayBuf->Des());
204 RDesWriteStream stream(arrayPtr);
206 ExternalizeFixedLengthArrayL(aArray, stream);
213 void ExternalizeFixedLengthArrayL(const RArray<T>& aArray, RWriteStream& aStream)
215 TInt arrayCount = aArray.Count();
216 aStream << TCardinality(arrayCount);
217 for (TInt i = 0; i < arrayCount; ++i)
219 aStream.WriteL(TPckgC<T>(aArray[i]));
226 void InternalizeFixedLengthArrayL(RArray<T>& aArray, RReadStream& aStream)
231 for (TInt i = 0; i < count; ++i)
234 TPckg<T> itemPckg(item);
235 aStream.ReadL(itemPckg);
236 User::LeaveIfError(aArray.Append(item));
240 #endif /* STREAMINGARRAY_INL*/