ximpfw/core/srcdatamodel/ximpeventcodec.cpp
changeset 0 e6b17d312c8b
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     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:  Base class for API implementations.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32strm.h>
       
    20 #include <s32mem.h>
       
    21 
       
    22 #include "ximpeventcodec.h"
       
    23 #include "ximprequestcompleteeventimp.h"
       
    24 #include "ximpcontextstateeventimp.h"
       
    25 
       
    26 
       
    27 /**
       
    28  * Type definition for exact API event constructor signature.
       
    29  */
       
    30 typedef CXIMPApiEventBase* (*TApiEventConstructor)( RReadStream& );
       
    31 
       
    32 
       
    33 
       
    34 /**
       
    35  * Structure for mapping event interface IDs to
       
    36  * to corect API event constructors.
       
    37  */
       
    38 struct SApiEventConstructorMap
       
    39     {
       
    40     //Interface id
       
    41     TInt32    iInterfaceId;
       
    42 
       
    43     //Function pointer to event interface implementation
       
    44     TApiEventConstructor iConstructorPtr;
       
    45     } ;
       
    46 
       
    47 
       
    48 
       
    49 /**
       
    50  * Helper macro to initialise KApiEventConstructorTable members.
       
    51  *
       
    52  * Macro forces that each event implementation class to have static
       
    53  * NewFromStreamLC() memeber function to instantiate the object.
       
    54  *
       
    55  * See TApiEventConstructor type definition for exact constructor
       
    56  * signature.
       
    57  */
       
    58 #define CONSTRUCTOR_ENTRY( TheImplementedIf, TheClass ) \
       
    59     { TheImplementedIf::KInterfaceId, TheClass::NewFromStreamLC } \
       
    60 
       
    61 
       
    62 
       
    63 /**
       
    64  * Constructor function mapping for event interface implementations.
       
    65  *
       
    66  * When new event types are implemented, add them here.
       
    67  */
       
    68 const SApiEventConstructorMap KApiEventConstructorTable[] =
       
    69     {
       
    70     CONSTRUCTOR_ENTRY( MXIMPRequestCompleteEvent, CXIMPRequestCompleteEventImp ),
       
    71     CONSTRUCTOR_ENTRY( MXIMPContextStateEvent, CXIMPContextStateEventImp )
       
    72     };
       
    73 
       
    74 
       
    75 /**
       
    76  * Count of constructor mappings.
       
    77  */
       
    78 const TInt KApiEventConstructorCount = sizeof( KApiEventConstructorTable )
       
    79                                         / sizeof( SApiEventConstructorMap );
       
    80 
       
    81 
       
    82 
       
    83 // ============================ HELPER FUNCTIONS =============================
       
    84 
       
    85 
       
    86 /**
       
    87  * Helper function to locate constructor function for
       
    88  * event interface ID.
       
    89  *
       
    90  * @param aEventInterfaceId The event interface ID.
       
    91  * @return Event object constructor function.
       
    92  */
       
    93 TApiEventConstructor ConstructorForInterface( TInt32 aEventInterfaceId )
       
    94     {
       
    95     //Locate correct constructor
       
    96     for( TInt ix = 0; ix < KApiEventConstructorCount; ix++ )
       
    97         {
       
    98         const SApiEventConstructorMap map = KApiEventConstructorTable[ ix ];
       
    99         if( map.iInterfaceId == aEventInterfaceId )
       
   100             {
       
   101             return map.iConstructorPtr;
       
   102             }
       
   103         }
       
   104 
       
   105 
       
   106     //If assertion below fails, check that event implementation
       
   107     //class implementing requested event interface (aEventInterfaceId)
       
   108     //is registered to KApiEventConstructorTable
       
   109 //    __ASSERT_DEBUG( EFalse,
       
   110 //                    User::Panic( _L("XIMPEventCodec"), KErrUnknown ) );
       
   111 
       
   112     return NULL;
       
   113     }
       
   114 
       
   115 
       
   116 /**
       
   117  * Helper function to instantiate new event object
       
   118  * of requested type and construct it from the stream.
       
   119  *
       
   120  * @param aEventInterfaceId
       
   121  * @return Event object constructor function.
       
   122  */
       
   123 CXIMPApiEventBase* NewEventObjectFromStreamLC( TInt32 aEventInterfaceId,
       
   124                                                RReadStream& aStream )
       
   125     {
       
   126     TApiEventConstructor newFromStreamLC = NULL;
       
   127 
       
   128     //Locate correct constructor for interface ID
       
   129     newFromStreamLC = ConstructorForInterface( aEventInterfaceId );
       
   130 
       
   131     if ( newFromStreamLC )
       
   132         {
       
   133         //Instantiate the object
       
   134         CXIMPApiEventBase* eventObject = newFromStreamLC( aStream );
       
   135         return eventObject;
       
   136         }
       
   137 
       
   138 
       
   139     // if no constructor
       
   140     return NULL;
       
   141     }
       
   142 
       
   143 
       
   144 
       
   145 // ============================ MEMBER FUNCTIONS =============================
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // XIMPEventCodec::PackL()
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C HBufC8* XIMPEventCodec::PackL( CXIMPApiEventBase& aEventObj,
       
   152                                         TInt32& aTypeOfEvent )
       
   153     {
       
   154     CBufFlat* buffer = CBufFlat::NewL( 10 ); // initial granularity to 10
       
   155     CleanupStack::PushL( buffer );
       
   156 
       
   157     RBufWriteStream ws;
       
   158     CleanupClosePushL( ws );
       
   159     ws.Open( *buffer ); // CSI: 65 #
       
   160 
       
   161     //Ask real event type through the event base interface
       
   162     TInt32 eventIfId = aEventObj.Base().GetInterfaceId();
       
   163 
       
   164     //And write both event type and data
       
   165     ws.WriteInt32L( eventIfId );
       
   166     aEventObj.ExternalizeL( ws );
       
   167     ws.CommitL();
       
   168     CleanupStack::PopAndDestroy();  //ws
       
   169 
       
   170     HBufC8* heapBuf = buffer->Ptr( 0 ).AllocL();
       
   171 
       
   172     CleanupStack::PopAndDestroy( buffer );
       
   173 
       
   174 
       
   175     aTypeOfEvent = eventIfId;
       
   176     return heapBuf;
       
   177     }
       
   178 
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // XIMPEventCodec::PackL()
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 EXPORT_C void XIMPEventCodec::PackL( CXIMPApiEventBase& aEventObj,
       
   185                                      TInt32& aTypeOfEvent,
       
   186                                      CBufFlat* aBuffer )
       
   187     {
       
   188     RBufWriteStream ws;
       
   189     CleanupClosePushL( ws );
       
   190     ws.Open( *aBuffer ); // CSI: 65 #
       
   191 
       
   192     //Ask real event type through the event base interface
       
   193     TInt32 eventIfId = aEventObj.Base().GetInterfaceId();
       
   194 
       
   195     //And write both event type and data
       
   196     ws.WriteInt32L( eventIfId );
       
   197     aEventObj.ExternalizeL( ws );
       
   198     ws.CommitL();
       
   199     CleanupStack::PopAndDestroy();  //ws
       
   200     
       
   201     aTypeOfEvent = eventIfId;
       
   202     }
       
   203 
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // XIMPEventCodec::UnPackL()
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 EXPORT_C CXIMPApiEventBase* XIMPEventCodec::UnPackL( const TDesC8& aEventData,
       
   210                                                      TInt32& aTypeOfEvent )
       
   211     {
       
   212     RDesReadStream rs;
       
   213     rs.Open( aEventData ); // CSI: 65 #
       
   214     CleanupClosePushL( rs );
       
   215 
       
   216     TInt32 eventIfId = rs.ReadInt32L();
       
   217     CXIMPApiEventBase* eventObject = NewEventObjectFromStreamLC( eventIfId, rs );
       
   218     
       
   219     if ( eventObject )
       
   220         {
       
   221         CleanupStack::Pop(); //eventObject    
       
   222         }
       
   223         
       
   224     CleanupStack::PopAndDestroy(); // rs
       
   225 
       
   226 
       
   227     aTypeOfEvent = eventIfId;
       
   228     return eventObject;
       
   229     }
       
   230 
       
   231 
       
   232 // End of file
       
   233