diff -r 000000000000 -r b16258d2340f applayerpluginsandutils/uripermissionservices/server/inc/StreamObjContainer.inl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/applayerpluginsandutils/uripermissionservices/server/inc/StreamObjContainer.inl Tue Feb 02 01:09:52 2010 +0200 @@ -0,0 +1,197 @@ +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// SqlSvrObjContainer.inl +// Should not be a template class! +// Constructs an empty container. +// +// + +template +RStreamObjContainer::RStreamObjContainer() : + iEntries(NULL), + iSize(0), + iFree(0) + { + } + +/** +Destroys the container and its content. +*/ +template +void RStreamObjContainer::Close() + { + if(iEntries) + { + for(TEntry* entry=iEntries;entry<(iEntries+iSize);++entry) + { + delete entry->iObj; + } + User::Free(iEntries); + iEntries = NULL; + } + iFree = iSize = 0; + } + +/** +Ensures that the next attempt for adding a new object to the container won't fail because there +is no memory. +(In other words - ensures that the container has at least one empty slot) + +@leave KErrNoMemory, an out of memory condition has occured; + +@panic SqlDb 7 In _DEBUG mode. Internal logic error. +*/ +template +void RStreamObjContainer::AllocL() + { + __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); + if(iFree == iSize) + { + if(iSize >= KMaxSize) + { + User::Leave(KErrNoMemory); + } + TInt size = iSize + RStreamObjContainer::KGranularity; + iEntries = (TEntry*)User::ReAllocL(iEntries, size * sizeof(TEntry)); + iSize = size; + for(TInt i=iFree;i +TInt RStreamObjContainer::Add(T* aObj) + { + __ASSERT_DEBUG(aObj != NULL, User::Invariant()); + __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); + TInt idx = iFree; + if(idx < iSize) + { + TEntry& entry = iEntries[idx]; + __ASSERT_DEBUG(!entry.iObj, User::Invariant()); + iFree = entry.iNext; + __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); + entry.iObj = aObj; + return MakeHandle(idx); + } + return 0; + } + +/** +@param aIndex Valid container index. + +@return Handle, uniquely identifying the object stored at aIndex index. +*/ +template +TInt RStreamObjContainer::MakeHandle(TInt aIndex) const + { + return aIndex + 1; + } + +/** +@param aHandle Unique object handle + +@return Container index of the object, identified by aHandle parameter. +*/ +template +TInt RStreamObjContainer::MakeIndex(TInt aHandle) const + { + return aHandle - 1; + } + +/** +Removes an object from the container. +The removed object will be destroyed. + +@param aHandle Unique object handle + +@panic SqlDb 7 In _DEBUG mode. Internal logic error or there is no such object in the container. +*/ +template +void RStreamObjContainer::Remove(TInt aHandle) + { + __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); + if(aHandle > 0) //It is a handle, sent by the client. It isn't a server's problem if the handle is <= 0. + { + TInt idx = MakeIndex(aHandle); + if(idx < iSize) + { + TEntry& entry = iEntries[idx]; + delete entry.iObj; + entry.iObj = NULL; + entry.iNext = iFree; + iFree = idx; + } + } + __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); + } + +/** +Looks up for an object in the container. + +@param aHandle Unique object handle + +@return A pointer to the found object or NULL. +*/ +template +T* RStreamObjContainer::Find(TInt aHandle) const + { + TEntry* entry = NULL; + if(aHandle > 0) //It is a handle, sent by the client. It isn't a server's problem if the handle is <= 0. + { + TInt idx = MakeIndex(aHandle); + if(idx < iSize) + { + entry = &iEntries[idx]; + } + } + return entry != NULL ? entry->iObj : NULL; + } + +/** +Counts the alive objects in the container + +@return The object count +*/ +template +TInt RStreamObjContainer::Count() const + { + TInt count = 0;; + const TEntry* const base = iEntries; + if(base) + { + for(const TEntry* e=base+iSize;--e>=base;) + { + if(e->iObj) + { + ++count; + } + } + } + return count; + }