websrv_pub/web_service_connection_api/inc/senpointermap.h
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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:    Pointermap class declaration        
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #ifndef POINTERMAP_H
       
    22 #define POINTERMAP_H
       
    23 
       
    24 // INCLUDES
       
    25 #include <e32std.h>
       
    26 
       
    27 // CLASS DECLARATION
       
    28 template <typename K, typename V>
       
    29 class RSenPointerMap
       
    30     {
       
    31     public: // Constructors and destructor
       
    32         
       
    33     RSenPointerMap(TBool aTakeOwnershipKey, TBool aTakeOwnershipValue)
       
    34         :
       
    35         iTakeOwnershipKey(aTakeOwnershipKey),
       
    36         iTakeOwnershipValue(aTakeOwnershipValue)
       
    37         {}
       
    38 
       
    39     ~RSenPointerMap()
       
    40         {
       
    41         Reset();
       
    42         }
       
    43 
       
    44         // New functions
       
    45         
       
    46     TInt Append(const K* aKey, const V* aValue)
       
    47         {
       
    48         TInt err = iKeys.Append(aKey);
       
    49         if (err == KErrNone)
       
    50             {
       
    51             err = iValues.Append(aValue);
       
    52             if (err != KErrNone)
       
    53                 {
       
    54                 // last element of iKeys should be removed
       
    55                 TInt lastElementIndex = iKeys.Count() - 1;
       
    56                 iKeys.Remove(lastElementIndex);
       
    57                 }
       
    58             }
       
    59         return err;
       
    60         }
       
    61 
       
    62     TInt Find(const K& aKey) const
       
    63         {
       
    64         TInt index( KErrNotFound );
       
    65         for (TInt i = 0; i < iKeys.Count(); i++)
       
    66             {
       
    67             if (*iKeys[i] == aKey)
       
    68                 {
       
    69                 index = i;
       
    70                 break;
       
    71                 }
       
    72             }
       
    73         return index;
       
    74         }
       
    75 
       
    76     TInt FindReverse(const K& aKey) const
       
    77         {
       
    78         TInt index( KErrNotFound );
       
    79         TInt count( iKeys.Count() );
       
    80         for (TInt i = count-1; i >=0 ; i--)
       
    81             {
       
    82             if (*iKeys[i] == aKey)
       
    83                 {
       
    84                 index = i;
       
    85                 break;
       
    86                 }
       
    87             }
       
    88         return index;
       
    89         }
       
    90         
       
    91         
       
    92     TInt RemoveAt( TInt aIndex ) 
       
    93         {
       
    94         K* key = KeyAt( aIndex );
       
    95     	return RemoveByKey( *key );
       
    96     	}
       
    97 
       
    98     // @return the index of removed key-value pair, or
       
    99     // KErrNotFound, if such key was not found
       
   100     TInt RemoveByKey(const K& aKey)
       
   101         {
       
   102         TInt index = Find(aKey);
       
   103         if (index != KErrNotFound)
       
   104             {
       
   105             if(iTakeOwnershipKey)
       
   106                 {
       
   107                 K* key = KeyAt(index);
       
   108                 delete key;
       
   109                 }
       
   110             if(iTakeOwnershipValue)
       
   111                 {
       
   112                 V* value = iValues[index];
       
   113                 delete value;
       
   114                 }
       
   115             iKeys.Remove(index);
       
   116             iValues.Remove(index);
       
   117             }
       
   118         return index;
       
   119         }
       
   120 
       
   121     // @return the index of removed key-value pair, or
       
   122     // KErrNotFound, if such key was not found
       
   123     TInt Remove(const V& aValue)
       
   124         {
       
   125         TInt index = FindValue(aValue);
       
   126         if (index != KErrNotFound)
       
   127             {
       
   128             if (iTakeOwnershipValue)
       
   129                 {
       
   130                 V* value = iValues[index];
       
   131                 delete value;
       
   132                 }
       
   133             if (iTakeOwnershipKey)
       
   134                 {
       
   135                 K* key = iKeys[index];
       
   136                 delete key;
       
   137                 }
       
   138             iValues.Remove(index);
       
   139             iKeys.Remove(index);
       
   140             }
       
   141         return index; 
       
   142         }
       
   143 
       
   144     TInt FindValue(const V& aValue) const
       
   145         {
       
   146         TInt index = KErrNotFound;
       
   147         for (TInt i = 0; i < iValues.Count(); i++)
       
   148             {
       
   149             if ((iValues[i]) && (*iValues[i] == aValue))
       
   150                 {
       
   151                 index = i;
       
   152                 break;
       
   153                 }
       
   154             }
       
   155         return index;
       
   156         }
       
   157 
       
   158 
       
   159     // Note: deletes the current value of this key
       
   160     TInt UpdateValue(const K* aKey, const V* aValue)
       
   161         {
       
   162         TInt index=Find(*aKey);
       
   163         if (index==KErrNotFound)
       
   164             {
       
   165             return Append(aKey, aValue);
       
   166             }
       
   167 
       
   168         V* bValue=iValues[index];
       
   169         if (iTakeOwnershipValue)
       
   170             {
       
   171             // Since OWNED value is going to be replaced with aValue,
       
   172             // destroy old value instance first:
       
   173             delete bValue;  
       
   174             }
       
   175 
       
   176         iValues[index]=(V*)aValue;
       
   177         return KErrNone;
       
   178         }
       
   179 
       
   180 
       
   181     K* KeyAt(TInt aIndex)
       
   182         {
       
   183         return iKeys[aIndex];
       
   184         }
       
   185 
       
   186     const V* ValueAt(TInt aIndex) const
       
   187         {
       
   188         return iValues[aIndex];
       
   189         }
       
   190 
       
   191     TInt Count() const
       
   192         {
       
   193         return iKeys.Count();
       
   194         }
       
   195 
       
   196     void Reset()
       
   197         {
       
   198         if ( iTakeOwnershipKey )
       
   199             {
       
   200             iKeys.ResetAndDestroy();
       
   201             }
       
   202         else
       
   203             {
       
   204             iKeys.Reset();
       
   205             }
       
   206 
       
   207         if ( iTakeOwnershipValue )
       
   208             {
       
   209             iValues.ResetAndDestroy();
       
   210             }
       
   211         else
       
   212             {
       
   213             iValues.Reset();
       
   214             }
       
   215         }
       
   216         
       
   217     TInt Insert(const K* aKey, const V* aValue)
       
   218         {
       
   219         TInt count=iKeys.Count();
       
   220         TInt err=KErrNone;
       
   221 		TBool inserted=EFalse;
       
   222 
       
   223         for(TInt i=0; i<count; i++)
       
   224         	{
       
   225         	 if(*iKeys[i] >= *aKey)
       
   226         	 {
       
   227 	        err = iKeys.Insert(aKey, i);
       
   228 	        if (err == KErrNone)
       
   229 	            {
       
   230 	            err = iValues.Insert(aValue, i);
       
   231 	            if (err != KErrNone)
       
   232 	                {
       
   233 	                // inserted element of iKeys should be removed
       
   234 	                iKeys.Remove(i);
       
   235 	                }
       
   236 				    else
       
   237 					{
       
   238 					inserted=ETrue;
       
   239 					}
       
   240             	}
       
   241 			break;  	 	
       
   242         	 }
       
   243 			}
       
   244 
       
   245         if(!inserted)
       
   246         	{
       
   247 	        err = iKeys.Append(aKey);
       
   248 	        if (err == KErrNone)
       
   249 	            {
       
   250 	            err = iValues.Append(aValue);
       
   251 	            if (err != KErrNone)
       
   252 	                {
       
   253 	                // last element of iKeys should be removed
       
   254 	                TInt lastElementIndex = iKeys.Count() - 1;
       
   255 	                iKeys.Remove(lastElementIndex);
       
   256 	                }
       
   257             	}
       
   258         	}
       
   259 	return err;
       
   260         }
       
   261         
       
   262     private: // Data
       
   263     TBool iTakeOwnershipKey;
       
   264     TBool iTakeOwnershipValue;
       
   265     RPointerArray<K> iKeys;
       
   266     RPointerArray<V> iValues;
       
   267     };
       
   268 
       
   269 #endif // POINTERMAP_H
       
   270 
       
   271 // End of File
       
   272