epoc32/include/mw/senpointermap.h
branchSymbian2
changeset 2 2fe1408b6811
child 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/mw/senpointermap.h	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -0,0 +1,247 @@
     1.4 +/*
     1.5 +* Copyright (c) 2002-2005 Nokia Corporation and/or its subsidiary(-ies). 
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* 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
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: Pointermap class declaration        
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +#ifndef POINTERMAP_H
    1.22 +#define POINTERMAP_H
    1.23 +
    1.24 +// INCLUDES
    1.25 +#include <e32std.h>
    1.26 +
    1.27 +// CLASS DECLARATION
    1.28 +template <typename K, typename V>
    1.29 +class RSenPointerMap
    1.30 +    {
    1.31 +    public: // Constructors and destructor
    1.32 +        
    1.33 +    RSenPointerMap(TBool aTakeOwnershipKey, TBool aTakeOwnershipValue)
    1.34 +        :
    1.35 +        iTakeOwnershipKey(aTakeOwnershipKey),
    1.36 +        iTakeOwnershipValue(aTakeOwnershipValue)
    1.37 +        {}
    1.38 +
    1.39 +    ~RSenPointerMap()
    1.40 +        {
    1.41 +        Reset();
    1.42 +        }
    1.43 +
    1.44 +        // New functions
    1.45 +        
    1.46 +    TInt Append(const K* aKey, const V* aValue)
    1.47 +        {
    1.48 +        TInt err = iKeys.Append(aKey);
    1.49 +        if (err == KErrNone)
    1.50 +            {
    1.51 +            err = iValues.Append(aValue);
    1.52 +            if (err != KErrNone)
    1.53 +                {
    1.54 +                // last element of iKeys should be removed
    1.55 +                TInt lastElementIndex = iKeys.Count() - 1;
    1.56 +                iKeys.Remove(lastElementIndex);
    1.57 +                }
    1.58 +            }
    1.59 +        return err;
    1.60 +        }
    1.61 +
    1.62 +    TInt Find(const K& aKey) const
    1.63 +        {
    1.64 +        TInt index = KErrNotFound;
    1.65 +        for (int i = 0; i < iKeys.Count(); i++)
    1.66 +            {
    1.67 +            if (*iKeys[i] == aKey)
    1.68 +                {
    1.69 +                index = i;
    1.70 +                break;
    1.71 +                }
    1.72 +            }
    1.73 +        return index;
    1.74 +        }
    1.75 +
    1.76 +    // @return the index of removed key-value pair, or
    1.77 +    // KErrNotFound, if such key was not found
    1.78 +    TInt RemoveByKey(const K& aKey)
    1.79 +        {
    1.80 +        TInt index = Find(aKey);
    1.81 +        if (index != KErrNotFound)
    1.82 +            {
    1.83 +            if(iTakeOwnershipKey)
    1.84 +                {
    1.85 +                K* key = KeyAt(index);
    1.86 +                delete key;
    1.87 +                }
    1.88 +            if(iTakeOwnershipValue)
    1.89 +                {
    1.90 +                V* value = iValues[index];
    1.91 +                delete value;
    1.92 +                }
    1.93 +            iKeys.Remove(index);
    1.94 +            iValues.Remove(index);
    1.95 +            }
    1.96 +        return index;
    1.97 +        }
    1.98 +
    1.99 +    // @return the index of removed key-value pair, or
   1.100 +    // KErrNotFound, if such key was not found
   1.101 +    TInt Remove(const V& aValue)
   1.102 +        {
   1.103 +        TInt index = FindValue(aValue);
   1.104 +        if (index != KErrNotFound)
   1.105 +            {
   1.106 +            if (iTakeOwnershipValue)
   1.107 +                {
   1.108 +                V* value = iValues[index];
   1.109 +                delete value;
   1.110 +                }
   1.111 +            if (iTakeOwnershipKey)
   1.112 +                {
   1.113 +                K* key = iKeys[index];
   1.114 +                delete key;
   1.115 +                }
   1.116 +            iValues.Remove(index);
   1.117 +            iKeys.Remove(index);
   1.118 +            }
   1.119 +        return index; 
   1.120 +        }
   1.121 +
   1.122 +    TInt FindValue(const V& aValue) const
   1.123 +        {
   1.124 +        TInt index = KErrNotFound;
   1.125 +        for (int i = 0; i < iValues.Count(); i++)
   1.126 +            {
   1.127 +            if ((iValues[i]) && (*iValues[i] == aValue))
   1.128 +                {
   1.129 +                index = i;
   1.130 +                break;
   1.131 +                }
   1.132 +            }
   1.133 +        return index;
   1.134 +        }
   1.135 +
   1.136 +
   1.137 +    // Note: deletes the current value of this key
   1.138 +    TInt UpdateValue(const K* aKey, const V* aValue)
   1.139 +        {
   1.140 +        TInt index=Find(*aKey);
   1.141 +        if (index==KErrNotFound)
   1.142 +            {
   1.143 +            return Append(aKey, aValue);
   1.144 +            }
   1.145 +
   1.146 +        V* bValue=iValues[index];
   1.147 +        if (iTakeOwnershipValue)
   1.148 +            {
   1.149 +            // Since OWNED value is going to be replaced with aValue,
   1.150 +            // destroy old value instance first:
   1.151 +            delete bValue;  
   1.152 +            }
   1.153 +
   1.154 +        iValues[index]=(V*)aValue;
   1.155 +        return KErrNone;
   1.156 +        }
   1.157 +
   1.158 +
   1.159 +    K* KeyAt(TInt aIndex)
   1.160 +        {
   1.161 +        return iKeys[aIndex];
   1.162 +        }
   1.163 +
   1.164 +    const V* ValueAt(TInt aIndex) const
   1.165 +        {
   1.166 +        return iValues[aIndex];
   1.167 +        }
   1.168 +
   1.169 +    TInt Count() const
   1.170 +        {
   1.171 +        return iKeys.Count();
   1.172 +        }
   1.173 +
   1.174 +    void Reset()
   1.175 +        {
   1.176 +        if ( iTakeOwnershipKey )
   1.177 +            {
   1.178 +            iKeys.ResetAndDestroy();
   1.179 +            }
   1.180 +        else
   1.181 +            {
   1.182 +            iKeys.Reset();
   1.183 +            }
   1.184 +
   1.185 +        if ( iTakeOwnershipValue )
   1.186 +            {
   1.187 +            iValues.ResetAndDestroy();
   1.188 +            }
   1.189 +        else
   1.190 +            {
   1.191 +            iValues.Reset();
   1.192 +            }
   1.193 +        }
   1.194 +        
   1.195 +    TInt Insert(const K* aKey, const V* aValue)
   1.196 +        {
   1.197 +        TInt count=iKeys.Count();
   1.198 +        TInt err=KErrNone;
   1.199 +		TBool inserted=EFalse;
   1.200 +
   1.201 +        for(TInt i=0; i<count; i++)
   1.202 +        	{
   1.203 +        	 if(*iKeys[i] >= *aKey)
   1.204 +        	 {
   1.205 +	        err = iKeys.Insert(aKey, i);
   1.206 +	        if (err == KErrNone)
   1.207 +	            {
   1.208 +	            err = iValues.Insert(aValue, i);
   1.209 +	            if (err != KErrNone)
   1.210 +	                {
   1.211 +	                // inserted element of iKeys should be removed
   1.212 +	                iKeys.Remove(i);
   1.213 +	                }
   1.214 +				    else
   1.215 +					{
   1.216 +					inserted=ETrue;
   1.217 +					}
   1.218 +            	}
   1.219 +			break;  	 	
   1.220 +        	 }
   1.221 +			}
   1.222 +
   1.223 +        if(!inserted)
   1.224 +        	{
   1.225 +	        err = iKeys.Append(aKey);
   1.226 +	        if (err == KErrNone)
   1.227 +	            {
   1.228 +	            err = iValues.Append(aValue);
   1.229 +	            if (err != KErrNone)
   1.230 +	                {
   1.231 +	                // last element of iKeys should be removed
   1.232 +	                TInt lastElementIndex = iKeys.Count() - 1;
   1.233 +	                iKeys.Remove(lastElementIndex);
   1.234 +	                }
   1.235 +            	}
   1.236 +        	}
   1.237 +	return err;
   1.238 +        }
   1.239 +        
   1.240 +    private: // Data
   1.241 +    TBool iTakeOwnershipKey;
   1.242 +    TBool iTakeOwnershipValue;
   1.243 +    RPointerArray<K> iKeys;
   1.244 +    RPointerArray<V> iValues;
   1.245 +    };
   1.246 +
   1.247 +#endif // POINTERMAP_H
   1.248 +
   1.249 +// End of File
   1.250 +