persistentstorage/sql/SRC/Common/SqlMap.inl
changeset 0 08ec8eefde2f
child 9 667e88a979d7
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // RSqlMap template class implementation.
       
    15 // ////////////////////                 TSqlPair implementation              //////////////////////////////////
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  
       
    21  Initializes TSqlPair data members using the supplied parameter values.
       
    22  
       
    23  @param aKey Key part of TSqlPair object
       
    24  @param aData Data part of TSqlPair object
       
    25 */
       
    26 template <class KEY, class DATA, class REFCNTR> 
       
    27 TSqlPair<KEY, DATA, REFCNTR>::TSqlPair(const KEY& aKey, const DATA& aData) :
       
    28 	iKey(aKey),
       
    29 	iData(aData)
       
    30 	{
       
    31 	}
       
    32 
       
    33 /**
       
    34 Initializes TSqlPair data members using the supplied parameter value.
       
    35 
       
    36 @param aKey Key part of TSqlPair object
       
    37 */
       
    38 template <class KEY, class DATA, class REFCNTR> 
       
    39 TSqlPair<KEY, DATA, REFCNTR>::TSqlPair(const KEY& aKey) :
       
    40 	iKey(aKey)
       
    41 	{
       
    42 	}
       
    43 
       
    44 /**
       
    45 Initializes TSqlPair data members with their default values.
       
    46 */
       
    47 template <class KEY, class DATA, class REFCNTR> 
       
    48 TSqlPair<KEY, DATA, REFCNTR>::TSqlPair()
       
    49 	{
       
    50 	}
       
    51 
       
    52 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    53 ///////////////////////                 RSqlMap implementation              ///////////////////////////////////
       
    54 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
    55 
       
    56 /**
       
    57 @param aOrder An object of TLinearOrder< TSqlPair<KEY, DATA, REFCNTR> > type, which will be used when
       
    58 new elements are inserted into the collection (to determine their order).
       
    59 @param aDestructor An object of TSqlPairDestructor<KEY, DATA> type which will be used for the destruction of
       
    60 KEY and DATA TSqlPair data members.
       
    61 */
       
    62 template <class KEY, class DATA, class REFCNTR, class DESTRUCTOR> 
       
    63 RSqlMap<KEY, DATA, REFCNTR, DESTRUCTOR>::RSqlMap(const TLinearOrder< TSqlPair<KEY, DATA, REFCNTR> >& aOrder, const DESTRUCTOR& aDestructor) :
       
    64 	iOrder(aOrder),
       
    65 	iDestructor(aDestructor)
       
    66 	{
       
    67 	}
       
    68 
       
    69 /**
       
    70 Closes RSqlMap instance and destroys all RSqlMap data.
       
    71 */
       
    72 template <class KEY, class DATA, class REFCNTR, class DESTRUCTOR> 
       
    73 void RSqlMap<KEY, DATA, REFCNTR, DESTRUCTOR>::Close()
       
    74 	{
       
    75 	TInt idx = iSet.Count();
       
    76 	while(--idx >= 0)
       
    77 		{
       
    78 		TSqlPair<KEY, DATA, REFCNTR>& pair = iSet[idx];
       
    79 		iDestructor.Destroy(pair.iKey, pair.iData);
       
    80 		}
       
    81 	iSet.Close();
       
    82 	}
       
    83 
       
    84 /**
       
    85 This method will create and insert new (KEY, DATA, REFCNTR) pair in the map.
       
    86 
       
    87 RSqlMap class maintains a set of reference counted objects.
       
    88 If an object with aKey key is already in the map, no insertion will occur, the reference counter
       
    89 of the existing object will be incremented.
       
    90 
       
    91 @param aKey The key part of TSqlPair object, which will be inserted
       
    92 @param aData The data part of TSqlPair object, which will be inserted
       
    93 
       
    94 @return System-wide error code if the insertion fails, reference counter value otherwise.
       
    95 */
       
    96 template <class KEY, class DATA, class REFCNTR, class DESTRUCTOR> 
       
    97 TInt RSqlMap<KEY, DATA, REFCNTR, DESTRUCTOR>::Insert(const KEY& aKey, const DATA& aData)
       
    98 	{
       
    99 	TInt idx = iSet.FindInOrder(TSqlPair<KEY, DATA, REFCNTR>(aKey), iOrder);
       
   100 	if(idx >= 0)
       
   101 		{
       
   102 		return iSet[idx].iRefCounter.Increment();
       
   103 		}
       
   104 	else
       
   105 		{
       
   106 		TInt err = iSet.InsertInOrder(TSqlPair<KEY, DATA, REFCNTR>(aKey, aData), iOrder);
       
   107 		return err == KErrNone ? 1 : err;
       
   108 		}
       
   109 	}
       
   110 
       
   111 /**
       
   112 This method removes an element with aKey key from the map.
       
   113 
       
   114 If there is no such element in the map, the debug verison of the method will panic.
       
   115 
       
   116 RSqlMap class maintains a set of reference counted objects.
       
   117 If an object with aKey key is in the map, the reference counter of the object will be decremented.
       
   118 If the object's reference counter reaches 0 then the object will be destroyed.
       
   119 
       
   120 @param aKey The key of TSqlPair object, which has to be removed from the collection.
       
   121 
       
   122 @panic 7 In _DEBUG mode if there is no entry with aKey key in the RSqlMap container.
       
   123 */
       
   124 template <class KEY, class DATA, class REFCNTR, class DESTRUCTOR> 
       
   125 void RSqlMap<KEY, DATA, REFCNTR, DESTRUCTOR>::Remove(const KEY& aKey)
       
   126 	{
       
   127 	TInt idx = iSet.FindInOrder(TSqlPair<KEY, DATA, REFCNTR>(aKey), iOrder);
       
   128 	if(idx != KErrNotFound)
       
   129 		{
       
   130 		TSqlPair<KEY, DATA, REFCNTR>& pair = iSet[idx];
       
   131 		if(pair.iRefCounter.Decrement() == 0)
       
   132 			{
       
   133 			iDestructor.Destroy(pair.iKey, pair.iData);
       
   134 			iSet.Remove(idx);
       
   135 			}
       
   136 		return;
       
   137 		}
       
   138 	__SQLASSERT(EFalse, ESqlPanicInternalError);
       
   139 	}
       
   140 
       
   141 /**
       
   142 This method performs a search for an element with aKey key in the map.
       
   143 If such element exists, the method will return a pointer to it.
       
   144 If not, then the method will return NULL.
       
   145 
       
   146 @param aKey The search key.
       
   147 
       
   148 @return A pointer to the found element or NULL.
       
   149 */
       
   150 template <class KEY, class DATA, class REFCNTR, class DESTRUCTOR> 
       
   151 TSqlPair<KEY, DATA, REFCNTR>* RSqlMap<KEY, DATA, REFCNTR, DESTRUCTOR>::Entry(const KEY& aKey)
       
   152 	{
       
   153 	TInt idx = iSet.FindInOrder(TSqlPair<KEY, DATA, REFCNTR>(aKey), iOrder);
       
   154 	return idx == KErrNotFound ? NULL : &iSet[idx];
       
   155 	}
       
   156 
       
   157 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   158 ///////////////////////            TSqlMapIterator implementation              ////////////////////////////////
       
   159 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   160 
       
   161 /**
       
   162 Initializes TSqlMapIterator instance.
       
   163 
       
   164 @param aMap A const reference to the RSqlMap object, which will be iterated.
       
   165 */
       
   166 template <class KEY, class DATA, class REFCNTR, class DESTRUCTOR> 
       
   167 TSqlMapIterator<KEY, DATA, REFCNTR, DESTRUCTOR>::TSqlMapIterator(const RSqlMap<KEY, DATA, REFCNTR, DESTRUCTOR>& aMap) :
       
   168 	iMap(aMap),
       
   169 	iIndex(0)
       
   170 	{
       
   171 	}
       
   172 
       
   173 /**
       
   174 If iterator's current position is not beyond the end of the map,
       
   175 the iterator will retrieve current map entry into aPair argument, advance iterator position
       
   176 by 1 and return ETrue. 
       
   177 
       
   178 Otherwise the iterator will return EFalse.
       
   179 
       
   180 @param aPair An output parameter, which references the location, where the next RSqlMap element will be stored.
       
   181 
       
   182 @return ETrue The next RSqlMap element successfully loaded into aPair parameter. This is not the 
       
   183               end of RSqlMap collection.
       
   184 @return EFalse The next RSqlMap element successfully loaded into aPair parameter. This is the 
       
   185               end of RSqlMap collection. Do not call Next() anymore.
       
   186 */
       
   187 template <class KEY, class DATA, class REFCNTR, class DESTRUCTOR> 
       
   188 TBool TSqlMapIterator<KEY, DATA, REFCNTR, DESTRUCTOR>::Next(TSqlPair<KEY, DATA, REFCNTR>& aPair) const
       
   189 	{
       
   190 	return (iIndex < iMap.iSet.Count()) ? aPair = iMap.iSet[iIndex++], ETrue : EFalse;
       
   191 	}