commsfwsupport/commselements/NetMeta/src/MetaContainer.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     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 //
       
    15 
       
    16 /**
       
    17  @file 
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <elements/metacontainer.h>
       
    22 #include <elements/metadata.h>
       
    23 
       
    24 namespace Meta
       
    25 {
       
    26 const TInt KContainerUid = 0x10205A04;
       
    27 }
       
    28 
       
    29 using namespace Meta;
       
    30 
       
    31 
       
    32 EXPORT_C RMetaDataContainerBase::RMetaDataContainerBase()
       
    33 /**
       
    34  * Constructor
       
    35  */
       
    36     {	
       
    37     }
       
    38 
       
    39 EXPORT_C STypeId RMetaDataContainerBase::GetTypeId() const
       
    40 	{
       
    41 	return STypeId::CreateSTypeId( KContainerUid, KNetMetaTypeAny );
       
    42 	}
       
    43 
       
    44 EXPORT_C void RMetaDataContainerBase::Copy(const RMetaDataContainerBase& aMetaContainer)
       
    45 /**
       
    46  * Copies a content of a meta container into this container.
       
    47  * Copies only pointers (does not do a deep copy).
       
    48  */
       
    49 	{
       
    50 	TUint count = aMetaContainer.Count();
       
    51 
       
    52 	for (TUint i = 0; i < count; ++i)
       
    53 		{
       
    54 		Append(aMetaContainer[i]);
       
    55 		}
       
    56 	}
       
    57 
       
    58 EXPORT_C TInt RMetaDataContainerBase::Store(TDes8& aDes) const
       
    59 /**
       
    60  * Stores the content of this container in a descriptor.
       
    61  */
       
    62     {
       
    63 	if (aDes.MaxLength()-aDes.Length() < this->Length())
       
    64 		{
       
    65 		return KErrArgument;
       
    66 		}
       
    67 
       
    68 	const STypeId& typeId = GetTypeId();
       
    69 	aDes.Append((TUint8*)&typeId,sizeof(STypeId)); //store TID
       
    70 
       
    71     TInt count = Count();
       
    72 	TInt length = Length();
       
    73 	aDes.Append((TUint8*)&length, sizeof(TInt32));	// Store Length of container
       
    74 
       
    75     for (TInt i = 0; i < count; ++i)
       
    76         {
       
    77 		SMetaData* element = (*this)[i];
       
    78 		if (element)
       
    79 			{
       
    80 	        TInt ret = element->Store(aDes);    //store the object
       
    81 			if (ret != KErrNone)
       
    82 				{
       
    83 				return ret;
       
    84 				}
       
    85 			}
       
    86 		else
       
    87 			{
       
    88 			if (aDes.MaxLength() - aDes.Length() < (TInt) sizeof(TUint32))
       
    89 				{
       
    90 				return KErrOverflow;
       
    91 				}
       
    92 			const TUint32 KNullInt = 0;
       
    93 			aDes.Append((TUint8*)&KNullInt, sizeof(TUint32));
       
    94 			}
       
    95         }
       
    96 	return KErrNone;
       
    97     }
       
    98 
       
    99 EXPORT_C TInt RMetaDataContainerBase::Load(TPtrC8& aDes)
       
   100 /**
       
   101  * Loads a content from a descriptor into this container.
       
   102  */
       
   103 	{
       
   104 	ResetAndDestroy();
       
   105     TInt32 containerLength;
       
   106     Mem::Copy(&containerLength, aDes.Ptr() ,sizeof(TInt32));
       
   107 
       
   108 	if (containerLength < 0 || (containerLength - sizeof(STypeId))> aDes.Length())
       
   109 		{
       
   110 		// Fatal error, descriptor is corrupt
       
   111 		return KErrArgument;
       
   112 		}
       
   113 
       
   114 	// Construct a buffer to work on for this container
       
   115 	TPtrC8 containerBuffer(aDes.Ptr() + sizeof(TInt32),	
       
   116 		containerLength - sizeof (STypeId) - sizeof (TInt32));
       
   117 
       
   118 	// Move descriptor pointer past the container buffer
       
   119 	aDes.Set(aDes.Ptr() + containerLength - sizeof (STypeId), aDes.Length() - containerLength + sizeof (STypeId));
       
   120 
       
   121 	while (containerBuffer.Length() > 0)
       
   122 		{
       
   123 		if (containerBuffer.Length() < (TInt) sizeof(TUint32))
       
   124 			{
       
   125 			return KErrArgument;
       
   126 			}
       
   127 
       
   128 		// Check for a NULL pointer when stored (First four bytes == 0)
       
   129 		TUint32 firstFourBytes;
       
   130         Mem::Copy(&firstFourBytes, containerBuffer.Ptr(), sizeof(TUint32));
       
   131 		if (firstFourBytes == 0)
       
   132 			{
       
   133 			// The pointer was NULL when it was stored
       
   134 			containerBuffer.Set(containerBuffer.Ptr() + sizeof(TUint32), containerBuffer.Length() - sizeof(TUint32));
       
   135 			TRAPD(error, AppendL(NULL));
       
   136 			if (error != KErrNone)
       
   137 				{
       
   138 				return error;
       
   139 				}
       
   140 			}
       
   141 		else
       
   142 			{
       
   143 			SMetaData* containerItem = NULL;
       
   144 			TRAPD(error, containerItem = LoadElementL(containerBuffer));
       
   145 			if (error == KErrNone)
       
   146 				{
       
   147 				TRAP(error, AppendL(containerItem));
       
   148 				}
       
   149 			if (error != KErrNone && error != KErrNotFound)
       
   150 				{
       
   151 				delete containerItem;
       
   152 				return error;
       
   153 				}
       
   154 			}
       
   155 		}
       
   156 	return KErrNone;
       
   157 	}
       
   158 
       
   159 EXPORT_C TInt RMetaDataContainerBase::Length() const
       
   160 /**
       
   161  * Returns the length of the content of this container if it was to be stored.
       
   162  */
       
   163 	{
       
   164     TInt length = 0, count = this->Count();
       
   165     for (TInt i = 0; i < count; ++i)
       
   166 		{
       
   167 		SMetaData* element = (*this)[i];
       
   168 		if (element)
       
   169 			{
       
   170 			// UID + Object Type Id + Length Parameter + Length of Data
       
   171 			length += element->Length();
       
   172 			}
       
   173 		else
       
   174 			{
       
   175 			// Add length of null word (Used to represent a NULL pointer)
       
   176 			length += sizeof(TUint32);
       
   177 			}
       
   178 		}
       
   179 	return length + sizeof(TInt32) + sizeof(STypeId);
       
   180 	}
       
   181 
       
   182 
       
   183