1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/store/USTOR/UT_MAP.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,249 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "UT_STD.H"
1.20 +
1.21 +//#pragma message( __FILE__ " : Find helper function may be less code." )
1.22 +
1.23 +const TInt KStoreMapGranularity=4;
1.24 +
1.25 +EXPORT_C CStoreMap* CStoreMap::NewL(CStreamStore& aStore)
1.26 +/** Allocates and constructs a store map associated with the specified store and
1.27 +returns a pointer to that store map.
1.28 +
1.29 +The function leaves if it cannot complete successfully.
1.30 +
1.31 +@param aStore A reference to the store associated with this store map.
1.32 +@return A pointer to the new store map object. */
1.33 + {
1.34 + return new(ELeave) CStoreMap(aStore);
1.35 + }
1.36 +
1.37 +EXPORT_C CStoreMap* CStoreMap::NewLC(CStreamStore& aStore)
1.38 +/** Allocates and constructs a store map associated with the specified store, returns
1.39 +a pointer to that store map and puts the pointer onto the cleanup stack.
1.40 +
1.41 +The function leaves if it cannot complete successfully.
1.42 +
1.43 +Placing a pointer to this object onto the cleanup stack allows the object
1.44 +and allocated resources to be cleaned up if a subsequent leave occurs.
1.45 +
1.46 +@param aStore A reference to the store associated with this store map.
1.47 +@return A pointer to the new store map object. */
1.48 + {
1.49 + CStoreMap* map=NewL(aStore);
1.50 + CleanupStack::PushL(map);
1.51 + return map;
1.52 + }
1.53 +
1.54 +EXPORT_C CStoreMap::CStoreMap(CStreamStore& aStore)
1.55 + : iArray(KStoreMapGranularity),iFree(KNullStreamId),iStore(&aStore)
1.56 + {}
1.57 +
1.58 +EXPORT_C CStoreMap::~CStoreMap()
1.59 +/** Frees resources owned by the object, prior to its destruction.
1.60 +
1.61 +In particular, the destructor deletes all streams whose stream ids are currently
1.62 +held within the store map and then empties the store map. */
1.63 + {
1.64 + ResetAndDestroy();
1.65 + }
1.66 +
1.67 +EXPORT_C void CStoreMap::BindL(TSwizzleC<TAny> aSwizzle,TStreamId anId)
1.68 +/** Creates an entry in the store map to contain the specified stream id and an
1.69 +associated swizzle. On return from this function, the stream id is said to
1.70 +be bound to the swizzle.
1.71 +
1.72 +If the store map already contains an entry with the same swizzle, then the
1.73 +new entry replaces the existing entry; in effect, knowledge of the original
1.74 +stream id associated with aSwizzle, is lost.
1.75 +
1.76 +Callers of this function can pass any type of swizzle as the first parameter,
1.77 +i.e. any swizzle derived from TSwizzleCBase. The required TSwizzleC<TAny>
1.78 +object is constructed from the swizzle passed by the caller.
1.79 +
1.80 +Note that in most applications, callers pass a general swizzle, i.e. an object
1.81 +of type TSwizzle<class T>, where T is the type of object represented by that
1.82 +swizzle.
1.83 +
1.84 +@param aSwizzle Any swizzle derived from TSwizzleCBase.
1.85 +@param anId The stream id to be bound to the swizzle. */
1.86 + {
1.87 + __ASSERT_DEBUG(aSwizzle!=NULL||anId==KNullStreamId,Panic(EStoreMapSwizzleMissing));
1.88 + if (aSwizzle==NULL)
1.89 + return;
1.90 +//
1.91 + __ASSERT_DEBUG(anId!=KNullStreamId,Panic(EStoreMapIdMissing));
1.92 + __DEBUG(TSwizzleC<TAny> label=Label(anId));
1.93 + __ASSERT_DEBUG(label==NULL||label==aSwizzle,Panic(EStoreMapIntroducingAlias));
1.94 + TEntry entry;
1.95 + entry.swizzle=aSwizzle;
1.96 + TKeyArrayFix key(_FOFF(TEntry,swizzle),ECmpTUint32);
1.97 + TInt i;
1.98 + if (iArray.FindIsq(entry,key,i)==0)
1.99 + {
1.100 + iArray[i].id=anId;
1.101 + return;
1.102 + }
1.103 +//
1.104 + __ASSERT_DEBUG(iFree==KNullStreamId,Panic(EStoreMapFreeIdPresent));
1.105 + iFree=entry.id=anId;
1.106 + iArray.InsertL(i,entry);
1.107 + iFree=KNullStreamId;
1.108 + }
1.109 +
1.110 +EXPORT_C void CStoreMap::Unbind(TSwizzleC<TAny> aSwizzle)
1.111 +/** Deletes the first entry from the store map corresponding to the specified swizzle.
1.112 +
1.113 +On return from this function, the swizzle is said to be unbound and knowledge
1.114 +of the associated stream id is lost.
1.115 +
1.116 +Callers of this function can pass any type of swizzle as the parameter, i.e.
1.117 +any swizzle derived from TSwizzleCBase. The required TSwizzleC<TAny> object
1.118 +is constructed from the swizzle passed by the caller.
1.119 +
1.120 +Notes:
1.121 +
1.122 +In most applications, callers pass a general swizzle, i.e. an object of type
1.123 +TSwizzle<class T>, where T is the type of object represented by that swizzle.
1.124 +
1.125 +If no matching entry can be found in the store map, then the store map remains
1.126 +unaltered.
1.127 +
1.128 +@param aSwizzle Any swizzle derived from TSwizzleCBase. */
1.129 + {
1.130 + TEntry entry;
1.131 + entry.swizzle=aSwizzle;
1.132 + TKeyArrayFix key(_FOFF(TEntry,swizzle),ECmpTUint32);
1.133 + TInt i;
1.134 + if (iArray.FindIsq(entry,key,i)==0)
1.135 + iArray.Delete(i);
1.136 + }
1.137 +
1.138 +EXPORT_C void CStoreMap::Forget(TStreamId anId)
1.139 +/** Deletes an entry from the store map. The entry selected is the first one whose
1.140 +stream id matches the specified stream id.
1.141 +
1.142 +On return from this function, the stream id is said to be forgotten and knowledge
1.143 +of that stream id is lost.
1.144 +
1.145 +@param anId The stream id used to identify the entry in the store map to be
1.146 +deleted. */
1.147 + {
1.148 + if (anId==iFree)
1.149 + {
1.150 + iFree=KNullStreamId;
1.151 + return;
1.152 + }
1.153 +//
1.154 + TEntry entry;
1.155 + entry.id=anId;
1.156 + TKeyArrayFix key(_FOFF(TEntry,id),ECmpTUint32);
1.157 + TInt i;
1.158 + if (iArray.Find(entry,key,i)==0)
1.159 + iArray.Delete(i);
1.160 + }
1.161 +
1.162 +EXPORT_C void CStoreMap::Reset()
1.163 +/** Deletes all entries from the store map.
1.164 +
1.165 +It is important that this function is called before deleting the store map,
1.166 +as the destructor attempts to delete all streams whose stream ids are held
1.167 +within the map. */
1.168 + {
1.169 + iFree=KNullStreamId;
1.170 + iArray.Reset();
1.171 + }
1.172 +
1.173 +EXPORT_C void CStoreMap::ResetAndDestroy()
1.174 +/** Deletes all streams whose ids are held within the store map, and then empties
1.175 +the store map. */
1.176 + {
1.177 + iStore->Delete(iFree);
1.178 + for (TIterator iter=Begin(),end=End();iter!=end;++iter)
1.179 + {
1.180 + const TEntry& entry=*iter;
1.181 + iStore->Delete(entry.id);
1.182 + }
1.183 + Reset();
1.184 + }
1.185 +
1.186 +EXPORT_C TStreamId CStoreMap::At(TSwizzleC<TAny> aSwizzle) const
1.187 +/** Returns the stream id of the first entry the store map matching the specified
1.188 +swizzle.
1.189 +
1.190 +Callers of this function can pass any type of swizzle as the first parameter,
1.191 +i.e. any swizzle derived from TSwizzleCBase. The required TSwizzleC<TAny>
1.192 +object is constructed from the swizzle passed by the caller.
1.193 +
1.194 +@param aSwizzle Any type of swizzle derived from TSwizzleCBase.
1.195 +@return The required streamid.KNullStreamId, if no matching entry can be found. */
1.196 + {
1.197 + TEntry entry;
1.198 + entry.swizzle=aSwizzle;
1.199 + TKeyArrayFix key(_FOFF(TEntry,swizzle),ECmpTUint32);
1.200 + TInt i;
1.201 + if (iArray.FindIsq(entry,key,i)!=0)
1.202 + return KNullStreamId;
1.203 +//
1.204 + return iArray[i].id;
1.205 + }
1.206 +
1.207 +EXPORT_C TSwizzleC<TAny> CStoreMap::Label(TStreamId anId) const
1.208 +/** Returns the swizzle in the store map assocated with the specified stream id.
1.209 +
1.210 +@param anId The id of the stream
1.211 +@return The associated swizzle. If there is no matching swizzle, then this
1.212 +is an uninitialized swizzle. */
1.213 + {
1.214 + TEntry entry;
1.215 + entry.id=anId;
1.216 + TKeyArrayFix key(_FOFF(TEntry,id),ECmpTUint32);
1.217 + TInt i;
1.218 + if (iArray.Find(entry,key,i)!=0)
1.219 + return NULL;
1.220 +//
1.221 + return iArray[i].swizzle;
1.222 + }
1.223 +
1.224 +EXPORT_C CStoreMap::TIterator CStoreMap::Begin() const
1.225 +//
1.226 +// Return the starting iterator for map iteration.
1.227 +//
1.228 + {
1.229 + return (iArray.Count()==0)?NULL:&iArray[0];
1.230 + }
1.231 +
1.232 +EXPORT_C CStoreMap::TIterator CStoreMap::End() const
1.233 +//
1.234 +// Return the end iterator for map iteration.
1.235 +//
1.236 + {
1.237 + return Begin()+iArray.Count();
1.238 + }
1.239 +
1.240 +void CStoreMap::ExternalizeL(const TStreamRef& aRef,RWriteStream& aStream) const
1.241 +//
1.242 +// Write the stream id bound to aRef to aStream.
1.243 +//
1.244 + {
1.245 + TSwizzleC<TAny> swizzle=aRef;
1.246 + TStreamId id=At(swizzle);
1.247 + if (id==KNullStreamId&&swizzle!=NULL)
1.248 + __LEAVE(KErrNotFound);
1.249 +//
1.250 + aStream<<id;
1.251 + }
1.252 +