windowing/windowserver/nonnga/SERVER/WSOBJIX.CPP
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1996-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 // Object index
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "WSOBJIX.H"
       
    19 
       
    20 #include "OBJECT.H"
       
    21 #include "server.h"
       
    22 #include "panics.h"
       
    23 
       
    24 const TInt KNewObjectInitialCount = 1;
       
    25 
       
    26 CWsObjectIx::CWsObjectIx() : iObjectArray(8), iNewObjectCount(KNewObjectInitialCount)
       
    27 	{
       
    28 	}
       
    29 
       
    30 CWsObjectIx::~CWsObjectIx()
       
    31 	{
       
    32 	TInt count=iObjectArray.Count();
       
    33 	if (count==0)
       
    34 		return;
       
    35 	TWsObject* ptr=&iObjectArray[0];
       
    36 	const TWsObject* end=ptr+count;
       
    37 	for (;ptr<end;ptr++)
       
    38 		{
       
    39 		CWsObject* obj=ptr->iObject;
       
    40 		if (obj)
       
    41 			{
       
    42 			ptr->iObject=NULL;
       
    43 			obj->CloseObject();
       
    44 			}
       
    45 		}
       
    46 	iObjectArray.Reset();
       
    47 	}
       
    48 
       
    49 void CWsObjectIx::ConstructL()
       
    50 	{
       
    51 	// Insert a dummy object into the first slot to avoid
       
    52 	// having a null handle
       
    53 	TWsObject nullObject(NULL,0);
       
    54 	iObjectArray.AppendL(nullObject);
       
    55 	}
       
    56 
       
    57 /** Adds the object to the list.
       
    58 
       
    59 If there is an empty slot (i.e. a slot that was previously used but has been freed) the
       
    60 object is put in that slot else a new slot is created at the end of the array.
       
    61 
       
    62 @param anObj The object to add to the list.
       
    63 @return The handle to be used by the client to identify this object.
       
    64 @internalComponent
       
    65 @released
       
    66 */
       
    67 TInt CWsObjectIx::AddL(CWsObject* anObj)
       
    68 	{
       
    69 	TWsObject* ptr=&iObjectArray[0];
       
    70 	const TWsObject* base=ptr;
       
    71 	TInt index=iObjectArray.Count();
       
    72 	const TWsObject* end=ptr+index;
       
    73 	WS_ASSERT_DEBUG(base->iObject==NULL, EWsPanicObjectIndexError);
       
    74 	
       
    75 	// Search for an empty slot
       
    76 	while(++ptr<end)
       
    77 		{
       
    78 		if (ptr->iObject==NULL)
       
    79 			{
       
    80 			ptr->iObject=anObj;
       
    81 			index=ptr-base;
       
    82 			break;
       
    83 			}
       
    84 		}
       
    85 	
       
    86 	// create a new handle for the object
       
    87 	TUint handleCount = ((iNewObjectCount<<TWsObject::ECountPosition) + TWsObject::ECountInc) & TWsObject::ECountMask;
       
    88 	
       
    89 	if (ptr==end)
       
    90 		{
       
    91 		// No available empty slot, so append this object to the queue
       
    92 		if (index==TWsObject::ECountInc)
       
    93 			{
       
    94 			User::LeaveNoMemory();	//Things will go wrong later if we ever have more 64K objects
       
    95 			}	
       
    96 		TWsObject newObject(anObj,handleCount);
       
    97 		iObjectArray.AppendL(newObject);
       
    98 		ptr=&iObjectArray[index];
       
    99 		}
       
   100  
       
   101 	// assign the object a unique server-side handle (combination of handle count & object type)
       
   102 	ptr->iHandle = handleCount | anObj->Type();
       
   103 
       
   104 	// increment the object counter
       
   105 	if (++iNewObjectCount==TWsObject::ECountWrapAround)
       
   106 		iNewObjectCount = KNewObjectInitialCount;
       
   107  	
       
   108 	// return to the client their unique handle for the object (combination of handle count & slot number)
       
   109 	return (handleCount + index);
       
   110 	}	 
       
   111 
       
   112 void CWsObjectIx::Tidy()
       
   113 	{
       
   114 	TInt count=iObjectArray.Count()-1;
       
   115 	while(count>0)	// Don't delete object [0]
       
   116 		{
       
   117 		if (iObjectArray[count].iObject!=NULL)
       
   118 			break;
       
   119 		iObjectArray.Delete(count--);
       
   120 		}
       
   121 	}
       
   122 
       
   123 void CWsObjectIx::Remove(CWsObject* anObj)
       
   124 	{
       
   125 	const TWsObject* ptr=FirstObject();
       
   126 	const TWsObject* end=ptr+iObjectArray.Count();
       
   127 	WS_ASSERT_DEBUG(ptr->iObject==NULL, EWsPanicObjectIndexError);
       
   128 	while(++ptr<end)
       
   129 		{
       
   130 		if (ptr->iObject==anObj)
       
   131 			{
       
   132 			Remove(ptr);
       
   133 			Tidy();
       
   134 			return;
       
   135 			}
       
   136 		}
       
   137 	}
       
   138 
       
   139 void CWsObjectIx::Remove(const TWsObject* aObject)
       
   140 	{
       
   141 	WS_ASSERT_DEBUG(aObject->iObject!=NULL, EWsPanicObjectIndexError);
       
   142 	WS_ASSERT_DEBUG((aObject->iHandle&TWsObject::ETypeMask)==(TUint)aObject->iObject->Type(), EWsPanicObjectIndexError);
       
   143 	const_cast<TWsObject*>(aObject)->iObject=NULL;
       
   144 	}
       
   145 
       
   146 CWsObject* CWsObjectIx::HandleToObject(TInt aHandle) const
       
   147 	{
       
   148 	TInt slot=aHandle&TWsObject::ESlotMask;
       
   149 	if (slot<0 || slot>=iObjectArray.Count())
       
   150 		return(NULL);
       
   151 	const TWsObject* object=&iObjectArray[slot];
       
   152 	if ((object->iHandle&TWsObject::ECountMask)==((TUint)aHandle&TWsObject::ECountMask))
       
   153 		{
       
   154 		return object->iObject;
       
   155 		}
       
   156 	return(NULL);
       
   157 	}
       
   158 
       
   159 const TWsObject* CWsObjectIx::FirstObject() const
       
   160 	{
       
   161 	return(&iObjectArray[0]);
       
   162 	}
       
   163 
       
   164 TInt CWsObjectIx::At(const CWsObject* anObj)
       
   165 	{
       
   166 	const TWsObject* base=FirstObject();
       
   167 	const TWsObject* ptr=base;
       
   168 	const TWsObject* end=base+iObjectArray.Count();
       
   169 	WS_ASSERT_DEBUG(ptr->iObject==NULL, EWsPanicObjectIndexError);
       
   170 	while(++ptr<end)
       
   171 		{
       
   172 		if (ptr->iObject==anObj && (ptr->iHandle&TWsObject::ETypeMask)==(TUint)anObj->Type())
       
   173 			return(ptr-base);
       
   174 		}
       
   175 	return(KErrNotFound);
       
   176 	}
       
   177 
       
   178 TInt CWsObjectIx::Count() const
       
   179 	{
       
   180 	const TWsObject* ptr=FirstObject();
       
   181 	const TWsObject* end=ptr+iObjectArray.Count();
       
   182 	WS_ASSERT_DEBUG(ptr->iObject==NULL, EWsPanicObjectIndexError);
       
   183 	TInt count=0;
       
   184 	while(++ptr<end)
       
   185 		{
       
   186 		if (ptr->iObject && ptr->iObject->Type()!=WS_HANDLE_CLIENT)
       
   187 			count++;
       
   188 		}
       
   189 	return(count);
       
   190 	}
       
   191 
       
   192 TInt CWsObjectIx::Length() const
       
   193 	{
       
   194 	return(iObjectArray.Count());
       
   195 	}