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