imservices/searchfeatureplugin/srcsearchdatamodel/searchapidataobjfactory.inl
changeset 51 61fad867f68e
equal deleted inserted replaced
-1:000000000000 51:61fad867f68e
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Inline methods for codec to pack and unpack data objects.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <ximpapidataobjbase.h>
       
    21 
       
    22 #include "searchtypehelpers.h"
       
    23 #include "searchobjectfactory.h"
       
    24 
       
    25 
       
    26 #include "searchinfoimp.h"
       
    27 
       
    28 //DATA TYPES
       
    29 
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS =============================
       
    32 
       
    33 /**
       
    34  * Type definition for exact API data object constructor signature.
       
    35  */
       
    36 typedef CXIMPApiDataObjBase* (*TApiDataObjConstructor)( RReadStream& );
       
    37 
       
    38 
       
    39 
       
    40 /**
       
    41  * Structure for mapping data object interface IDs to
       
    42  * to corect API data object constructors.
       
    43  */
       
    44 struct SApiDataObjConstructorMap
       
    45     {
       
    46     //Interface id
       
    47     TInt32    iInterfaceId;
       
    48 
       
    49     //Function pointer to data object interface implementation
       
    50     TApiDataObjConstructor iConstructorPtr;
       
    51     } ;
       
    52 
       
    53 
       
    54 
       
    55 /**
       
    56  * Helper macro to initialise KApiDataObjConstructorTable members.
       
    57  *
       
    58  * Macro forces that each data object implementation class to have static
       
    59  * NewFromStreamLC() member function to instantiate the object.
       
    60  *
       
    61  * See TApiDataObjConstructor type definition for exact constructor
       
    62  * signature.
       
    63  */
       
    64 #define CONSTRUCTOR_ENTRY( TheImplementedIf, TheClass ) \
       
    65     { TheImplementedIf::KInterfaceId, TheClass::NewFromStreamLC } \
       
    66 
       
    67 /**
       
    68  * Constructor function mapping for data object interface implementations.
       
    69  *
       
    70  * When new data object types are implemented, add them here.
       
    71  */
       
    72 
       
    73 const SApiDataObjConstructorMap KApiDataObjConstructorTable[] =
       
    74     {
       
    75     CONSTRUCTOR_ENTRY( MSearchInfo, CSearchInfoImp  ),
       
    76     };
       
    77 
       
    78 
       
    79 /**
       
    80  * Count of constructor mappings.
       
    81  */
       
    82 const TInt KApiDataObjConstructorCount 	= sizeof( KApiDataObjConstructorTable )
       
    83                                        / sizeof( SApiDataObjConstructorMap );
       
    84 
       
    85 
       
    86 
       
    87 // ============================ HELPER FUNCTIONS =============================
       
    88 
       
    89 
       
    90 namespace {
       
    91 
       
    92     /**
       
    93      * Helper function to locate constructor function for
       
    94      * data object interface ID.
       
    95      *
       
    96      * @param aDataObjInterfaceId The data object interface ID.
       
    97      * @return Data object constructor function.
       
    98      */
       
    99     TApiDataObjConstructor ConstructorForInterface( TInt32 aDataObjInterfaceId )
       
   100     {
       
   101         //Locate correct constructor
       
   102         for( TInt ix = 0; ix < KApiDataObjConstructorCount; ix++ )
       
   103         {
       
   104            const SApiDataObjConstructorMap map = KApiDataObjConstructorTable[ ix ];
       
   105             if( map.iInterfaceId == aDataObjInterfaceId )
       
   106             {
       
   107                 return map.iConstructorPtr;
       
   108             } 
       
   109         }
       
   110 
       
   111 
       
   112         //If assertion below fails, check that data object implementation
       
   113         //class implementing requested data object interface (aDataObjInterfaceId)
       
   114         //is registered to KApiDataObjConstructorTable
       
   115         __ASSERT_DEBUG( EFalse,
       
   116                 User::Panic( _L("ImObjectFactory"), KErrUnknown ) );
       
   117 
       
   118         return NULL;
       
   119     }
       
   120 
       
   121     /**
       
   122      * Helper function to instantiate new data object object
       
   123      * of requested type and construct it from the stream.
       
   124      *
       
   125      * @param aDataObjInterfaceId
       
   126      * @return Data object constructor function.
       
   127      */
       
   128     CXIMPApiDataObjBase* NewDataObjectFromStreamLC( TInt32 aDataObjInterfaceId,
       
   129             RReadStream& aStream )
       
   130     {
       
   131         TApiDataObjConstructor newFromStreamLC = NULL;
       
   132 
       
   133         //Locate correct constructor for interface ID
       
   134         newFromStreamLC = ConstructorForInterface( aDataObjInterfaceId );
       
   135 
       
   136         //Instantiate the object
       
   137         CXIMPApiDataObjBase* dataObject = newFromStreamLC( aStream );
       
   138 
       
   139         return dataObject;
       
   140     }
       
   141 
       
   142 } // namespace
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // SearchApiDataObjFactory::InternalizeL
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 template< class INFOIMP >
       
   149 inline void CSearchApiDataObjFactory::InternalizeL( 
       
   150         RReadStream& aStream,
       
   151         RPointerArray<INFOIMP>& aArray )
       
   152     {
       
   153     TInt32 arrLen = aStream.ReadInt32L();
       
   154 
       
   155     for ( TInt i = 0; i < arrLen; i++ )
       
   156         {
       
   157         TInt paramType = aStream.ReadInt32L();
       
   158         INFOIMP* infoObj = ( INFOIMP* ) NewDataObjectFromStreamLC( paramType, aStream );
       
   159         aArray.AppendL( infoObj );
       
   160         CleanupStack::Pop( infoObj );
       
   161         }
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // SearchApiDataObjFactory::ExternalizeL
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 template <class INFOIMP>
       
   169 inline void CSearchApiDataObjFactory::ExternalizeL( 
       
   170         RWriteStream& aWs,
       
   171         const RPointerArray<INFOIMP>& aArray )
       
   172     {
       
   173     aWs.WriteInt32L( aArray.Count() ); // array length
       
   174 
       
   175     for ( TInt i = 0; i < aArray.Count(); i++ )
       
   176         {
       
   177         INFOIMP* infoField = aArray[ i ];
       
   178 
       
   179         // write the type
       
   180         aWs.WriteInt32L( infoField->Base().GetInterfaceId() );
       
   181 
       
   182         // write the actual object
       
   183         infoField->ExternalizeL( aWs );
       
   184         }
       
   185 
       
   186     aWs.CommitL();
       
   187     }
       
   188