javamanager/javaregistry/legacy/installedappsregistry/inc/arrayutils.h
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2003 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:  Definition of various helper functions used for
       
    15 *                serialization.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef ARRAYUTILS_H
       
    21 #define ARRAYUTILS_H
       
    22 
       
    23 // INCLUDE FILES
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <s32strm.h>
       
    27 
       
    28 /////////////////////////////////////////////////////////////////////
       
    29 // RPointerArray utilities
       
    30 /////////////////////////////////////////////////////////////////////
       
    31 
       
    32 template <class T>
       
    33 void CopyPointerArrayL(RPointerArray<T>& aNew,
       
    34                        const RPointerArray<T>& aOld);
       
    35 
       
    36 template <class T>
       
    37 void ExternalizePointerArrayL(const RPointerArray<T>& aArray,
       
    38                               RWriteStream& aStream);
       
    39 
       
    40 template <class T>
       
    41 void InternalizePointerArrayL(RPointerArray<T>& aArray,
       
    42                               RReadStream& aStream);
       
    43 
       
    44 template <class T>
       
    45 TInt PointerArraySizeForStreamingL(const RPointerArray<T>& aArray);
       
    46 
       
    47 /////////////////////////////////////////////////////////////////////
       
    48 // RArray utilities
       
    49 /////////////////////////////////////////////////////////////////////
       
    50 
       
    51 template <class T>
       
    52 void CopyArrayL(RArray<T>& aNew, const RArray<T>& aOld);
       
    53 
       
    54 template <class T>
       
    55 void ExternalizeArrayL(const RArray<T>& aArray, RWriteStream& aStream);
       
    56 
       
    57 template <class T>
       
    58 void InternalizeArrayL(RArray<T>& aArray, RReadStream& aStream);
       
    59 
       
    60 template <class T>
       
    61 TInt ArraySizeForStreamingL(const RArray<T>& aArray);
       
    62 
       
    63 /////////////////////////////////////////////////////////////////////
       
    64 // Traits classes - internally required by RPointerArray functions
       
    65 /////////////////////////////////////////////////////////////////////
       
    66 
       
    67 template <class T>
       
    68 class TTraits
       
    69 {
       
    70 public:
       
    71     static T* CopyLC(const T& aOther)
       
    72     {
       
    73         return T::NewLC(aOther);
       
    74     }
       
    75 
       
    76     static T* ReadFromStreamLC(RReadStream& aStream)
       
    77     {
       
    78         return T::NewLC(aStream);
       
    79     }
       
    80 
       
    81     static void WriteToStreamL(const T& aItem,
       
    82                                RWriteStream& aStream)
       
    83     {
       
    84         aStream << aItem;
       
    85     }
       
    86 
       
    87     static TInt SizeForStreamingL(const T& aItem)
       
    88     {
       
    89         // Note: every possible 'T' must implement SizeForStreamingL method
       
    90         // in order for the next line to work.
       
    91         return aItem.SizeForStreamingL();
       
    92     }
       
    93 };
       
    94 
       
    95 // Specialisation for HBufs
       
    96 template <>
       
    97 class TTraits<HBufC16>
       
    98 {
       
    99 public:
       
   100     static HBufC16* CopyLC(const HBufC16& aOther)
       
   101     {
       
   102         return aOther.AllocLC();
       
   103     }
       
   104 
       
   105     static HBufC16* ReadFromStreamLC(RReadStream& aStream)
       
   106     {
       
   107         return HBufC16::NewLC(aStream, KMaxTInt);
       
   108     }
       
   109 
       
   110     static void WriteToStreamL(const HBufC16& aItem, RWriteStream& aStream)
       
   111     {
       
   112         aStream << aItem;
       
   113     }
       
   114 
       
   115     static TInt SizeForStreamingL(const HBufC16& aItem)
       
   116     {
       
   117         // Streaming a descriptor works as follows: first write out the length
       
   118         // of the descriptor. The size of this integer may vary depending on
       
   119         // the size of data stored, but it's at most == sizeof(TInt32).
       
   120         // Second, storing a Unicode string requires at most double space
       
   121         // compared to the 8-bit version, hence the multiplier of 2. Note that
       
   122         // a Unicode string may be compressed, too, (at least it's always
       
   123         // tried to be compressed), thus a '2*itemlength' seems like a valid
       
   124         // estimate for the size.
       
   125         return sizeof(TInt32) + 2 * aItem.Length();
       
   126     }
       
   127 };
       
   128 
       
   129 template <>
       
   130 class TTraits<HBufC8>
       
   131 {
       
   132 public:
       
   133     static HBufC8* CopyLC(const HBufC8& aOther)
       
   134     {
       
   135         return aOther.AllocLC();
       
   136     }
       
   137 
       
   138     static HBufC8* ReadFromStreamLC(RReadStream& aStream)
       
   139     {
       
   140         return HBufC8::NewLC(aStream, KMaxTInt);
       
   141     }
       
   142 
       
   143     static void WriteToStreamL(const HBufC8& aItem, RWriteStream& aStream)
       
   144     {
       
   145         aStream << aItem;
       
   146     }
       
   147 
       
   148     static TInt SizeForStreamingL(const HBufC8& aItem)
       
   149     {
       
   150         // Streaming a descriptor works as follows: first write out the length
       
   151         // of the descriptor. The size of this integer may vary depending on
       
   152         // the size of data stored, but it's at most == sizeof(TInt32).
       
   153         // Second, storing an 8-bit string requires exactly the same size of
       
   154         // the length of the string.
       
   155         return sizeof(TInt32) + aItem.Length();
       
   156     }
       
   157 };
       
   158 
       
   159 #include "ArrayUtils.inl"
       
   160 
       
   161 #endif // ARRAYUTILS_H