|
1 // Copyright (c) 2002-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 // $Header$ |
|
15 // Noticed lots of classes want to 'pile' up objects (as in stack 'em) but |
|
16 // all create their own 'class'. Stuff that for a game of soldiers... |
|
17 // here's a templated class that serves all! |
|
18 // mjd, mark@mjdss.com, july 2002 |
|
19 // |
|
20 // |
|
21 |
|
22 #include "CPile.h" |
|
23 |
|
24 //----------------------------------------------------------------------------- |
|
25 // the NewL calls default values (e.g. 16) in ConstructL |
|
26 |
|
27 template <class T> |
|
28 CPile<T> * CPile<T>::NewL() |
|
29 { |
|
30 CPile* self = NewLC(); |
|
31 CleanupStack::Pop(); |
|
32 return self; |
|
33 } |
|
34 |
|
35 //----------------------------------------------------------------------------- |
|
36 // the NewL calls passes desired default size to ConstructL |
|
37 |
|
38 template <class T> |
|
39 CPile<T> * CPile<T>::NewL(const TInt &aElements) |
|
40 { |
|
41 CPile *self = NewLC(aElements); |
|
42 CleanupStack::Pop(); |
|
43 return self; |
|
44 } |
|
45 |
|
46 //----------------------------------------------------------------------------- |
|
47 |
|
48 template <class T> |
|
49 CPile<T> * CPile<T>::NewLC(const TInt &aElements) |
|
50 { |
|
51 CPile *self = new (ELeave) CPile(); |
|
52 CleanupStack::PushL(self); |
|
53 self->ConstructL(aElements); |
|
54 return self; |
|
55 } |
|
56 |
|
57 //----------------------------------------------------------------------------- |
|
58 // destroy the pile of T! |
|
59 // not forgetting to delete the stacks contents first! |
|
60 |
|
61 template <class T> |
|
62 CPile<T>::~CPile() |
|
63 { |
|
64 iPile->ResetAndDestroy(); |
|
65 delete iPile; |
|
66 iPile = NULL; |
|
67 } |
|
68 |
|
69 //----------------------------------------------------------------------------- |
|
70 // would be nice to be able to easily specify in the constructor |
|
71 // the size of the initial stack... |
|
72 // mjd: added in the aElements which is now passed (via default) from NewL |
|
73 |
|
74 template <class T> |
|
75 void CPile<T>::ConstructL(const TInt &aElements) |
|
76 { |
|
77 iPile = new (ELeave) CArrayPtrSeg<T>(aElements); |
|
78 } |
|
79 |
|
80 //----------------------------------------------------------------------------- |
|
81 // ok, push an object of class T onto the stack |
|
82 |
|
83 template <class T> |
|
84 void CPile<T>::PushL(T *aT) |
|
85 { |
|
86 iPile->AppendL(aT); |
|
87 } |
|
88 |
|
89 //----------------------------------------------------------------------------- |
|
90 // pop an object of class T off the top of the pile (stack) or |
|
91 // NULL if nothing to get... |
|
92 // note: Count is always 1 more than actual contents of stack... |
|
93 |
|
94 template <class T> |
|
95 T *CPile<T>::Pop() |
|
96 { |
|
97 TInt count = Count(); |
|
98 if (count > 0) |
|
99 { |
|
100 T *t = At(--count); |
|
101 iPile->Delete(count); |
|
102 return t; |
|
103 } |
|
104 else |
|
105 return NULL; |
|
106 } |
|
107 |
|
108 //----------------------------------------------------------------------------- |
|
109 // zap the objcet of class T sitting at the top of the stack |
|
110 |
|
111 template <class T> |
|
112 void CPile<T>::Skim() |
|
113 { |
|
114 T *t = Pop(); |
|
115 delete t; |
|
116 } |
|
117 |
|
118 //----------------------------------------------------------------------------- |
|
119 // zap all objects of class T sitting on the stack |
|
120 |
|
121 template <class T> |
|
122 void CPile<T>::Empty() |
|
123 { |
|
124 while (iPile->Count()) |
|
125 Skim(); |
|
126 } |
|
127 |
|
128 //----------------------------------------------------------------------------- |
|
129 // How many do we have - this SHOULD be a property! |
|
130 |
|
131 template <class T> |
|
132 TInt CPile<T>::Count() const |
|
133 { |
|
134 return iPile->Count(); |
|
135 } |
|
136 |
|
137 //----------------------------------------------------------------------------- |
|
138 // get the n'th item in the list |
|
139 // indexed from 0 so [0] is valid. |
|
140 |
|
141 template <class T> |
|
142 T *CPile<T>::At(const TInt &aIndex) const |
|
143 { |
|
144 if ((aIndex >= 0) || (aIndex < Count())) |
|
145 return iPile->At(aIndex); |
|
146 return NULL; |
|
147 } |
|
148 |
|
149 //----------------------------------------------------------------------------- |
|
150 // get the [n]'th item in the list |
|
151 |
|
152 template <class T> |
|
153 T *CPile<T>::operator[](const TInt &aIndex) const |
|
154 { |
|
155 return At(aIndex); |
|
156 } |
|
157 |
|
158 //----------------------------------------------------------------------------- |
|
159 |
|
160 template <class T> |
|
161 T *CPile<T>::Peek() |
|
162 { |
|
163 TInt count = Count(); |
|
164 if (count > 0) |
|
165 { |
|
166 T *t = At(--count); |
|
167 return t; |
|
168 } |
|
169 |
|
170 return NULL; |
|
171 } |
|
172 //----------------------------------------------------------------------------- |
|
173 // Template Specialization requirements: CLogFile |
|
174 //----------------------------------------------------------------------------- |
|
175 |
|
176 CPile<CLogFile> * CPile<CLogFile>::NewL() |
|
177 { |
|
178 CPile *self = NewLC(); |
|
179 CleanupStack::Pop(); |
|
180 return self; |
|
181 } |
|
182 |
|
183 //----------------------------------------------------------------------------- |
|
184 |
|
185 CPile<CLogFile> *CPile<CLogFile>::NewLC(const TInt &aElements) |
|
186 { |
|
187 CPile *self = new (ELeave) CPile(); |
|
188 CleanupStack::PushL(self); |
|
189 self->ConstructL(aElements); |
|
190 return self; |
|
191 } |
|
192 |
|
193 //----------------------------------------------------------------------------- |
|
194 |
|
195 void CPile<CLogFile>::ConstructL(const TInt &aElements) |
|
196 { |
|
197 iPile = new (ELeave) CArrayPtrSeg<CLogFile>(aElements); |
|
198 } |
|
199 |
|
200 //----------------------------------------------------------------------------- |
|
201 |
|
202 void CPile<CLogFile>::PushL(CLogFile *aT) |
|
203 { |
|
204 iPile->AppendL(aT); |
|
205 } |
|
206 |
|
207 //----------------------------------------------------------------------------- |
|
208 |
|
209 CLogFile *CPile<CLogFile>::Pop() |
|
210 { |
|
211 TInt count = Count(); |
|
212 if (count > 0) |
|
213 { |
|
214 CLogFile *t = At(--count); |
|
215 iPile->Delete(count); |
|
216 return t; |
|
217 } |
|
218 else |
|
219 return NULL; |
|
220 } |
|
221 |
|
222 //----------------------------------------------------------------------------- |
|
223 |
|
224 CLogFile *CPile<CLogFile>::At(const TInt &aIndex) const |
|
225 { |
|
226 if ((aIndex >= 0) || (aIndex < Count())) |
|
227 return (CLogFile *)iPile->At(aIndex); |
|
228 //return (CLogFile *) &iPile[aIndex]; |
|
229 return NULL; |
|
230 } |
|
231 |
|
232 //----------------------------------------------------------------------------- |
|
233 |
|
234 TInt CPile<CLogFile>::Count() const |
|
235 { |
|
236 return iPile->Count(); |
|
237 } |
|
238 |
|
239 //----------------------------------------------------------------------------- |
|
240 |
|
241 CPile<CLogFile>::~CPile() |
|
242 { |
|
243 iPile->ResetAndDestroy(); |
|
244 delete iPile; |
|
245 iPile = NULL; |
|
246 } |
|
247 |
|
248 |
|
249 //----------------------------------------------------------------------------- |
|
250 // Template Specialization requirements: CCmdFile |
|
251 //----------------------------------------------------------------------------- |
|
252 |
|
253 CPile<CCmdFile> *CPile<CCmdFile>::NewL() |
|
254 { |
|
255 CPile *self = NewLC(); |
|
256 CleanupStack::Pop(); |
|
257 return self; |
|
258 } |
|
259 |
|
260 //----------------------------------------------------------------------------- |
|
261 |
|
262 CPile<CCmdFile> *CPile<CCmdFile>::NewLC(const TInt &aElements) |
|
263 { |
|
264 CPile *self = new (ELeave) CPile(); |
|
265 CleanupStack::PushL(self); |
|
266 self->ConstructL(aElements); |
|
267 return self; |
|
268 } |
|
269 |
|
270 //----------------------------------------------------------------------------- |
|
271 |
|
272 CPile<CCmdFile>::~CPile() |
|
273 { |
|
274 iPile->ResetAndDestroy(); |
|
275 delete iPile; |
|
276 iPile = NULL; |
|
277 } |
|
278 |
|
279 //----------------------------------------------------------------------------- |
|
280 |
|
281 void CPile<CCmdFile>::ConstructL(const TInt &aElements) |
|
282 { |
|
283 iPile = new (ELeave) CArrayPtrSeg<CCmdFile>(aElements); |
|
284 } |
|
285 |
|
286 //----------------------------------------------------------------------------- |
|
287 |
|
288 TInt CPile<CCmdFile>::Count() const |
|
289 { |
|
290 return iPile->Count(); |
|
291 } |
|
292 |
|
293 //----------------------------------------------------------------------------- |
|
294 |
|
295 CCmdFile *CPile<CCmdFile>::At(const TInt &aIndex) const |
|
296 { |
|
297 if ((aIndex >= 0) || (aIndex < Count())) |
|
298 return (CCmdFile *)iPile->At(aIndex); |
|
299 //return (CCmdFile *) &iPile[aIndex]; |
|
300 return NULL; |
|
301 } |
|
302 |
|
303 //----------------------------------------------------------------------------- |
|
304 |
|
305 void CPile<CCmdFile>::PushL(CCmdFile *aT) |
|
306 { |
|
307 iPile->AppendL(aT); |
|
308 } |
|
309 |
|
310 //----------------------------------------------------------------------------- |
|
311 |
|
312 CCmdFile *CPile<CCmdFile>::Pop() |
|
313 { |
|
314 TInt count = Count(); |
|
315 if (count > 0) |
|
316 { |
|
317 CCmdFile *t = At(--count); |
|
318 iPile->Delete(count); |
|
319 return t; |
|
320 } |
|
321 else |
|
322 return NULL; |
|
323 } |
|
324 |
|
325 //----------------------------------------------------------------------------- |
|
326 |
|
327 void CPile<CCmdFile>::Skim() |
|
328 { |
|
329 CCmdFile *t = Pop(); |
|
330 delete t; |
|
331 } |
|
332 |
|
333 //----------------------------------------------------------------------------- |
|
334 |
|
335 void CPile<CCmdFile>::Empty() |
|
336 { |
|
337 while (iPile->Count()) |
|
338 Skim(); |
|
339 } |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 //----------------------------------------------------------------------------- |
|
345 //----------------------------------------------------------------------------- |
|
346 // Template Specialization requirements: CIFControl |
|
347 //----------------------------------------------------------------------------- |
|
348 //----------------------------------------------------------------------------- |
|
349 |
|
350 CPile<CIFControl> *CPile<CIFControl>::NewL() |
|
351 { |
|
352 CPile *self = NewLC(); |
|
353 CleanupStack::Pop(); |
|
354 return self; |
|
355 } |
|
356 |
|
357 //----------------------------------------------------------------------------- |
|
358 |
|
359 CPile<CIFControl> *CPile<CIFControl>::NewLC(const TInt &aElements) |
|
360 { |
|
361 CPile *self = new (ELeave) CPile(); |
|
362 CleanupStack::PushL(self); |
|
363 self->ConstructL(aElements); |
|
364 return self; |
|
365 } |
|
366 |
|
367 //----------------------------------------------------------------------------- |
|
368 |
|
369 void CPile<CIFControl>::ConstructL(const TInt &aElements) |
|
370 { |
|
371 iPile = new (ELeave) CArrayPtrSeg<CIFControl>(aElements); |
|
372 } |
|
373 |
|
374 //----------------------------------------------------------------------------- |
|
375 |
|
376 CIFControl *CPile<CIFControl>::At(const TInt &aIndex) const |
|
377 { |
|
378 if ((aIndex >= 0) || (aIndex < Count())) |
|
379 return (CIFControl *)iPile->At(aIndex); |
|
380 return NULL; |
|
381 } |
|
382 |
|
383 //----------------------------------------------------------------------------- |
|
384 |
|
385 void CPile<CIFControl>::PushL(CIFControl *aT) |
|
386 { |
|
387 iPile->AppendL(aT); |
|
388 } |
|
389 |
|
390 //----------------------------------------------------------------------------- |
|
391 |
|
392 TInt CPile<CIFControl>::Count() const |
|
393 { |
|
394 return iPile->Count(); |
|
395 } |
|
396 |
|
397 //----------------------------------------------------------------------------- |
|
398 |
|
399 CIFControl *CPile<CIFControl>::Pop() |
|
400 { |
|
401 TInt count = Count(); |
|
402 if (count > 0) |
|
403 { |
|
404 CIFControl *t = At(--count); |
|
405 iPile->Delete(count); |
|
406 return t; |
|
407 } |
|
408 |
|
409 return NULL; |
|
410 } |
|
411 |
|
412 void CPile<CIFControl>::Skim() |
|
413 { |
|
414 CIFControl *t = Pop(); |
|
415 delete t; |
|
416 } |
|
417 |
|
418 //----------------------------------------------------------------------------- |
|
419 |
|
420 CIFControl *CPile<CIFControl>::Peek() |
|
421 { |
|
422 TInt count = Count(); |
|
423 if (count > 0) |
|
424 { |
|
425 CIFControl *t = At(--count); |
|
426 return t; |
|
427 } |
|
428 |
|
429 return NULL; |
|
430 } |
|
431 |
|
432 |
|
433 //----------------------------------------------------------------------------- |
|
434 |
|
435 CPile<CIFControl>::~CPile() |
|
436 { |
|
437 iPile->ResetAndDestroy(); |
|
438 delete iPile; |
|
439 iPile = NULL; |
|
440 } |
|
441 |
|
442 //----------------------------------------------------------------------------- |
|
443 // End of File |
|
444 //----------------------------------------------------------------------------- |