sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "UT_STD.H" sl@0: sl@0: //#pragma message( __FILE__ " : Find helper function may be less code." ) sl@0: sl@0: const TInt KStoreMapGranularity=4; sl@0: sl@0: EXPORT_C CStoreMap* CStoreMap::NewL(CStreamStore& aStore) sl@0: /** Allocates and constructs a store map associated with the specified store and sl@0: returns a pointer to that store map. sl@0: sl@0: The function leaves if it cannot complete successfully. sl@0: sl@0: @param aStore A reference to the store associated with this store map. sl@0: @return A pointer to the new store map object. */ sl@0: { sl@0: return new(ELeave) CStoreMap(aStore); sl@0: } sl@0: sl@0: EXPORT_C CStoreMap* CStoreMap::NewLC(CStreamStore& aStore) sl@0: /** Allocates and constructs a store map associated with the specified store, returns sl@0: a pointer to that store map and puts the pointer onto the cleanup stack. sl@0: sl@0: The function leaves if it cannot complete successfully. sl@0: sl@0: Placing a pointer to this object onto the cleanup stack allows the object sl@0: and allocated resources to be cleaned up if a subsequent leave occurs. sl@0: sl@0: @param aStore A reference to the store associated with this store map. sl@0: @return A pointer to the new store map object. */ sl@0: { sl@0: CStoreMap* map=NewL(aStore); sl@0: CleanupStack::PushL(map); sl@0: return map; sl@0: } sl@0: sl@0: EXPORT_C CStoreMap::CStoreMap(CStreamStore& aStore) sl@0: : iArray(KStoreMapGranularity),iFree(KNullStreamId),iStore(&aStore) sl@0: {} sl@0: sl@0: EXPORT_C CStoreMap::~CStoreMap() sl@0: /** Frees resources owned by the object, prior to its destruction. sl@0: sl@0: In particular, the destructor deletes all streams whose stream ids are currently sl@0: held within the store map and then empties the store map. */ sl@0: { sl@0: ResetAndDestroy(); sl@0: } sl@0: sl@0: EXPORT_C void CStoreMap::BindL(TSwizzleC aSwizzle,TStreamId anId) sl@0: /** Creates an entry in the store map to contain the specified stream id and an sl@0: associated swizzle. On return from this function, the stream id is said to sl@0: be bound to the swizzle. sl@0: sl@0: If the store map already contains an entry with the same swizzle, then the sl@0: new entry replaces the existing entry; in effect, knowledge of the original sl@0: stream id associated with aSwizzle, is lost. sl@0: sl@0: Callers of this function can pass any type of swizzle as the first parameter, sl@0: i.e. any swizzle derived from TSwizzleCBase. The required TSwizzleC sl@0: object is constructed from the swizzle passed by the caller. sl@0: sl@0: Note that in most applications, callers pass a general swizzle, i.e. an object sl@0: of type TSwizzle, where T is the type of object represented by that sl@0: swizzle. sl@0: sl@0: @param aSwizzle Any swizzle derived from TSwizzleCBase. sl@0: @param anId The stream id to be bound to the swizzle. */ sl@0: { sl@0: __ASSERT_DEBUG(aSwizzle!=NULL||anId==KNullStreamId,Panic(EStoreMapSwizzleMissing)); sl@0: if (aSwizzle==NULL) sl@0: return; sl@0: // sl@0: __ASSERT_DEBUG(anId!=KNullStreamId,Panic(EStoreMapIdMissing)); sl@0: __DEBUG(TSwizzleC label=Label(anId)); sl@0: __ASSERT_DEBUG(label==NULL||label==aSwizzle,Panic(EStoreMapIntroducingAlias)); sl@0: TEntry entry; sl@0: entry.swizzle=aSwizzle; sl@0: TKeyArrayFix key(_FOFF(TEntry,swizzle),ECmpTUint32); sl@0: TInt i; sl@0: if (iArray.FindIsq(entry,key,i)==0) sl@0: { sl@0: iArray[i].id=anId; sl@0: return; sl@0: } sl@0: // sl@0: __ASSERT_DEBUG(iFree==KNullStreamId,Panic(EStoreMapFreeIdPresent)); sl@0: iFree=entry.id=anId; sl@0: iArray.InsertL(i,entry); sl@0: iFree=KNullStreamId; sl@0: } sl@0: sl@0: EXPORT_C void CStoreMap::Unbind(TSwizzleC aSwizzle) sl@0: /** Deletes the first entry from the store map corresponding to the specified swizzle. sl@0: sl@0: On return from this function, the swizzle is said to be unbound and knowledge sl@0: of the associated stream id is lost. sl@0: sl@0: Callers of this function can pass any type of swizzle as the parameter, i.e. sl@0: any swizzle derived from TSwizzleCBase. The required TSwizzleC object sl@0: is constructed from the swizzle passed by the caller. sl@0: sl@0: Notes: sl@0: sl@0: In most applications, callers pass a general swizzle, i.e. an object of type sl@0: TSwizzle, where T is the type of object represented by that swizzle. sl@0: sl@0: If no matching entry can be found in the store map, then the store map remains sl@0: unaltered. sl@0: sl@0: @param aSwizzle Any swizzle derived from TSwizzleCBase. */ sl@0: { sl@0: TEntry entry; sl@0: entry.swizzle=aSwizzle; sl@0: TKeyArrayFix key(_FOFF(TEntry,swizzle),ECmpTUint32); sl@0: TInt i; sl@0: if (iArray.FindIsq(entry,key,i)==0) sl@0: iArray.Delete(i); sl@0: } sl@0: sl@0: EXPORT_C void CStoreMap::Forget(TStreamId anId) sl@0: /** Deletes an entry from the store map. The entry selected is the first one whose sl@0: stream id matches the specified stream id. sl@0: sl@0: On return from this function, the stream id is said to be forgotten and knowledge sl@0: of that stream id is lost. sl@0: sl@0: @param anId The stream id used to identify the entry in the store map to be sl@0: deleted. */ sl@0: { sl@0: if (anId==iFree) sl@0: { sl@0: iFree=KNullStreamId; sl@0: return; sl@0: } sl@0: // sl@0: TEntry entry; sl@0: entry.id=anId; sl@0: TKeyArrayFix key(_FOFF(TEntry,id),ECmpTUint32); sl@0: TInt i; sl@0: if (iArray.Find(entry,key,i)==0) sl@0: iArray.Delete(i); sl@0: } sl@0: sl@0: EXPORT_C void CStoreMap::Reset() sl@0: /** Deletes all entries from the store map. sl@0: sl@0: It is important that this function is called before deleting the store map, sl@0: as the destructor attempts to delete all streams whose stream ids are held sl@0: within the map. */ sl@0: { sl@0: iFree=KNullStreamId; sl@0: iArray.Reset(); sl@0: } sl@0: sl@0: EXPORT_C void CStoreMap::ResetAndDestroy() sl@0: /** Deletes all streams whose ids are held within the store map, and then empties sl@0: the store map. */ sl@0: { sl@0: iStore->Delete(iFree); sl@0: for (TIterator iter=Begin(),end=End();iter!=end;++iter) sl@0: { sl@0: const TEntry& entry=*iter; sl@0: iStore->Delete(entry.id); sl@0: } sl@0: Reset(); sl@0: } sl@0: sl@0: EXPORT_C TStreamId CStoreMap::At(TSwizzleC aSwizzle) const sl@0: /** Returns the stream id of the first entry the store map matching the specified sl@0: swizzle. sl@0: sl@0: Callers of this function can pass any type of swizzle as the first parameter, sl@0: i.e. any swizzle derived from TSwizzleCBase. The required TSwizzleC sl@0: object is constructed from the swizzle passed by the caller. sl@0: sl@0: @param aSwizzle Any type of swizzle derived from TSwizzleCBase. sl@0: @return The required streamid.KNullStreamId, if no matching entry can be found. */ sl@0: { sl@0: TEntry entry; sl@0: entry.swizzle=aSwizzle; sl@0: TKeyArrayFix key(_FOFF(TEntry,swizzle),ECmpTUint32); sl@0: TInt i; sl@0: if (iArray.FindIsq(entry,key,i)!=0) sl@0: return KNullStreamId; sl@0: // sl@0: return iArray[i].id; sl@0: } sl@0: sl@0: EXPORT_C TSwizzleC CStoreMap::Label(TStreamId anId) const sl@0: /** Returns the swizzle in the store map assocated with the specified stream id. sl@0: sl@0: @param anId The id of the stream sl@0: @return The associated swizzle. If there is no matching swizzle, then this sl@0: is an uninitialized swizzle. */ sl@0: { sl@0: TEntry entry; sl@0: entry.id=anId; sl@0: TKeyArrayFix key(_FOFF(TEntry,id),ECmpTUint32); sl@0: TInt i; sl@0: if (iArray.Find(entry,key,i)!=0) sl@0: return NULL; sl@0: // sl@0: return iArray[i].swizzle; sl@0: } sl@0: sl@0: EXPORT_C CStoreMap::TIterator CStoreMap::Begin() const sl@0: // sl@0: // Return the starting iterator for map iteration. sl@0: // sl@0: { sl@0: return (iArray.Count()==0)?NULL:&iArray[0]; sl@0: } sl@0: sl@0: EXPORT_C CStoreMap::TIterator CStoreMap::End() const sl@0: // sl@0: // Return the end iterator for map iteration. sl@0: // sl@0: { sl@0: return Begin()+iArray.Count(); sl@0: } sl@0: sl@0: void CStoreMap::ExternalizeL(const TStreamRef& aRef,RWriteStream& aStream) const sl@0: // sl@0: // Write the stream id bound to aRef to aStream. sl@0: // sl@0: { sl@0: TSwizzleC swizzle=aRef; sl@0: TStreamId id=At(swizzle); sl@0: if (id==KNullStreamId&&swizzle!=NULL) sl@0: __LEAVE(KErrNotFound); sl@0: // sl@0: aStream<