epoc32/include/mw/senpointermap.h
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Final list of Symbian^2 public API header files
     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 the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: Pointermap class declaration        
    15 *
    16 */
    17 
    18 #ifndef POINTERMAP_H
    19 #define POINTERMAP_H
    20 
    21 // INCLUDES
    22 #include <e32std.h>
    23 
    24 // CLASS DECLARATION
    25 template <typename K, typename V>
    26 class RSenPointerMap
    27     {
    28     public: // Constructors and destructor
    29         
    30     RSenPointerMap(TBool aTakeOwnershipKey, TBool aTakeOwnershipValue)
    31         :
    32         iTakeOwnershipKey(aTakeOwnershipKey),
    33         iTakeOwnershipValue(aTakeOwnershipValue)
    34         {}
    35 
    36     ~RSenPointerMap()
    37         {
    38         Reset();
    39         }
    40 
    41         // New functions
    42         
    43     TInt Append(const K* aKey, const V* aValue)
    44         {
    45         TInt err = iKeys.Append(aKey);
    46         if (err == KErrNone)
    47             {
    48             err = iValues.Append(aValue);
    49             if (err != KErrNone)
    50                 {
    51                 // last element of iKeys should be removed
    52                 TInt lastElementIndex = iKeys.Count() - 1;
    53                 iKeys.Remove(lastElementIndex);
    54                 }
    55             }
    56         return err;
    57         }
    58 
    59     TInt Find(const K& aKey) const
    60         {
    61         TInt index = KErrNotFound;
    62         for (int i = 0; i < iKeys.Count(); i++)
    63             {
    64             if (*iKeys[i] == aKey)
    65                 {
    66                 index = i;
    67                 break;
    68                 }
    69             }
    70         return index;
    71         }
    72 
    73     // @return the index of removed key-value pair, or
    74     // KErrNotFound, if such key was not found
    75     TInt RemoveByKey(const K& aKey)
    76         {
    77         TInt index = Find(aKey);
    78         if (index != KErrNotFound)
    79             {
    80             if(iTakeOwnershipKey)
    81                 {
    82                 K* key = KeyAt(index);
    83                 delete key;
    84                 }
    85             if(iTakeOwnershipValue)
    86                 {
    87                 V* value = iValues[index];
    88                 delete value;
    89                 }
    90             iKeys.Remove(index);
    91             iValues.Remove(index);
    92             }
    93         return index;
    94         }
    95 
    96     // @return the index of removed key-value pair, or
    97     // KErrNotFound, if such key was not found
    98     TInt Remove(const V& aValue)
    99         {
   100         TInt index = FindValue(aValue);
   101         if (index != KErrNotFound)
   102             {
   103             if (iTakeOwnershipValue)
   104                 {
   105                 V* value = iValues[index];
   106                 delete value;
   107                 }
   108             if (iTakeOwnershipKey)
   109                 {
   110                 K* key = iKeys[index];
   111                 delete key;
   112                 }
   113             iValues.Remove(index);
   114             iKeys.Remove(index);
   115             }
   116         return index; 
   117         }
   118 
   119     TInt FindValue(const V& aValue) const
   120         {
   121         TInt index = KErrNotFound;
   122         for (int i = 0; i < iValues.Count(); i++)
   123             {
   124             if ((iValues[i]) && (*iValues[i] == aValue))
   125                 {
   126                 index = i;
   127                 break;
   128                 }
   129             }
   130         return index;
   131         }
   132 
   133 
   134     // Note: deletes the current value of this key
   135     TInt UpdateValue(const K* aKey, const V* aValue)
   136         {
   137         TInt index=Find(*aKey);
   138         if (index==KErrNotFound)
   139             {
   140             return Append(aKey, aValue);
   141             }
   142 
   143         V* bValue=iValues[index];
   144         if (iTakeOwnershipValue)
   145             {
   146             // Since OWNED value is going to be replaced with aValue,
   147             // destroy old value instance first:
   148             delete bValue;  
   149             }
   150 
   151         iValues[index]=(V*)aValue;
   152         return KErrNone;
   153         }
   154 
   155 
   156     K* KeyAt(TInt aIndex)
   157         {
   158         return iKeys[aIndex];
   159         }
   160 
   161     const V* ValueAt(TInt aIndex) const
   162         {
   163         return iValues[aIndex];
   164         }
   165 
   166     TInt Count() const
   167         {
   168         return iKeys.Count();
   169         }
   170 
   171     void Reset()
   172         {
   173         if ( iTakeOwnershipKey )
   174             {
   175             iKeys.ResetAndDestroy();
   176             }
   177         else
   178             {
   179             iKeys.Reset();
   180             }
   181 
   182         if ( iTakeOwnershipValue )
   183             {
   184             iValues.ResetAndDestroy();
   185             }
   186         else
   187             {
   188             iValues.Reset();
   189             }
   190         }
   191         
   192     TInt Insert(const K* aKey, const V* aValue)
   193         {
   194         TInt count=iKeys.Count();
   195         TInt err=KErrNone;
   196 		TBool inserted=EFalse;
   197 
   198         for(TInt i=0; i<count; i++)
   199         	{
   200         	 if(*iKeys[i] >= *aKey)
   201         	 {
   202 	        err = iKeys.Insert(aKey, i);
   203 	        if (err == KErrNone)
   204 	            {
   205 	            err = iValues.Insert(aValue, i);
   206 	            if (err != KErrNone)
   207 	                {
   208 	                // inserted element of iKeys should be removed
   209 	                iKeys.Remove(i);
   210 	                }
   211 				    else
   212 					{
   213 					inserted=ETrue;
   214 					}
   215             	}
   216 			break;  	 	
   217         	 }
   218 			}
   219 
   220         if(!inserted)
   221         	{
   222 	        err = iKeys.Append(aKey);
   223 	        if (err == KErrNone)
   224 	            {
   225 	            err = iValues.Append(aValue);
   226 	            if (err != KErrNone)
   227 	                {
   228 	                // last element of iKeys should be removed
   229 	                TInt lastElementIndex = iKeys.Count() - 1;
   230 	                iKeys.Remove(lastElementIndex);
   231 	                }
   232             	}
   233         	}
   234 	return err;
   235         }
   236         
   237     private: // Data
   238     TBool iTakeOwnershipKey;
   239     TBool iTakeOwnershipValue;
   240     RPointerArray<K> iKeys;
   241     RPointerArray<V> iValues;
   242     };
   243 
   244 #endif // POINTERMAP_H
   245 
   246 // End of File
   247