Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / collection / FBaseColQueue.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FBaseColQueue.cpp
19  * @brief               This is the implementation for Queue class.
20  */
21
22 #include <new>
23 #include <unique_ptr.h>
24 #include <FBaseColQueue.h>
25 #include <FBaseResult.h>
26 #include <FBaseSysLog.h>
27
28
29 namespace Tizen { namespace Base { namespace Collection
30 {
31
32 /**
33  * @class       _QueueEnumerator
34  * @brief       This is an implementation of IEnumerator for Queue.
35  */
36 class _QueueEnumerator
37         : public IEnumerator
38         , public Object
39 {
40 public:
41         _QueueEnumerator(const Queue& queue, int modCount);
42         virtual ~_QueueEnumerator(void);
43
44         virtual Object* GetCurrent(void) const;
45         virtual result MoveNext(void);
46         virtual result Reset(void);
47
48 private:
49         const Queue& __queue;
50         int __modCount;
51         int __position;
52 };
53
54 _QueueEnumerator::_QueueEnumerator(const Queue& queue, int modCount)
55         : __queue(queue)
56         , __modCount(modCount)
57         , __position(-1)
58 {
59 }
60
61 _QueueEnumerator::~_QueueEnumerator(void)
62 {
63 }
64
65 Object*
66 _QueueEnumerator::GetCurrent(void) const
67 {
68         SysTryReturn(NID_BASE_COL, __modCount == __queue.__modCount, null, E_INVALID_OPERATION, "[%s] The source collection was modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
69         SysTryReturn(NID_BASE_COL, (__position >= __queue.__tail) && (__position < __queue.__head), null, E_INVALID_OPERATION, "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
70
71         SetLastResult(E_SUCCESS);
72         return __queue.__pObjArray[__position % __queue.__capacity];
73 }
74
75 result
76 _QueueEnumerator::MoveNext(void)
77 {
78         SysTryReturnResult(NID_BASE_COL, __modCount == __queue.__modCount, E_INVALID_OPERATION, "The source collection was modified after the creation of this enumerator.");
79
80         if ((__position + 1) >= __queue.__head)
81         {
82                 return E_OUT_OF_RANGE;
83         }
84
85         if (__position == -1)
86         {
87                 __position = __queue.__tail;
88         }
89         else
90         {
91                 __position++;
92         }
93
94         return E_SUCCESS;
95 }
96
97 result
98 _QueueEnumerator::Reset(void)
99 {
100         SysTryReturnResult(NID_BASE_COL, __modCount == __queue.__modCount, E_INVALID_OPERATION, "The source collection was modified after the creation of this enumerator.");
101
102         __position = -1;
103         return E_SUCCESS;
104 }
105
106 Queue::Queue(void)
107         : __capacity(0)
108         , __head(0)
109         , __tail(0)
110         , __pObjArray(null)
111         , __modCount(0)
112         , __pQueueImpl(null)
113 {
114 }
115
116 Queue::~Queue(void)
117 {
118         __modCount++;
119
120         delete[] __pObjArray;
121 }
122
123 result
124 Queue::Construct(int capacity)
125 {
126         SysTryReturn(NID_BASE_COL, capacity >= 0, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument(s) is used. The capacity(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_INVALID_ARG), capacity);
127
128         result r = E_SUCCESS;
129
130         if (capacity > 0)
131         {
132                 __pObjArray = new (std::nothrow) Object*[capacity];
133                 SysTryCatch(NID_BASE_COL, __pObjArray != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
134         }
135         __capacity = capacity;
136
137         return r;
138
139 CATCH:
140         return r;
141 }
142
143 result
144 Queue::Construct(const ICollection& collection)
145 {
146         result r = E_SUCCESS;
147         int count = collection.GetCount();
148         r = Construct(count);
149         SysTryReturnResult(NID_BASE_COL, r == E_SUCCESS, r, "Propagating.");
150
151         if (count > 0)
152         {
153                 std::unique_ptr< IEnumerator > pEnum(collection.GetEnumeratorN());
154                 SysTryCatch(NID_BASE_COL, pEnum != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
155
156                 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
157                 {
158                         SysTryCatch(NID_BASE_COL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
159
160                         Object* pTemp = pEnum->GetCurrent();
161                         SysTryCatch(NID_BASE_COL, pTemp != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
162
163                         __pObjArray[__head % __capacity] = pTemp;
164                         __head++;
165                 }
166         }
167
168         return E_SUCCESS;
169
170 CATCH:
171         delete[] __pObjArray;
172         __pObjArray = null;
173
174         return r;
175 }
176
177 Object*
178 Queue::Dequeue(void)
179 {
180         SysTryReturn(NID_BASE_COL, __head > __tail, null, E_UNDERFLOW, "[%s] Dequeue operation failed.", GetErrorMessage(E_UNDERFLOW));
181
182         __modCount++;
183
184         SetLastResult(E_SUCCESS);
185         return __pObjArray[(__tail++) % __capacity];
186 }
187
188 result
189 Queue::Enqueue(Object* pObj)
190 {
191         SysTryReturnResult(NID_BASE_COL, pObj != null, E_INVALID_ARG, "Invalid argument used. The pObj is null");
192
193         if (__pObjArray == null)
194         {
195                 __pObjArray = new (std::nothrow) Object*[DEFAULT_CAPACITY];
196                 SysTryReturnResult(NID_BASE_COL, __pObjArray != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
197
198                 __capacity = DEFAULT_CAPACITY;
199         }
200         else if ((__head - __tail) >= __capacity)
201         {
202                 Object** pNewArray = new (std::nothrow) Object*[__capacity + DEFAULT_CAPACITY];
203                 SysTryReturnResult(NID_BASE_COL, pNewArray != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
204                 for (int i = 0, j = __tail; i < __capacity; i++, j++)
205                 {
206                         pNewArray[i] = __pObjArray[j % __capacity];
207                 }
208
209                 delete[] __pObjArray;
210
211                 __pObjArray = pNewArray;
212                 __tail = 0;
213                 __head = __capacity;
214                 __capacity += DEFAULT_CAPACITY;
215         }
216
217         __modCount++;
218
219         __pObjArray[__head % __capacity] = pObj;
220         __head++;
221
222         return E_SUCCESS;
223 }
224
225 IEnumerator*
226 Queue::GetEnumeratorN(void) const
227 {
228         std::unique_ptr< _QueueEnumerator > pEnum(new (std::nothrow) _QueueEnumerator(*this, __modCount));
229         SysTryReturn(NID_BASE_COL, pEnum != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
230
231         SetLastResult(E_SUCCESS);
232         return pEnum.release();
233 }
234
235 const Object*
236 Queue::Peek(void) const
237 {
238         SysTryReturn(NID_BASE_COL, __head > __tail, null, E_UNDERFLOW, "[%s] Peek operation failed.", GetErrorMessage(E_UNDERFLOW));
239
240         SetLastResult(E_SUCCESS);
241         return __pObjArray[__tail % __capacity];
242 }
243
244 void
245 Queue::RemoveAll(bool deallocate)
246 {
247         __modCount++;
248         int count = GetCount();
249         for (int i = 0; i < count; i++)
250         {
251                 if (deallocate)
252                 {
253                         delete __pObjArray[(__tail + i) % __capacity];
254                 }
255
256                 __pObjArray[(__tail + i) % __capacity] = null;
257         }
258
259         __head = 0;
260         __tail = 0;
261 }
262
263 int
264 Queue::GetCount(void) const
265 {
266         return(__head - __tail);
267 }
268
269 bool
270 Queue::Contains(const Object& obj) const
271 {
272         int count = GetCount();
273         for (int i = 0; i < count; i++)
274         {
275                 if (__pObjArray[(__tail + i) % __capacity]->Equals(obj))
276                 {
277                         return true;
278                 }
279         }
280
281         return false;
282 }
283
284 bool
285 Queue::ContainsAll(const ICollection& collection) const
286 {
287         result r = E_SUCCESS;
288         SetLastResult(r);
289
290         std::unique_ptr< IEnumerator > pEnum(collection.GetEnumeratorN());
291         SysTryReturn(NID_BASE_COL, pEnum != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
292
293         while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
294         {
295                 SysTryReturn(NID_BASE_COL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
296
297                 Object* pItem = pEnum->GetCurrent();
298                 SysTryReturn(NID_BASE_COL, pItem != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
299
300                 if (!Contains(*pItem))
301                 {
302                         return false;
303                 }
304         }
305
306         return true;
307 }
308
309 bool
310 Queue::Equals(const Object& obj) const
311 {
312         const Queue* pOther = dynamic_cast< const Queue* >(&obj);
313         if (pOther == null)
314         {
315                 return false;
316         }
317         else if (pOther == this)
318         {
319                 return true;
320         }
321         else if (GetCount() != pOther->GetCount())
322         {
323                 return false;
324         }
325         else
326         {
327                 int count = GetCount();
328                 for (int i = 0; i < count; i++)
329                 {
330                         if (!(__pObjArray[(__tail + i) % __capacity]->Equals(*(pOther->__pObjArray[(pOther->__tail + i) % pOther->__capacity]))))
331                         {
332                                 return false;
333                         }
334                 }
335         }
336
337         return true;
338 }
339
340 int
341 Queue::GetHashCode(void) const
342 {
343         int hash = 0;
344         int count = GetCount();
345         for (int i = 0; i < count; i++)
346         {
347                 if (__pObjArray[(__tail + i) % __capacity] != null)
348                 {
349                         hash += __pObjArray[(__tail + i) % __capacity]->GetHashCode();
350                 }
351         }
352         return hash;
353 }
354 }}}  // Tizen::Base::Collection