backupandrestore/backupengine/inc/sbexternalisablearray.inl
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // externalisablearray.inl
       
    15 // Templated RArray for serialising and deserialising simple (flat) classes
       
    16 // for transmission inside descriptors over IPC
       
    17 // 
       
    18 //
       
    19 
       
    20 /**
       
    21  @file
       
    22  @publishedPartner
       
    23  @released
       
    24 */
       
    25 
       
    26 namespace conn
       
    27 	{
       
    28 	template<class T> inline RExternalisableArray<T>* RExternalisableArray<T>::InternaliseL(const TDesC8& aExternalisedArray)
       
    29 	/**
       
    30 	Internalises an RArray of objects passed via IPC from a descriptor
       
    31 	
       
    32 	@param aExternalisedArray The descriptor passed via IPC that contains the flattened array to internalise
       
    33 	@return Pointer to a newly created RExternalisableArray
       
    34 	*/
       
    35 		{
       
    36 		RExternalisableArray<T>* self = new (ELeave) RExternalisableArray<T>;
       
    37 		
       
    38 		CleanupStack::PushL(self);
       
    39 		
       
    40 		TUint8* unpackPtr = const_cast<TUint8*>(aExternalisedArray.Ptr());
       
    41 		TInt elementCount = *reinterpret_cast<TInt*>(unpackPtr);
       
    42 		
       
    43 		// point to the start of the packed elements
       
    44 		unpackPtr += sizeof(TInt);
       
    45 		
       
    46 		for (TInt index = 0; index < elementCount; index++)
       
    47 			{
       
    48 			User::LeaveIfError(self->Append(*reinterpret_cast<T*>(unpackPtr)));
       
    49 			unpackPtr += sizeof(T);
       
    50 			}
       
    51 			
       
    52 		CleanupStack::Pop(self);
       
    53 			
       
    54 		return self;
       
    55 		}
       
    56 	
       
    57 	template<class T> inline HBufC8* RExternalisableArray<T>::ExternaliseL()
       
    58 	/**
       
    59 	Externalises an RArray of objects (not pointers) into a descriptor so that the RArray
       
    60 	may be passed via IPC
       
    61 	
       
    62 	@return Pointer to the created HBufC8
       
    63 	*/
       
    64 		{
       
    65 		// calculate the flattened size of the array
       
    66 		TInt elementSize = sizeof(T);
       
    67 		TInt count = RArray<T>::Count();
       
    68 		TInt totalPackedSize = sizeof(TInt) + (sizeof(T) * count);	// TInt stores the number of elements to be packed
       
    69 	
       
    70 		// create the HBufC8 to be returned
       
    71 		HBufC8* externalisedArray = HBufC8::NewL(totalPackedSize);
       
    72 	
       
    73 		TPtr8 buffer = externalisedArray->Des();
       
    74 	
       
    75 		// Append the number of entries to the beginning of the array
       
    76 		buffer.Append(reinterpret_cast<TUint8*>(&count), sizeof(TInt));
       
    77 	
       
    78 		// bitwise copy each entry into the buffer
       
    79 		for (TInt index = 0; index < count; index++)
       
    80 			{
       
    81 			buffer.Append(reinterpret_cast<TUint8*>(&(*this)[index]), elementSize);
       
    82 			}
       
    83 		
       
    84 		return externalisedArray;
       
    85 		}
       
    86 	}