cryptomgmtlibs/securitycommonutils/inc/streamingarray.inl
changeset 8 35751d3474b7
child 61 641f389e9157
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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: 
       
    15 * Implementation of utility functions for copying and streaming RArray/RpointerArray. 
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file 
       
    22  @internalTechnology
       
    23  @prototype
       
    24 */
       
    25 
       
    26 #ifndef STREAMINGARRAY_INL
       
    27 #define STREAMINGARRAY_INL
       
    28 
       
    29 #include <s32mem.h>
       
    30 #include <scs/streamingarray.h>
       
    31 #include <scs/nullstream.h>
       
    32 #include <scs/streamingarray.h>
       
    33 
       
    34 // Traits classes - internally required by RArray/RPointerArray functions
       
    35 
       
    36 template <class T>
       
    37 class TTraits
       
    38 	{
       
    39 public:
       
    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; }
       
    44 	};
       
    45 
       
    46 // Specialisation for HBufC
       
    47 template <>
       
    48 class TTraits<HBufC16>
       
    49 	{
       
    50 public:
       
    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; }
       
    55 	};
       
    56 		
       
    57 template <>
       
    58 class TTraits<HBufC8>
       
    59 	{
       
    60 public:
       
    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; }
       
    65 	};
       
    66 
       
    67 // Size calculator - internally required by RPointerArray functions
       
    68 
       
    69 template <typename T>
       
    70 TInt GetObjectBufferSizeL(const T& aObject)
       
    71 	{
       
    72 	TInt size(0);
       
    73 	RNullWriteStream nullstream;
       
    74 	CleanupClosePushL(nullstream);
       
    75 	nullstream << aObject;
       
    76 	nullstream.CommitL();
       
    77 	size = nullstream.BytesWritten();
       
    78 	CleanupStack::PopAndDestroy(&nullstream);
       
    79 	return size;
       
    80 	}
       
    81 
       
    82 template <class T>
       
    83 TInt ExternalizedBufferSizeL(const RPointerArray<T>& aArray)
       
    84 /**
       
    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.
       
    88  */
       
    89 	{
       
    90 	TInt totalSize(1);	// 1-byte is reserved for the array count
       
    91 	TInt count = aArray.Count();
       
    92 	
       
    93 	for (TInt i = 0; i < count; ++i)
       
    94 			{
       
    95 			totalSize += GetObjectBufferSizeL(*aArray[i]);
       
    96 			}
       
    97 	return totalSize;
       
    98 	}
       
    99 
       
   100 
       
   101 // RPointerArray utilities
       
   102 
       
   103 template <class T>
       
   104 void CopyPointerArrayL(RPointerArray<T>& aTarget, const RPointerArray<T>& aSource)
       
   105 	{
       
   106 	TInt arrayCount =  aSource.Count();
       
   107 	for (TInt i = 0; i < arrayCount; ++i)
       
   108 		{
       
   109 		T* item = TTraits<T>::CopyL(*aSource[i]);
       
   110 		CleanupStack::PushL(item);
       
   111 		User::LeaveIfError(aTarget.Append(item));
       
   112 		CleanupStack::Pop(item);
       
   113 		}
       
   114 	}
       
   115 	
       
   116 template <class T>
       
   117 HBufC8* ExternalizePointersArrayLC(const RPointerArray<T>& aArray, TBool aAddLength)
       
   118 	{
       
   119 	TInt arraySize = ExternalizedBufferSizeL(aArray);
       
   120 	HBufC8 *arrayBuf = HBufC8::NewLC(arraySize + sizeof(TInt32));
       
   121 	TPtr8 arrayPtr(arrayBuf->Des());
       
   122 	
       
   123 	RDesWriteStream stream(arrayPtr);
       
   124 	stream.PushL();
       
   125 	ExternalizePointersArrayL(aArray, stream, aAddLength);
       
   126 	stream.Pop();
       
   127 	stream.Release();
       
   128 	return arrayBuf;
       
   129 	}
       
   130 
       
   131 template <class T>
       
   132 void ExternalizePointersArrayL(const RPointerArray<T>& aArray, RWriteStream& aStream, TBool aAddLength)
       
   133 	{
       
   134 	TInt arrayCount = aArray.Count();
       
   135 	if(aAddLength)
       
   136 		aStream << TCardinality(arrayCount);
       
   137 	for (TInt i = 0; i < arrayCount; ++i)
       
   138 		{
       
   139 		TTraits<T>::WriteToStreamL(*aArray[i], aStream);
       
   140 		}
       
   141 	aStream.CommitL();
       
   142 	}
       
   143 
       
   144 template <class T>
       
   145 void InternalizePointersArrayL(RPointerArray<T>& aArray, RReadStream& aStream, TBool aAddLength)
       
   146 	{
       
   147 	TInt count (0);
       
   148 	if(aAddLength)
       
   149 		{
       
   150 		TCardinality c;
       
   151 		aStream >> c;
       
   152 		count = c;
       
   153 		}
       
   154 	for (TInt i = 0; i < count; ++i)
       
   155 		{
       
   156 		T* item = TTraits<T>::ReadFromStreamL(aStream);
       
   157 		CleanupStack::PushL(item);
       
   158 		User::LeaveIfError(aArray.Append(item));
       
   159 		CleanupStack::Pop(item);
       
   160 		}
       
   161 	}
       
   162 
       
   163 
       
   164 // Size calculator - internally required by RArray/RPointerArray functions
       
   165 
       
   166 template <class T>
       
   167 TInt ExternalizedBufferSizeL(const RArray<T>& aArray)
       
   168 /**
       
   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.
       
   172  */
       
   173 	{
       
   174 	TInt totalSize(1); // 1-byte is reserved for the array count
       
   175 	TInt count = aArray.Count();
       
   176 
       
   177 	for (TInt i = 0; i < count; ++i)
       
   178 			{
       
   179 			totalSize += GetObjectBufferSizeL(TPckgC<T>(aArray[i]));
       
   180 			}
       
   181 	return totalSize;
       
   182 	}
       
   183 
       
   184 
       
   185 // RArray utilities
       
   186 
       
   187 template <class T>
       
   188 void CopyFixedLengthArrayL(RArray<T>& aTarget, const RArray<T>& aSource)
       
   189 	{
       
   190 	TInt arrayCount = aSource.Count();
       
   191 	for (TInt i = 0; i < arrayCount; ++i)
       
   192 		{
       
   193 		User::LeaveIfError(aTarget.Append(aSource[i]));
       
   194 		}
       
   195 	}
       
   196 
       
   197 template <class T>
       
   198 HBufC8* ExternalizeFixedLengthArrayL(const RArray<T>& aArray)
       
   199 	{
       
   200 	TInt arraySize = ExternalizedBufferSizeL(aArray, arraySize);
       
   201 	HBufC8 *arrayBuf = HBufC8::NewLC(arraySize + sizeof(TInt32));
       
   202 	TPtr8 arrayPtr(arrayBuf->Des());
       
   203 	
       
   204 	RDesWriteStream stream(arrayPtr);
       
   205 	stream.PushL();
       
   206 	ExternalizeFixedLengthArrayL(aArray, stream);
       
   207 	stream.Pop();
       
   208 	stream.Release();
       
   209 	return arrayBuf;
       
   210 	}
       
   211 
       
   212 template <class T>
       
   213 void ExternalizeFixedLengthArrayL(const RArray<T>& aArray, RWriteStream& aStream)
       
   214 	{
       
   215 	TInt arrayCount = aArray.Count();
       
   216 	aStream << TCardinality(arrayCount);
       
   217 	for (TInt i = 0; i < arrayCount; ++i)
       
   218 		{
       
   219 		aStream.WriteL(TPckgC<T>(aArray[i]));
       
   220 		}
       
   221 	aStream.CommitL();
       
   222 	}
       
   223 
       
   224 
       
   225 template <class T>
       
   226 void InternalizeFixedLengthArrayL(RArray<T>& aArray, RReadStream& aStream)
       
   227 	{
       
   228 	TCardinality c;
       
   229 	aStream >> c;
       
   230 	TInt count = c;
       
   231 	for (TInt i = 0; i < count; ++i)
       
   232 		{
       
   233 		T item;
       
   234 		TPckg<T> itemPckg(item);
       
   235 		aStream.ReadL(itemPckg);
       
   236 		User::LeaveIfError(aArray.Append(item));
       
   237 		}
       
   238 	}
       
   239 
       
   240 #endif /* STREAMINGARRAY_INL*/