wvsettings20/IMPSSrc/IMPSSAPObjectHandler.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 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: Object handler implementation
       
    15 *
       
    16 */
       
    17 
       
    18 //  INCLUDES
       
    19 #include    "IMPSSAPObjectHandler.h"
       
    20 #include    <s32mem.h>
       
    21 
       
    22 // size to use if the source object size is 0 in copy
       
    23 const TInt KTargetObjectSize = 200;
       
    24 
       
    25 // ================= LOCAL FUNCTIONS =======================
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // ImplementationVersionsLeft()
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 TBool ImplementationVersionsLeft( const MIMPSSapObject& aObj, TInt aVersionID )
       
    32     {
       
    33     return ( aVersionID < aObj.ObjectVersion() );
       
    34     }
       
    35 
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // WriteZeroVersionMarkL()
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 TStreamPos WriteZeroExtensionMarkL( RWriteStream& aStream )
       
    42     {
       
    43     //Write extension mark & return its position
       
    44     TStreamPos extMarkPos = aStream.Sink()->TellL( MStreamBuf::EWrite );
       
    45     aStream.WriteInt32L( 0 ); //Zero length extension
       
    46     return extMarkPos;
       
    47     }
       
    48 
       
    49 
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // PatchVersionMarkL()
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 void PatchExtensionMarkL( const TStreamPos& aExtMarkPos,
       
    56                           RWriteStream& aStream,
       
    57                           TInt aExtensionLength )
       
    58     {
       
    59     aStream.Sink()->SeekL( MStreamBuf::EWrite, aExtMarkPos );
       
    60     aStream.WriteInt32L( aExtensionLength );
       
    61     }
       
    62 
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // ReadExtensionMarkL()
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 TInt ReadExtensionMarkL( RReadStream& aStream )
       
    69     {
       
    70     return aStream.ReadInt32L();
       
    71     }
       
    72 
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // ExternalizeVersionL()
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 TInt ExternalizeVersionL( const MIMPSSapObject& aObj, TInt aVersionID, RWriteStream& aStream )
       
    79     {
       
    80     TStreamPos versionStartPos = aStream.Sink()->TellL( MStreamBuf::EWrite );
       
    81 
       
    82     //Write version data & zero extension mark
       
    83     aObj.ExternalizeVersionDataL( aVersionID, aStream );
       
    84     TStreamPos extMarkPos = WriteZeroExtensionMarkL( aStream );
       
    85 
       
    86     //Calculate version length
       
    87     TInt extensionLength = 0;
       
    88     TInt versionLenght = aStream.Sink()->TellL( MStreamBuf::EWrite ) -
       
    89                          versionStartPos;
       
    90 
       
    91 
       
    92     //Recurse to next version if needed & patch
       
    93     //the extension mark to contain cumulative length
       
    94     //all data stored during recursion
       
    95     if ( ImplementationVersionsLeft( aObj, aVersionID ) )
       
    96         {
       
    97         extensionLength = ExternalizeVersionL( aObj, ++aVersionID, aStream );
       
    98         PatchExtensionMarkL( extMarkPos, aStream, extensionLength );
       
    99         }
       
   100 
       
   101 
       
   102     return versionLenght + extensionLength;
       
   103     }
       
   104 
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // InternalizeVersionL()
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void InternalizeVersionL( MIMPSSapObject& aObj,
       
   111                           TInt aVersionID,
       
   112                           RReadStream& aStream )
       
   113     {
       
   114     //Three cases:
       
   115     // 1. Stream has same versions than implementaion.
       
   116     // 2. Stream is missing data version compared to the implementation.
       
   117     // 3. Stream has higher data version than implementation supports.
       
   118 
       
   119     aObj.InternalizeVersionDataL( aVersionID, aStream );
       
   120 
       
   121     TInt extensionLength = ReadExtensionMarkL( aStream );
       
   122 
       
   123     //Proceed if stream has subsequent extensions
       
   124     if ( extensionLength > 0 )
       
   125         {
       
   126         if ( ImplementationVersionsLeft( aObj, aVersionID ) )
       
   127             {
       
   128             //Current implementation is capable to read more versions
       
   129             //==> recurse to next one
       
   130             InternalizeVersionL( aObj, ++aVersionID, aStream );
       
   131             }
       
   132         else
       
   133             {
       
   134             //Implementation isn't capable to read more version
       
   135             //==> consume remaining extension blocks
       
   136             aStream.ReadL( extensionLength );
       
   137             }
       
   138         }
       
   139     }
       
   140 
       
   141 
       
   142 
       
   143 
       
   144 
       
   145 
       
   146 
       
   147 // ================= MEMBER FUNCTIONS =======================
       
   148 
       
   149 
       
   150 
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // IMPSSAPObjectHandler::DataSize()
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 TInt IMPSSAPObjectHandler::DataSize( const MIMPSSapObject& aObj )
       
   157     {
       
   158     // Object id: 4 bytes
       
   159     // Object version: 4 bytes
       
   160     TInt dataSize = 8;
       
   161 
       
   162 
       
   163     //For each version: version extension mark, 4 bytes
       
   164     TInt versionCount = aObj.ObjectVersion() - KIMPSSapObjVersionInitial;
       
   165     dataSize += ( versionCount * 4 );
       
   166 
       
   167 
       
   168     //and actual data size
       
   169     dataSize += aObj.DataSize();
       
   170 
       
   171 
       
   172     return dataSize;
       
   173     }
       
   174 
       
   175 
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // IMPSSAPObjectHandler::ExternalizeL()
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 void IMPSSAPObjectHandler::ExternalizeL( const MIMPSSapObject& aObj,
       
   182                                          RWriteStream& aStream )
       
   183     {
       
   184     //Externalize object ID's
       
   185     aStream.WriteInt32L( aObj.ObjectID() );
       
   186     aStream.WriteInt32L( aObj.ObjectVersion() );
       
   187 
       
   188 
       
   189     TStreamPos pos = aStream.Sink()->TellL( MStreamBuf::EWrite );
       
   190 
       
   191     //Recursively go through all data versions
       
   192     TInt length = ExternalizeVersionL( aObj, KIMPSSapObjVersionInitial, aStream );
       
   193 
       
   194     //And correct the stream write point
       
   195     aStream.Sink()->SeekL( MStreamBuf::EWrite, ( pos + length ) );
       
   196     }
       
   197 
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // IMPSSAPObjectHandler::InternalizeL()
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void IMPSSAPObjectHandler::InternalizeL( MIMPSSapObject& aObj, RReadStream& aStream )
       
   204     {
       
   205     aObj.Reset();
       
   206 
       
   207     //Object ID
       
   208     TIMPSSapObjectID id = static_cast<TIMPSSapObjectID> ( aStream.ReadInt32L() );
       
   209     if ( id != aObj.ObjectID() )
       
   210         {
       
   211         User::Leave( KErrCorrupt );
       
   212         }
       
   213 
       
   214     //Object version
       
   215     aStream.ReadInt32L();
       
   216 
       
   217     //and recursively go through all available version datas
       
   218     InternalizeVersionL( aObj, KIMPSSapObjVersionInitial, aStream );
       
   219     }
       
   220 
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // IMPSSAPObjectHandler::CopyL()
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 void IMPSSAPObjectHandler::CopyL( const MIMPSSapObject& aSource, MIMPSSapObject& aTarget )
       
   227     {
       
   228     //Don't check here object ID's because underlying stream
       
   229     //implementations might be capable to convert objects to another
       
   230 
       
   231     TInt objectSize = IMPSSAPObjectHandler::DataSize( aSource );
       
   232     if ( objectSize == 0 )
       
   233         {
       
   234         objectSize = KTargetObjectSize;
       
   235         }
       
   236 
       
   237     CBufSeg* buffer = CBufSeg::NewL( objectSize );
       
   238     CleanupStack::PushL( buffer );
       
   239 
       
   240     RBufWriteStream wstream;
       
   241     wstream.Open( *buffer );
       
   242     CleanupClosePushL( wstream );
       
   243 
       
   244     IMPSSAPObjectHandler::ExternalizeL( aSource, wstream );
       
   245     CleanupStack::PopAndDestroy(); //wstream
       
   246 
       
   247     RBufReadStream rstream;
       
   248     rstream.Open( *buffer );
       
   249     CleanupClosePushL( rstream );
       
   250 
       
   251     IMPSSAPObjectHandler::InternalizeL( aTarget, rstream );
       
   252 
       
   253     CleanupStack::PopAndDestroy( 2 ); //rstream, buffer
       
   254     }
       
   255 
       
   256 
       
   257 
       
   258 //  End of File