mpx/commonframework/common/src/mpxmediadataarray.cpp
changeset 0 a2952bb97e68
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     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:  Implementation of array of variable size elements on global chunk
       
    15 *
       
    16 */
       
    17 
       
    18 #include "mpxmediadataarray.h"
       
    19 #include "mpxheapmanager.h"
       
    20 #include "mpxdata.h"
       
    21 
       
    22 #define ITEM(x,y) RMPXHeapManager::Ptr<RMPXMediaDataItem>(x,y)
       
    23 
       
    24 // -----------------------------------------------------------------------------
       
    25 // RMPXMediaDataItemArray::Append
       
    26 // -----------------------------------------------------------------------------
       
    27 //
       
    28 TInt RMPXMediaDataItemArray::Append(
       
    29     TUint aClientHandle,
       
    30     const TMPXAttribute& aAttr,
       
    31     TMPXAttributeType aAttrType,
       
    32     const TDesC8& aData)
       
    33     {
       
    34     MPX_ASSERT(aClientHandle);
       
    35     //
       
    36     TInt r=KErrNone;
       
    37     RMPXMediaDataItem* item=NewItem(aClientHandle,aAttr,aAttrType,aData);
       
    38     if (item)
       
    39         {
       
    40         RMPXDataItemArray::Append(aClientHandle,*item);
       
    41         }
       
    42     else
       
    43         {
       
    44         r=KErrNoMemory;
       
    45         }
       
    46     return r;
       
    47     }
       
    48 
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // RMPXMediaDataItemArray::Set
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 TInt RMPXMediaDataItemArray::Set(
       
    55     TUint aClientHandle,
       
    56     const TMPXAttribute& aAttr,
       
    57     TMPXAttributeType aAttrType,
       
    58     const TDesC8& aData,
       
    59     TInt aIndex)
       
    60     {
       
    61     TInt r=KErrNone;
       
    62     RMPXMediaDataItem* item=NewItem(aClientHandle,aAttr,aAttrType,aData);
       
    63     if (item)
       
    64         {       
       
    65         RMPXDataItemArray::Set(aClientHandle,*item,aIndex);
       
    66         }
       
    67     else
       
    68         {
       
    69         r=KErrNoMemory;
       
    70         }
       
    71     return r;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // RMPXMediaDataItemArray::RMPXMediaDataItemArray
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 TInt RMPXMediaDataItemArray::Insert(
       
    79     TUint aClientHandle,
       
    80     const TMPXAttribute& aAttr,
       
    81     TMPXAttributeType aAttrType,
       
    82     const TDesC8& aData,
       
    83     TInt aIndex)
       
    84     {
       
    85     TInt r=KErrNone;
       
    86     RMPXMediaDataItem* item=NewItem(aClientHandle,aAttr,aAttrType,aData);
       
    87     if (item)
       
    88         {       
       
    89         RMPXDataItemArray::Insert(aClientHandle,*item,aIndex);
       
    90         }
       
    91     else
       
    92         {
       
    93         r=KErrNoMemory;
       
    94         }
       
    95     return r;
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // RMPXMediaDataItemArray::Index
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 TInt RMPXMediaDataItemArray::Index(TUint aClientHandle,const TMPXAttribute& aAttr)
       
   103     {
       
   104     TInt index( KErrNotFound );
       
   105     RMPXMediaDataItem* item=NULL;
       
   106     
       
   107     if (iFirstItemOffset)
       
   108         {
       
   109         item=ITEM(aClientHandle,iFirstItemOffset);
       
   110         while(item)
       
   111             {
       
   112             index++;
       
   113             if (item->Attribute()==aAttr)
       
   114                 {
       
   115                 //
       
   116                 // Found it, so break outer of the loop, but first store
       
   117                 // the current position in case an index based query will
       
   118                 // follow (e,g, for data, type etc.)
       
   119                 //
       
   120                 iIndex=index;
       
   121                 iPos=RMPXHeapManager::Offset(aClientHandle,item);
       
   122                 break;
       
   123                 }
       
   124             TInt next=item->NextOffset();
       
   125             item=next?ITEM(aClientHandle,next):NULL;
       
   126             }
       
   127         }
       
   128     return item?index:KErrNotFound;
       
   129     }
       
   130  
       
   131  // -----------------------------------------------------------------------------
       
   132 // RMPXMediaDataItemArray::NewItem
       
   133 // -----------------------------------------------------------------------------
       
   134 //   
       
   135  RMPXMediaDataItem* RMPXMediaDataItemArray::NewItem(
       
   136     TUint aClientHandle,
       
   137     const TMPXAttribute& aAttr,
       
   138     TMPXAttributeType aAttrType,
       
   139     const TDesC8& aData)
       
   140     {
       
   141     MPX_ASSERT(aClientHandle);
       
   142     //
       
   143     RMPXHeapManager& m=RMPXHeapManager::HeapManager(aClientHandle);
       
   144     TAny* ptr=m.Alloc(aClientHandle, sizeof(RMPXMediaDataItem) + aData.Size());
       
   145     RMPXMediaDataItem* item(NULL);
       
   146     if (ptr) 
       
   147         { 
       
   148         //Alloc media data item
       
   149         item = new(ptr)RMPXMediaDataItem();
       
   150         //Copy data to RMPXMediaDataItem
       
   151         item->Copy(aClientHandle,aData);
       
   152         item->SetAttribute(aAttr,aAttrType);
       
   153         }
       
   154     return item;
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // RMPXDataItemArray::Copy
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 TInt RMPXMediaDataItemArray::Copy(
       
   162     TUint aClientHandle,
       
   163     const RMPXMediaDataItemArray& aArray)
       
   164     {
       
   165     Reset(aClientHandle); // Clear existing data
       
   166     TInt r=KErrNone;
       
   167     TInt first=aArray.iFirstItemOffset;
       
   168     if (first)
       
   169         {
       
   170         RMPXMediaDataItem* item=ITEM(aClientHandle,first);
       
   171         while(item)
       
   172             {
       
   173             MMPXData* d=MMPXData::Data(aClientHandle,
       
   174                                        item->Buf(aClientHandle),
       
   175                                        item->Size());
       
   176             if (d)
       
   177                 { 
       
   178                 // It's a global object. e.g. media or media array, so we need to copy
       
   179                 // that and place the handle into the buffer
       
   180                 //
       
   181                 MMPXData* newd=NULL;
       
   182                 TRAP(r,newd=MMPXData::NewL(aClientHandle,*d)); // will end up recursive
       
   183                 if (r==KErrNone)
       
   184                     {
       
   185                     // Create the data: a buffer of 8 bytes
       
   186                     //
       
   187                     const TInt KMediaStreamLen=sizeof(MMPXData::TMPXObjectType)+sizeof(TUint);
       
   188                     TBuf8<KMediaStreamLen> newbuf;
       
   189                     //
       
   190                     TUint* ptr=(TUint*)newbuf.Ptr(); // pointer to the new (empty) buffer
       
   191                     TUint* oldPtr=(TUint*)item->Buf(aClientHandle); // pointer to the old data
       
   192                     //
       
   193                     *ptr=*oldPtr; // the TMPXObjectType
       
   194                     *++ptr=newd->DataHandle(aClientHandle); //  the data: handle to this new data
       
   195                     //
       
   196                     newbuf.SetLength(KMediaStreamLen);
       
   197                     //
       
   198                     r=Append(aClientHandle,item->Attribute(),item->Type(),newbuf);
       
   199                     //
       
   200                     // Don't need to call newd->Release(aClientHandle) currently, since the Append()
       
   201                     // would have created a ref count of 1, and the MMPXData::NewL() doesn't.
       
   202                     //
       
   203                     }
       
   204                 }
       
   205             else 
       
   206                 {
       
   207                 // Regular streamed object, so we can duplicate it by just copying the bytes
       
   208                 // in an Append()
       
   209                 //
       
   210                 r=Append(aClientHandle,item->Attribute(),item->Type(),item->Data(aClientHandle));
       
   211                 }
       
   212             item=item->NextOffset()&&r==KErrNone?ITEM(aClientHandle,item->NextOffset()):NULL;
       
   213             }
       
   214         }
       
   215     return r;
       
   216     }
       
   217 
       
   218 // End of file
       
   219