2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseColQueueT.h
20 * @brief This is the header file for the %QueueT class.
22 * This header file contains the declarations of the %QueueT class.
25 #ifndef _FBASE_COL_QUEUE_T_H_
26 #define _FBASE_COL_QUEUE_T_H_
28 #include <FBaseObject.h>
29 #include <FBaseResult.h>
30 #include <FBaseColICollectionT.h>
33 namespace Tizen { namespace Base { namespace Collection
36 template< class Type > class __QueueEnumeratorT;
40 * @brief This represents a template-based queue (a first-in-first-out collection of objects).
44 * The %QueueT class represents a template-based queue (a first-in-first-out collection of objects).
46 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/queue_stack.htm">Stack and Queue</a>.
48 * The following example demonstrates how to use the %QueueT class.
53 * using namespace Tizen::Base;
54 * using namespace Tizen::Base::Collection;
57 * MyClass::QueueTSample(void)
59 * QueueT< String > queue;
62 * String str1(L"First");
63 * String str2(L"Second");
64 * String str3(L"Third");
66 * queue.Enqueue(str1);
67 * queue.Enqueue(str2);
68 * queue.Enqueue(str3);
70 * // Reads the element at the beginning
72 * queue.Peek(temp); // temp: "First", queue.GetCount(): 3
74 * // Reads and removes the element at the beginning
75 * queue.Dequeue(temp); // temp: "First", queue.GetCount(): 2
79 template< class Type >
81 : public virtual ICollectionT< Type >
86 * The object is not fully constructed after this constructor is called. For full construction, @n
87 * the Construct() method must be called right after calling this constructor.
101 * This destructor overrides Tizen::Base::Object::~Object().
105 virtual ~QueueT(void)
112 * Initializes this instance of %QueueT with the specified capacity.
116 * @return An error code
117 * @param[in] capacity The number of elements in the queue @n
118 * The default capacity is @c 10.
119 * @exception E_SUCCESS The method is successful.
120 * @exception E_OUT_OF_MEMORY The memory is insufficient.
121 * @exception E_INVALID_ARG The specified input parameter is invalid, or
122 * the specified @c capacity is negative.
124 result Construct(int capacity = DEFAULT_CAPACITY)
126 TryReturn(capacity >= 0, E_INVALID_ARG, "[%s] The capacity(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_INVALID_ARG), capacity);
130 __pObjArray = new Type[capacity];
131 TryReturn(__pObjArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
133 __capacity = capacity;
139 * Initializes this instance of %QueueT that contains the elements of the specified @c collection. @n
140 * The capacity of the queue is the same as the number of elements copied.
144 * @return An error code
145 * @param[in] collection The collection to copy the elements from
146 * @exception E_SUCCESS The method is successful.
147 * @exception E_OUT_OF_MEMORY The memory is insufficient.
148 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
149 * the @c collection is modified during the operation of this method.
152 result Construct(const ICollectionT< Type >& collection)
154 result r = E_SUCCESS;
156 IEnumeratorT< Type >* pEnum = null;
157 if (collection.GetCount() > 0)
159 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
160 pEnum = pCol->GetEnumeratorN();
161 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
166 r = pEnum->MoveNext();
168 if (E_OUT_OF_RANGE == r)
173 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
175 r = pEnum->GetCurrent(temp);
176 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
179 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
200 * Reads and removes the element at the beginning of this queue.
204 * @return An error code
205 * @param[out] obj The element at the beginning of this queue
206 * @exception E_SUCCESS The method is successful.
207 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
208 * this queue is empty.
211 virtual result Dequeue(Type& obj)
213 if (__head <= __tail)
217 int index = (__tail) % __capacity;
218 obj = __pObjArray[index];
226 * Inserts an object at the end of this queue.
230 * @return An error code
231 * @param[in] obj The object to add to this queue
232 * @exception E_SUCCESS The method is successful.
233 * @exception E_OUT_OF_MEMORY The memory is insufficient.
236 virtual result Enqueue(const Type& obj)
238 if (null == __pObjArray)
240 __pObjArray = new Type[DEFAULT_CAPACITY];
241 TryReturn(__pObjArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
242 __capacity = DEFAULT_CAPACITY;
244 else if ((__head - __tail) >= __capacity)
246 Type* pArrayTemp = new Type[__capacity + DEFAULT_CAPACITY];
247 TryReturn(pArrayTemp != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
249 for (int i = 0, j = __tail; i < __capacity; i++, j++)
251 pArrayTemp[i] = __pObjArray[j % __capacity];
254 delete[] __pObjArray;
255 __pObjArray = pArrayTemp;
258 __capacity += DEFAULT_CAPACITY;
262 __pObjArray[__head % __capacity] = obj;
269 * Removes all the elements in this queue.
273 virtual void RemoveAll(void)
275 if (__pObjArray != null)
277 delete[] __pObjArray;
289 * Gets an enumerator of this queue.
293 * @return An enumerator (an instance of the IEnumeratorT derived class) of this queue, @n
294 * else @c null if an exception occurs
295 * @exception E_SUCCESS The method is successful.
296 * @exception E_OUT_OF_MEMORY The memory is insufficient.
297 * @remarks The specific error code can be accessed using the GetLastResult() method.
298 * @see Tizen::Base::Collection::IEnumeratorT
300 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
304 result r = E_SUCCESS;
306 __QueueEnumeratorT< Type >* pEnum = new __QueueEnumeratorT< Type >(*this, __modCount);
307 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
317 * Reads the element at the beginning of this queue without removing it.
321 * @return An error code
322 * @param[out] obj The element at the beginning of this queue
323 * @exception E_SUCCESS The method is successful.
324 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
325 * this queue is empty.
327 virtual result Peek(Type& obj) const
329 if (__head <= __tail)
334 obj = __pObjArray[__tail % __capacity];
340 * Gets the number of objects currently stored in this queue.
344 * @return The number of objects currently stored in this queue
346 virtual int GetCount(void) const
348 return __head - __tail;
352 * Checks whether this queue contains the specified object.
356 * @return @c true if this queue contains the specified object, @n
358 * @param[in] obj The object to locate
360 virtual bool Contains(const Type& obj) const
363 for (int i = 0; i < GetCount(); i++)
365 if (__pObjArray[(__tail + i) % __capacity] == obj)
376 * Checks whether this queue contains all of the elements in the specified collection.
380 * @return An error code
381 * @param[in] collection The collection to locate
382 * @param[out] out Set to @c true if this queue contains all of the elements in the specified collection, @n
384 * @exception E_SUCCESS The method is successful.
385 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
386 * the specified @c collection is modified during the operation of this method.
387 * @exception E_OUT_OF_MEMORY The memory is insufficient.
389 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
391 result r = E_SUCCESS;
393 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
394 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
395 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
400 r = pEnum->MoveNext();
402 if (E_OUT_OF_RANGE == r)
408 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
410 r = pEnum->GetCurrent(temp);
411 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
413 if (false == Contains(temp))
435 * Compares the specified instance to the current instance for equality.
439 * @return @c true if the specified instance equals the current instance, @n
441 * @param[in] obj The object to compare with the current instance
442 * @remarks This method returns @c true if and only if the specified object is also an instance of %QueueT class,
443 * both queues have the same size, and all corresponding pairs of elements in the two queues are equal.
444 * In other words, two queues are equal if they contain the same elements in the same order.
446 virtual bool Equals(const Object& obj) const
450 const QueueT< Type >* other = dynamic_cast< const QueueT< Type >* >(&obj);
455 else if (other == this)
459 else if (GetCount() != other->GetCount())
465 for (int i = 0; i < GetCount(); i++)
467 if (!(__pObjArray[(__tail + i) % __capacity] == other->__pObjArray[(other->__tail + i) % other->__capacity]))
479 * Gets the hash value of the current instance.
483 * @return The hash value of the current instance
484 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
485 * the used hash function must generate a random distribution for all inputs.
487 virtual int GetHashCode(void) const
490 int count = GetCount();
491 for (int i = 0; i < count; i++)
493 if (&(__pObjArray[(__tail + i) % __capacity]) != null)
495 hash += reinterpret_cast< int >(&(__pObjArray[(__tail + i) % __capacity]));
503 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
505 * @param[in] queue The specified instance of %QueueT to initialize the current instance
507 QueueT(const QueueT< Type >& queue);
510 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
512 * @param[in] queue An instance of %QueueT
514 QueueT< Type >& operator =(const QueueT< Type >& queue);
521 static const int DEFAULT_CAPACITY = 10;
523 friend class __QueueEnumeratorT< Type >;
528 // @class __QueueEnumeratorT
529 // @brief This class is an implementation of IEnumeratorT for %QueueT.
532 template< class Type >
533 class __QueueEnumeratorT
534 : public IEnumeratorT< Type >
539 * Initializes this instance of __QueueEnumeratorT with the specified parameters.
543 * @param[in] queue A queue to enumerate
544 * @param[in] modeCount The modification count to detect the change in the queue
546 __QueueEnumeratorT(const QueueT< Type >& queue, int modeCount)
548 , __modCount(modeCount)
554 * This is the destructor for this class.
558 virtual ~__QueueEnumeratorT(void)
563 * Gets the current object in the queue.
567 * @return An error code
568 * @param[out] obj The current object
569 * @exception E_INVALID_OPERATION Either of the following conditions has occurred: @n
570 * - The current state of the instance prohibits the execution of the specified operation. @n
571 * - This enumerator is currently positioned before the first element or
572 * past the last element. @n
573 * - The queue is modified after this enumerator is created.
574 * @exception E_SUCCESS The method is successful.
576 result GetCurrent(Type& obj) const
578 TryReturn((__modCount == __queue.__modCount), E_INVALID_OPERATION,
579 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
580 TryReturn((__position >= __queue.__tail) && (__position < __queue.__head), E_INVALID_OPERATION,
581 "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
583 obj = __queue.__pObjArray[__position % __queue.__capacity];
589 * Moves this enumerator to the next element of the queue. @n
590 * When this enumerator is first created or after the call to Reset(),
591 * the first call to MoveNext() positions this enumerator to the first element in the queue.
595 * @return An error code
596 * @exception E_SUCCESS The method is successful.
597 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
598 * the queue is modified after this enumerator is created.
599 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the queue.
602 result MoveNext(void)
604 TryReturn((__modCount == __queue.__modCount), E_INVALID_OPERATION,
605 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
607 if ((__position + 1) >= __queue.__head)
609 return E_OUT_OF_RANGE;
613 if (__position == -1)
615 __position = __queue.__tail;
627 * Positions this enumerator before the first element in the queue.
631 * @return An error code
632 * @exception E_SUCCESS The method is successful.
633 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
634 * the queue is modified after this enumerator is created.
638 TryReturn((__modCount == __queue.__modCount), E_INVALID_OPERATION,
639 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
647 const QueueT< Type >& __queue;
651 }; // __QueueEnumeratorT
653 }}} // Tizen::Base::Collection
655 #endif //_FBASE_COL_QUEUE_T_H_