|
1 // Copyright (c) 2006-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 // SqlSvrObjContainer.inl |
|
15 // Should not be a template class! |
|
16 // Constructs an empty container. |
|
17 // |
|
18 // |
|
19 |
|
20 template <class T> |
|
21 RStreamObjContainer<T>::RStreamObjContainer() : |
|
22 iEntries(NULL), |
|
23 iSize(0), |
|
24 iFree(0) |
|
25 { |
|
26 } |
|
27 |
|
28 /** |
|
29 Destroys the container and its content. |
|
30 */ |
|
31 template <class T> |
|
32 void RStreamObjContainer<T>::Close() |
|
33 { |
|
34 if(iEntries) |
|
35 { |
|
36 for(TEntry* entry=iEntries;entry<(iEntries+iSize);++entry) |
|
37 { |
|
38 delete entry->iObj; |
|
39 } |
|
40 User::Free(iEntries); |
|
41 iEntries = NULL; |
|
42 } |
|
43 iFree = iSize = 0; |
|
44 } |
|
45 |
|
46 /** |
|
47 Ensures that the next attempt for adding a new object to the container won't fail because there |
|
48 is no memory. |
|
49 (In other words - ensures that the container has at least one empty slot) |
|
50 |
|
51 @leave KErrNoMemory, an out of memory condition has occured; |
|
52 |
|
53 @panic SqlDb 7 In _DEBUG mode. Internal logic error. |
|
54 */ |
|
55 template <class T> |
|
56 void RStreamObjContainer<T>::AllocL() |
|
57 { |
|
58 __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); |
|
59 if(iFree == iSize) |
|
60 { |
|
61 if(iSize >= KMaxSize) |
|
62 { |
|
63 User::Leave(KErrNoMemory); |
|
64 } |
|
65 TInt size = iSize + RStreamObjContainer<T>::KGranularity; |
|
66 iEntries = (TEntry*)User::ReAllocL(iEntries, size * sizeof(TEntry)); |
|
67 iSize = size; |
|
68 for(TInt i=iFree;i<size;) |
|
69 { |
|
70 TEntry& entry = iEntries[i]; |
|
71 entry.iObj = NULL; |
|
72 entry.iNext = ++i; |
|
73 } |
|
74 } |
|
75 } |
|
76 |
|
77 /** |
|
78 The method adds "aObj" pointer to the container. |
|
79 |
|
80 @param aObj A pointer to the object which will be stored in the container. |
|
81 |
|
82 @return Handle, uniquely identifying the stored object |
|
83 |
|
84 @panic SqlDb 4 In _DEBUG mode. aObj parameter is NULL. |
|
85 @panic SqlDb 7 In _DEBUG mode. Internal logic error. |
|
86 */ |
|
87 template <class T> |
|
88 TInt RStreamObjContainer<T>::Add(T* aObj) |
|
89 { |
|
90 __ASSERT_DEBUG(aObj != NULL, User::Invariant()); |
|
91 __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); |
|
92 TInt idx = iFree; |
|
93 if(idx < iSize) |
|
94 { |
|
95 TEntry& entry = iEntries[idx]; |
|
96 __ASSERT_DEBUG(!entry.iObj, User::Invariant()); |
|
97 iFree = entry.iNext; |
|
98 __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); |
|
99 entry.iObj = aObj; |
|
100 return MakeHandle(idx); |
|
101 } |
|
102 return 0; |
|
103 } |
|
104 |
|
105 /** |
|
106 @param aIndex Valid container index. |
|
107 |
|
108 @return Handle, uniquely identifying the object stored at aIndex index. |
|
109 */ |
|
110 template <class T> |
|
111 TInt RStreamObjContainer<T>::MakeHandle(TInt aIndex) const |
|
112 { |
|
113 return aIndex + 1; |
|
114 } |
|
115 |
|
116 /** |
|
117 @param aHandle Unique object handle |
|
118 |
|
119 @return Container index of the object, identified by aHandle parameter. |
|
120 */ |
|
121 template <class T> |
|
122 TInt RStreamObjContainer<T>::MakeIndex(TInt aHandle) const |
|
123 { |
|
124 return aHandle - 1; |
|
125 } |
|
126 |
|
127 /** |
|
128 Removes an object from the container. |
|
129 The removed object will be destroyed. |
|
130 |
|
131 @param aHandle Unique object handle |
|
132 |
|
133 @panic SqlDb 7 In _DEBUG mode. Internal logic error or there is no such object in the container. |
|
134 */ |
|
135 template <class T> |
|
136 void RStreamObjContainer<T>::Remove(TInt aHandle) |
|
137 { |
|
138 __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); |
|
139 if(aHandle > 0) //It is a handle, sent by the client. It isn't a server's problem if the handle is <= 0. |
|
140 { |
|
141 TInt idx = MakeIndex(aHandle); |
|
142 if(idx < iSize) |
|
143 { |
|
144 TEntry& entry = iEntries[idx]; |
|
145 delete entry.iObj; |
|
146 entry.iObj = NULL; |
|
147 entry.iNext = iFree; |
|
148 iFree = idx; |
|
149 } |
|
150 } |
|
151 __ASSERT_DEBUG(iFree <= iSize, User::Invariant()); |
|
152 } |
|
153 |
|
154 /** |
|
155 Looks up for an object in the container. |
|
156 |
|
157 @param aHandle Unique object handle |
|
158 |
|
159 @return A pointer to the found object or NULL. |
|
160 */ |
|
161 template <class T> |
|
162 T* RStreamObjContainer<T>::Find(TInt aHandle) const |
|
163 { |
|
164 TEntry* entry = NULL; |
|
165 if(aHandle > 0) //It is a handle, sent by the client. It isn't a server's problem if the handle is <= 0. |
|
166 { |
|
167 TInt idx = MakeIndex(aHandle); |
|
168 if(idx < iSize) |
|
169 { |
|
170 entry = &iEntries[idx]; |
|
171 } |
|
172 } |
|
173 return entry != NULL ? entry->iObj : NULL; |
|
174 } |
|
175 |
|
176 /** |
|
177 Counts the alive objects in the container |
|
178 |
|
179 @return The object count |
|
180 */ |
|
181 template <class T> |
|
182 TInt RStreamObjContainer<T>::Count() const |
|
183 { |
|
184 TInt count = 0;; |
|
185 const TEntry* const base = iEntries; |
|
186 if(base) |
|
187 { |
|
188 for(const TEntry* e=base+iSize;--e>=base;) |
|
189 { |
|
190 if(e->iObj) |
|
191 { |
|
192 ++count; |
|
193 } |
|
194 } |
|
195 } |
|
196 return count; |
|
197 } |