ncdengine/inc/catalogsutils.inl
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006-2008 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:   Contains definition of catalogsutils functions 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "catalogsdebug.h"
       
    20 
       
    21 // ---------------------------------------------------------------------------
       
    22 // IntToDes16
       
    23 // 64-bit specialization is in catalogsutils.cpp
       
    24 // ---------------------------------------------------------------------------
       
    25 //   
       
    26 template<typename T>
       
    27 void IntToDes16( const T& aInt, TDes& aDes )
       
    28     {
       
    29     DLTRACEIN(("aDes.MaxLength() = %d", aDes.MaxLength() ));
       
    30     DASSERT( aDes.MaxLength() >= sizeof(T)/2 );    
       
    31     *(( T* )aDes.Ptr()) = aInt;        
       
    32     }
       
    33     
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // Des16ToInt
       
    37 // 64-bit specialization is in catalogsutils.cpp
       
    38 // ---------------------------------------------------------------------------
       
    39 //   
       
    40 template<typename T>    
       
    41 void Des16ToInt( const TDesC& aDes, T& aInt )
       
    42     {
       
    43     DLTRACEIN(( _L("Des: %S"), &aDes ));
       
    44     aInt = *(( T* )aDes.Ptr());
       
    45     DLTRACEOUT(( "Result: %d", aInt ));
       
    46     }
       
    47     
       
    48 
       
    49 
       
    50 
       
    51 // Template class CleanupResetAndDestroy
       
    52 template <typename T>
       
    53 inline void CleanupResetAndDestroy<T>::PushL(T& aRef)
       
    54 /**
       
    55 Creates a TCleanupItem for the specified object.
       
    56 
       
    57 The cleanup operation is the private static function Close() of this class.
       
    58 
       
    59 @param aRef The object for which a TCleanupItem is to be constructed.
       
    60 */
       
    61 	{CleanupStack::PushL(TCleanupItem(&ResetAndDestroy,&aRef));}
       
    62 
       
    63 
       
    64 
       
    65 
       
    66 template <typename T>
       
    67 void CleanupResetAndDestroy<T>::ResetAndDestroy(TAny *aPtr)
       
    68 /**
       
    69 The cleanup operation to be performed.
       
    70 
       
    71 @param aPtr A pointer to the object for which clean up is to be performed. 
       
    72             The implementation calls Close() on this object.
       
    73 */
       
    74 	{(STATIC_CAST(T*,aPtr))->ResetAndDestroy();}
       
    75 
       
    76 
       
    77 
       
    78 
       
    79 // See header file e32base.h for in-source comment.
       
    80 template <typename T>
       
    81 inline void CleanupResetAndDestroyPushL(T& aRef)
       
    82 	{CleanupResetAndDestroy<T>::PushL(aRef);}
       
    83 	
       
    84 
       
    85 // Template class CleanupInternalRelease
       
    86 template <typename T>
       
    87 inline void CleanupInternalRelease<T>::PushL(T& aRef)
       
    88 /**
       
    89 Creates a TCleanupItem for the specified object.
       
    90 
       
    91 The cleanup operation is the private static function Close() of this class.
       
    92 
       
    93 @param aRef The object for which a TCleanupItem is to be constructed.
       
    94 */
       
    95 	{CleanupStack::PushL(TCleanupItem(&InternalRelease,&aRef));}
       
    96 
       
    97 
       
    98 template <typename T>
       
    99 void CleanupInternalRelease<T>::InternalRelease(TAny *aPtr)
       
   100 /**
       
   101 The cleanup operation to be performed.
       
   102 
       
   103 @param aPtr A pointer to the object for which clean up is to be performed. 
       
   104             The implementation calls Close() on this object.
       
   105 */
       
   106 	{(STATIC_CAST(T*,aPtr))->InternalRelease();}
       
   107 	
       
   108 
       
   109 template <typename T>
       
   110 inline void CleanupInternalReleasePushL(T& aRef)
       
   111 	{CleanupInternalRelease<T>::PushL(aRef);}
       
   112 
       
   113 template <typename T>
       
   114 void ResetAndCloseArray( RPointerArray<T>& aArray )
       
   115     {
       
   116     TInt count( aArray.Count() );    
       
   117     while ( count-- )
       
   118         {
       
   119         aArray[count]->Close();                    
       
   120         }
       
   121     aArray.Reset();
       
   122     }
       
   123 
       
   124 
       
   125 template <typename T>
       
   126 void ResetAndCloseArray( RArray<T>& aArray )
       
   127     {
       
   128     TInt count( aArray.Count() );    
       
   129     while ( count-- )
       
   130         {
       
   131         aArray[count].Close();                    
       
   132         }
       
   133     aArray.Reset();
       
   134     }
       
   135     
       
   136     
       
   137 template <typename Casted, typename T>
       
   138 void ResetAndDestroyWithCast( RPointerArray<T>& aArray )
       
   139     {
       
   140     TInt count = aArray.Count();
       
   141     for ( TInt i = 0; i < count; i++ )
       
   142         {
       
   143         Casted* info =
       
   144             static_cast<Casted*>( aArray[i] );
       
   145         delete info;
       
   146         info = NULL;
       
   147         }
       
   148     aArray.Reset();        
       
   149     }
       
   150     
       
   151 
       
   152 template <typename T>
       
   153 void DeleteFromArray( RPointerArray<T>& aArray, TInt aIndex )
       
   154     {
       
   155     delete aArray[ aIndex ];
       
   156     aArray.Remove( aIndex );
       
   157     }
       
   158 
       
   159 template <typename Casted, typename T>
       
   160 void DeleteFromArray( RPointerArray<T>& aArray, TInt aIndex )
       
   161     {
       
   162     delete static_cast<Casted*>( aArray[ aIndex ] );
       
   163     aArray.Remove( aIndex );    
       
   164     }
       
   165 
       
   166     
       
   167 template <typename EnumType>
       
   168 void InternalizeEnumL( EnumType& aTarget, RReadStream& aReadStream )
       
   169     {
       
   170     aTarget = static_cast<EnumType>( aReadStream.ReadInt32L() );
       
   171     }
       
   172 
       
   173 
       
   174 template <typename EnumType>
       
   175 void ExternalizeEnumL( const EnumType& aSource, RWriteStream& aWriteStream )
       
   176     {
       
   177     aWriteStream.WriteInt32L( aSource );
       
   178     }
       
   179     
       
   180 
       
   181 template <typename T>
       
   182 TBool IsOneOf( 
       
   183     const T& aData, 
       
   184     const T& aComp1, 
       
   185     const T& aComp2 )
       
   186     {
       
   187     return aData == aComp1 || 
       
   188            aData == aComp2;
       
   189     }
       
   190 
       
   191 
       
   192 template <typename T>
       
   193 TBool IsOneOf( 
       
   194     const T& aData, 
       
   195     const T& aComp1, 
       
   196     const T& aComp2, 
       
   197     const T& aComp3 )
       
   198     {
       
   199     return IsOneOf( aData, aComp1, aComp2 ) || 
       
   200            aData == aComp3;
       
   201     }
       
   202 
       
   203 
       
   204 template <typename T>
       
   205 TBool IsOneOf( 
       
   206     const T& aData, 
       
   207     const T& aComp1, 
       
   208     const T& aComp2, 
       
   209     const T& aComp3, 
       
   210     const T& aComp4 )
       
   211     {
       
   212     return aData == aComp1 || 
       
   213            aData == aComp2 ||
       
   214            aData == aComp3 ||
       
   215            aData == aComp4;
       
   216     }
       
   217 
       
   218 
       
   219 template <typename T>
       
   220 void ReleasePtr( T*& aObject )
       
   221     {
       
   222     if ( aObject ) 
       
   223         {
       
   224         aObject->Release();
       
   225         aObject = NULL;
       
   226         }
       
   227     }
       
   228 
       
   229 
       
   230 template <typename T>
       
   231 void ClosePtr( T*& aObject )
       
   232     {
       
   233     if ( aObject ) 
       
   234         {
       
   235         aObject->Close();
       
   236         aObject = NULL;
       
   237         }
       
   238     }
       
   239 
       
   240 
       
   241 template <typename T>
       
   242 void DeletePtr( T*& aObject )
       
   243     {
       
   244     delete aObject;
       
   245     aObject = NULL;
       
   246     }
       
   247