2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FBaseColQueue.h
19 * @brief This is the header file for the %Queue class.
21 * This header file contains the declarations of the %Queue class.
24 #ifndef _FBASE_COL_QUEUE_H_
25 #define _FBASE_COL_QUEUE_H_
27 #include <FBaseObject.h>
28 #include <FBaseTypes.h>
29 #include <FBaseColICollection.h>
31 namespace Tizen { namespace Base { namespace Collection
35 * @brief This class represents a first-in-first-out collection of objects, that is, a queue.
39 * The %Queue class represents a first-in-first-out collection of objects, that is, a queue.
41 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/queue_stack.htm">Stack and Queue</a>.
43 * The following example demonstrates how to use the %Queue class.
48 * using namespace Tizen::Base;
49 * using namespace Tizen::Base::Collection;
52 * MyClass::QueueSample(void)
55 * queue.Construct(3); // capacity = 3
57 * queue.Enqueue(new String(L"First"));
58 * queue.Enqueue(new String(L"Second"));
59 * queue.Enqueue(new String(L"Third"));
61 * // Reads the element at the beginning
62 * const Object* pObj = queue.Peek(); // pObj: "First", queue.GetCount(): 3
64 * // Reads and removes the element at the beginning
65 * String* pStr = static_cast< String* > (queue.Dequeue()); // pStr: "First", queue.GetCount(): 2
67 * delete pStr; // Because the queue does not have the Ownership of this pStr after dequeueing
69 * // Deallocates all objects
70 * queue.RemoveAll(true);
74 class _OSP_EXPORT_ Queue
75 : public virtual ICollection
80 * The object is not fully constructed after this constructor is called. For full construction, @n
81 * the Construct() method must be called right after calling this constructor.
88 * This destructor overrides Tizen::Base::Object::~Object().
95 * Initializes an instance of %Queue with the specified capacity.
99 * @return An error code
100 * @param[in] capacity The number of elements in the queue @n
101 * The default capacity is @c 10.
102 * @exception E_SUCCESS The method is successful.
103 * @exception E_INVALID_ARG The specified input parameter is invalid, or
104 * the specified @c capacity is negative.
105 * @remarks If the number of elements added to the queue reaches the current capacity,
106 * the capacity is automatically increased by memory reallocation.
107 * Therefore, if the size of the queue can be estimated,
108 * specifying the initial capacity eliminates the need to perform a number of
109 * resizing operations while adding elements to the queue.
112 result Construct(int capacity = DEFAULT_CAPACITY);
115 * Initializes an instance of %Queue with the elements of the given collection. @n
116 * The capacity of the queue is the same as the number of elements copied.
120 * @return An error code
121 * @param[in] collection The collection to copy elements from
122 * @exception E_SUCCESS The method is successful.
123 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
124 * the @c collection is modified during the operation of this method.
125 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
128 result Construct(const ICollection& collection);
131 * Removes the element at the beginning of this queue and returns it.
135 * @return The element at the beginning of this queue, @n
136 * else @c null if this queue is empty
137 * @exception E_SUCCESS The method is successful.
138 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
139 * this queue is empty.
140 * @remarks The specific error code can be accessed using the GetLastResult() method.
141 * @see Enqueue(Object*)
143 virtual Object* Dequeue(void);
147 * Inserts an object at the end of this queue.
149 * @brief <i> [Deprecated] </i>
150 * @deprecated This method is deprecated because it has a problem of const reference argument.
151 * Instead of using this method, use Enqueue(Object* pObj).
154 * @return An error code
155 * @param[in] obj The object to add to this queue
156 * @exception E_SUCCESS The method is successful.
157 * @exception E_OUT_OF_MEMORY The memory is insufficient.
158 * @remarks This method performs a shallow copy. It inserts just the pointer; not the element itself.
162 result Enqueue(const Object& obj)
164 return Enqueue(const_cast< Object* >(&obj));
168 * Inserts an object at the end of this queue.
172 * @return An error code
173 * @param[in] pObj The pointer to object to add to this queue
174 * @exception E_SUCCESS The method is successful.
175 * @exception E_INVALID_ARG The specified input parameter is invalid.
176 * @remarks This method performs a shallow copy. It inserts just the pointer; not the element itself.
179 virtual result Enqueue(Object* pObj);
182 * Gets an enumerator of this queue.
186 * @return An enumerator (an instance of the IEnumerator derived class) of this queue, @n
187 * else @c null if an exception occurs
188 * @remarks The specific error code can be accessed using the GetLastResult() method.
191 virtual IEnumerator* GetEnumeratorN(void) const;
194 * Gets the element at the front of this queue without removing it.
198 * @return The element at the beginning of this queue, @n
199 * else @c null if this queue is empty
200 * @exception E_SUCCESS The method is successful.
201 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
202 * this queue is empty.
203 * @remarks The specific error code can be accessed using the GetLastResult() method.
205 virtual const Object* Peek(void) const;
208 * Removes all objects and their pointers in collection, when the @c deallocate parameter is set to @c true.
212 * @param[in] deallocate Set to @c true to deallocate all objects, @n
214 * @remarks This method can be called before deleting the collection.
216 virtual void RemoveAll(bool deallocate = false);
219 * Gets the number of objects currently stored in this queue.
223 * @return The number of objects currently stored in this queue
225 virtual int GetCount(void) const;
228 * Checks whether this queue contains the specified object.
232 * @return @c true if this queue contains the specified object, @n
234 * @param[in] obj The object to locate
236 virtual bool Contains(const Object& obj) const;
240 * Checks whether this queue contains all the elements in the specified collection.
242 * @brief <i> [Deprecated] </i>
243 * @deprecated This method is deprecated because it transfers a result of comparison in out-parameter form.
244 * The return type will be changed into boolean type and this method will return the result.
245 * Instead of using this method, use bool ContainsAll(const ICollection& collection).
248 * @return An error code
249 * @param[in] collection The collection to locate
250 * @param[out] out Set to @c true if this queue contains all the elements in the specified collection, @n
252 * @exception E_SUCCESS The method is successful.
253 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
254 * the @c collection is modified during the operation of this method.
257 result ContainsAll(const ICollection& collection, bool& out) const
259 out = ContainsAll(collection);
260 result r = GetLastResult();
265 * Checks whether this queue contains all the elements in the specified collection.
269 * @return @c true if this queue contains all the elements in the specified collection, @n
271 * @param[in] collection The collection to locate
272 * @exception E_SUCCESS The method is successful.
273 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
274 * the @c collection is modified during the operation of this method.
275 * @remarks The specific error code can be accessed using the GetLastResult() method.
277 virtual bool ContainsAll(const ICollection& collection) const;
280 * Compares the specified instance to the current instance for equality.
284 * @return @c true if the specified instance equals the current instance, @n
286 * @param[in] obj The object to compare with the current instance
287 * @remarks This method returns @c true if and only if the specified object is also an instance of %Queue class,
288 * both queues have the same size, and all corresponding pairs of elements in the two queues are equal.
289 * In other words, two queues are equal if they contain the same elements in the same order.
291 virtual bool Equals(const Object& obj) const;
294 * Gets the hash value of the current instance.
298 * @return The hash value of the current instance
299 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
300 * the used hash function must generate a random distribution for all inputs.
302 virtual int GetHashCode(void) const;
306 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
308 // @param[in] queue The specified instance of %Queue to initialize the current instance
310 Queue(const Queue& queue);
313 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
315 // @param[in] queue An instance of %Queue
317 Queue& operator =(const Queue& queue);
322 Object** __pObjArray;
324 static const int DEFAULT_CAPACITY = 10;
326 friend class _QueueEnumerator;
327 friend class _QueueImpl;
328 class _QueueImpl* __pQueueImpl;
332 }}} // Tizen::Collection
334 #endif // _FBASE_COL_QUEUE_H_