uiservicetabsettings/src/cvimpstsettingskeyvaluepairs.cpp
branchRCL_3
changeset 28 3104fc151679
parent 27 2b7283837edb
child 29 9a48e301e94b
equal deleted inserted replaced
27:2b7283837edb 28:3104fc151679
     1 /*
       
     2 * Copyright (c) 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:  Container for individual key-value pairs
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include    "cvimpstsettingskeyvaluepairs.h"
       
    20 #include    "cvimpstsettingskeyvaluepair.h"
       
    21 #include    "cvimpstsettingslookupkeyvaluepair.h"
       
    22 
       
    23 #include    <e32std.h>
       
    24 
       
    25 //CONSTANTS
       
    26 const TInt KKeyValuePairGranurality = 5;
       
    27 
       
    28 
       
    29 // LOCAL CONSTANTS AND MACROS
       
    30 #define KEYWORD_ORDER TLinearOrder< CVIMPSTSettingsKeyValuePair > ( CompareKeys )
       
    31 
       
    32 
       
    33 // ============================ LOCAL FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CompareKeys()
       
    37 // Comparison function for TLinearOrder
       
    38 //
       
    39 // @param aLhs The left hand side in comparison.
       
    40 // @param aRhs The right hand side in comparison.
       
    41 // @return Result from compare
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 TInt CompareKeys( const CVIMPSTSettingsKeyValuePair& aLhs, const CVIMPSTSettingsKeyValuePair& aRhs )
       
    45     {
       
    46     return aLhs.Key().Compare( aRhs.Key() );
       
    47     }
       
    48  
       
    49 // -----------------------------------------------------------------------------
       
    50 // SetValueToPairs()
       
    51 // Templated algorithm to set or update new value to list of key values.
       
    52 //
       
    53 // @param aPairs The list of key-value pairs.
       
    54 // @param aLookupKey The lookup key to use in searching.
       
    55 // @param aKey The key for which to assign the value.
       
    56 // @param aValue Templated data type to assign to key.
       
    57 // @return Error code.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 template< typename T >
       
    61 TInt SetValueToPairs( RPointerArray< CVIMPSTSettingsKeyValuePair >& aPairs,
       
    62                       CVIMPSTSettingsLookupKeyValuePair& aLookupKey,
       
    63                       const TDesC& aKey, 
       
    64                       const T& aValue )
       
    65     {
       
    66     aLookupKey.SetLookupKey( aKey );
       
    67     
       
    68 
       
    69     TInt err = KErrNone;
       
    70     TInt index = aPairs.FindInOrder( &aLookupKey, KEYWORD_ORDER );
       
    71     
       
    72     if( index != KErrNotFound )
       
    73         {
       
    74         //Update existing
       
    75         err = aPairs[ index ]->SetValue( aValue ); 
       
    76         }
       
    77 
       
    78     else
       
    79         {
       
    80         //create new
       
    81         CVIMPSTSettingsKeyValuePair* newPair = CVIMPSTSettingsKeyValuePair::New( aKey ); 
       
    82         if( !newPair )
       
    83             {
       
    84             err = KErrNoMemory;
       
    85             }
       
    86         else
       
    87             {
       
    88             //Insert new 
       
    89             
       
    90             err = newPair->SetValue( aValue );
       
    91 			if( err == KErrNone )
       
    92    				{
       
    93    				err = aPairs.InsertInOrderAllowRepeats( newPair, KEYWORD_ORDER ); 
       
    94   				 }
       
    95 
       
    96 			if( err != KErrNone )
       
    97    				{
       
    98    				delete newPair;
       
    99    				}
       
   100 
       
   101         	}
       
   102         }
       
   103     aLookupKey.ResetLookupKey();
       
   104     return err;
       
   105     }
       
   106 
       
   107 
       
   108 
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // GetValueFromPairs()
       
   112 // 
       
   113 // Templated algorithm to get value from list of key values.
       
   114 //
       
   115 // @param aPairs The list of key-value pairs.
       
   116 // @param aLookupKey The lookup key to use in searching.
       
   117 // @param aKey The key for which to get the value.
       
   118 // @param aValue Templated data type to get.
       
   119 // @return Error code.
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 template< typename T >
       
   123 TInt GetValueFromPairs( const RPointerArray< CVIMPSTSettingsKeyValuePair >& aPairs,
       
   124                         CVIMPSTSettingsLookupKeyValuePair& aLookupKey,
       
   125                         const TDesC& aKey, 
       
   126                         T& aValue ) 
       
   127     {
       
   128     aLookupKey.SetLookupKey( aKey );
       
   129 
       
   130 
       
   131     TInt index = aPairs.FindInOrder( &aLookupKey, KEYWORD_ORDER );
       
   132     if( index != KErrNotFound )
       
   133         {
       
   134         index = aPairs[ index ]->GetValue( aValue ); 
       
   135         }
       
   136 
       
   137     
       
   138     aLookupKey.ResetLookupKey();
       
   139     return index;
       
   140     }
       
   141 
       
   142 // ================= MEMBER FUNCTIONS =======================
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CVIMPSTUiSAPKeyValuePairs::NewL()
       
   146 // Two-phased constructor.
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 CVIMPSTSettingsKeyValuePairs* CVIMPSTSettingsKeyValuePairs::NewL()
       
   150     {
       
   151     CVIMPSTSettingsKeyValuePairs* self = new (ELeave) CVIMPSTSettingsKeyValuePairs;
       
   152     CleanupStack::PushL( self );
       
   153     self->ConstructL();
       
   154     CleanupStack::Pop(); //self
       
   155     return self;
       
   156     }
       
   157 
       
   158 
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CVIMPSTSettingsKeyValuePairs::~CVIMPSTSettingsKeyValuePairs()
       
   162 // Destructor
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 CVIMPSTSettingsKeyValuePairs::~CVIMPSTSettingsKeyValuePairs()
       
   166     {
       
   167     iPairs.ResetAndDestroy();
       
   168     delete iLookupKey;
       
   169     }
       
   170 
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CVIMPSTSettingsKeyValuePairs::CVIMPSTSettingsKeyValuePairs()
       
   174 // C++ default constructor can NOT contain any code, that
       
   175 // might leave.
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 CVIMPSTSettingsKeyValuePairs::CVIMPSTSettingsKeyValuePairs()
       
   179     : iPairs( KKeyValuePairGranurality )
       
   180     {
       
   181     }
       
   182 
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CVIMPSTSettingsKeyValuePairs::ConstructL)
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CVIMPSTSettingsKeyValuePairs::ConstructL()
       
   189     {
       
   190     iLookupKey = CVIMPSTSettingsLookupKeyValuePair::NewL();
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CVIMPSTSettingsKeyValuePairs::SetValueDesC16()
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 TInt CVIMPSTSettingsKeyValuePairs::SetValueDesC16( const TDesC& aKey, const TDesC16& aValue )
       
   198     {
       
   199     return SetValueToPairs( iPairs, *iLookupKey, aKey, aValue );
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CVIMPSTSettingsKeyValuePairs::GetValueDesC16()
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 TInt CVIMPSTSettingsKeyValuePairs::GetValueDesC16( const TDesC& aKey, TPtrC16& aValue )
       
   207     {
       
   208     return GetValueFromPairs( iPairs, *iLookupKey, aKey, aValue );
       
   209     }
       
   210 
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CVIMPSTSettingsKeyValuePairs::DeletePair()
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 TInt CVIMPSTSettingsKeyValuePairs::DeletePair( const TDesC& aKey )
       
   217     {    
       
   218     iLookupKey->SetLookupKey( aKey );
       
   219     
       
   220     TInt index = iPairs.FindInOrder( iLookupKey, KEYWORD_ORDER );
       
   221     if( index != KErrNotFound )
       
   222         {
       
   223         delete iPairs[ index ]; 
       
   224         iPairs.Remove( index );
       
   225         index = KErrNone;
       
   226         }
       
   227      
       
   228     iLookupKey->ResetLookupKey();
       
   229     
       
   230     return index;
       
   231     }
       
   232 
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CVIMPSTSettingsKeyValuePairs::Pairs()
       
   236 // -----------------------------------------------------------------------------
       
   237 //
       
   238 RPointerArray< CVIMPSTSettingsKeyValuePair >& CVIMPSTSettingsKeyValuePairs::Pairs() 
       
   239     {
       
   240     return iPairs;
       
   241     }
       
   242 
       
   243 //  End of File
       
   244