appfw/apparchitecture/apgrfx/apgcommonutils.h
branchRCL_3
changeset 19 924385140d98
equal deleted inserted replaced
18:0818dd463d41 19:924385140d98
       
     1 // Copyright (c) 2010 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 #ifndef __APGCOMMONUTILS_H__
       
    17 #define __APGCOMMONUTILS_H__
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <s32strm.h>
       
    21 #include <s32buf.h>
       
    22 #include<usif/scr/appregentries.h>
       
    23 
       
    24 
       
    25 /**
       
    26 Implementation of the MStreamBuf interface that throws away all
       
    27 data written to it but keeps track of how many bytes have been
       
    28 written to it.  It does not support reading.
       
    29 */
       
    30 class TNullBuf : public MStreamBuf
       
    31     {
       
    32 public:
       
    33     inline TNullBuf();
       
    34     inline TUint BytesWritten();
       
    35 private:
       
    36     inline virtual void DoWriteL(const TAny* aPtr,TInt aLength);
       
    37 private:
       
    38     TUint iBytesWritten;
       
    39     };
       
    40 
       
    41 /**
       
    42 A write stream that throws away all its input, but keeps track of how many
       
    43 bytes have been written to it.  It is used for determining the amount of
       
    44 memory needed to store externalised objects.
       
    45 */
       
    46 class RNullWriteStream : public RWriteStream
       
    47     {
       
    48 public:
       
    49     inline RNullWriteStream();
       
    50     inline TUint BytesWritten();
       
    51 private:
       
    52     TNullBuf iSink;
       
    53     };
       
    54 
       
    55 inline TNullBuf::TNullBuf() : iBytesWritten(0) 
       
    56     {
       
    57     }
       
    58 
       
    59 inline TUint TNullBuf::BytesWritten() 
       
    60     {
       
    61     return iBytesWritten;
       
    62     }
       
    63 
       
    64 inline void TNullBuf::DoWriteL(const TAny*,TInt aLength)
       
    65     {
       
    66     iBytesWritten += aLength;
       
    67     }
       
    68 
       
    69 inline RNullWriteStream::RNullWriteStream()
       
    70     {
       
    71     Attach(&iSink);
       
    72     }
       
    73 
       
    74 inline TUint RNullWriteStream::BytesWritten()
       
    75     {
       
    76     return iSink.BytesWritten();
       
    77     }
       
    78 
       
    79 template <class T>
       
    80 TInt GetObjectSizeL(T* aObject)
       
    81     {
       
    82     TInt size(0);
       
    83     if(aObject)
       
    84         {
       
    85         RNullWriteStream nullstream;
       
    86         CleanupClosePushL(nullstream);
       
    87         nullstream << *aObject;
       
    88         nullstream.CommitL();
       
    89         size = nullstream.BytesWritten();
       
    90         CleanupStack::PopAndDestroy(&nullstream);
       
    91         return size;
       
    92         }
       
    93     return -1;
       
    94     }
       
    95 
       
    96 #endif //__APGCOMMONUTILS_H__