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 FBaseColQueue.h
20 * @brief This is the header file for the %Queue class.
22 * This header file contains the declarations of the %Queue class.
25 #ifndef _FBASE_COL_QUEUE_H_
26 #define _FBASE_COL_QUEUE_H_
28 #include <FBaseObject.h>
29 #include <FBaseTypes.h>
30 #include <FBaseColICollection.h>
33 namespace Tizen { namespace Base { namespace Collection
37 * @brief This class represents a first-in-first-out collection of objects, that is, a queue.
41 * The %Queue class represents a first-in-first-out collection of objects, that is, a queue.
43 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/queue_stack.htm">Stack and Queue</a>.
45 * The following example demonstrates how to use the %Queue class.
50 * using namespace Tizen::Base;
51 * using namespace Tizen::Base::Collection;
54 * MyClass::QueueSample(void)
57 * queue.Construct(3); // capacity = 3
59 * queue.Enqueue(new String(L"First"));
60 * queue.Enqueue(new String(L"Second"));
61 * queue.Enqueue(new String(L"Third"));
63 * // Reads the element at the beginning
64 * const Object* pObj = queue.Peek(); // pObj: "First", queue.GetCount(): 3
66 * // Reads and removes the element at the beginning
67 * String* pStr = static_cast< String* > (queue.Dequeue()); // pStr: "First", queue.GetCount(): 2
69 * delete pStr; // Because the queue does not have the Ownership of this pStr after dequeueing
71 * // Deallocates all objects
72 * queue.RemoveAll(true);
76 class _OSP_EXPORT_ Queue
77 : public virtual ICollection
82 * The object is not fully constructed after this constructor is called. For full construction, @n
83 * the Construct() method must be called right after calling this constructor.
90 * This destructor overrides Tizen::Base::Object::~Object().
97 * Initializes an instance of %Queue with the specified capacity.
101 * @return An error code
102 * @param[in] capacity The number of elements in the queue @n
103 * The default capacity is @c 10.
104 * @exception E_SUCCESS The method is successful.
105 * @exception E_INVALID_ARG The specified input parameter is invalid, or
106 * the specified @c capacity is negative.
107 * @remarks If the number of elements added to the queue reaches the current capacity,
108 * the capacity is automatically increased by memory reallocation.
109 * Therefore, if the size of the queue can be estimated,
110 * specifying the initial capacity eliminates the need to perform a number of
111 * resizing operations while adding elements to the queue.
114 result Construct(int capacity = DEFAULT_CAPACITY);
117 * Initializes an instance of %Queue with the elements of the given collection. @n
118 * The capacity of the queue is the same as the number of elements copied.
122 * @return An error code
123 * @param[in] collection The collection to copy elements from
124 * @exception E_SUCCESS The method is successful.
125 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
126 * the @c collection is modified during the operation of this method.
127 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
130 result Construct(const ICollection& collection);
133 * Removes the element at the beginning of this queue and returns it.
137 * @return The element at the beginning of this queue, @n
138 * else @c null if this queue is empty
139 * @exception E_SUCCESS The method is successful.
140 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
141 * this queue is empty.
142 * @remarks The specific error code can be accessed using the GetLastResult() method.
145 virtual Object* Dequeue(void);
149 * Inserts an object at the end of this queue.
151 * @brief <i> [Deprecated] </i>
152 * @deprecated This method is deprecated because it has a problem of const reference argument.
153 * Instead of using this method, use Enqueue(Object* pObj).
156 * @return An error code
157 * @param[in] obj The object to add to this queue
158 * @exception E_SUCCESS The method is successful.
159 * @exception E_OUT_OF_MEMORY The memory is insufficient.
160 * @remarks This method performs a shallow copy. It inserts just the pointer; not the element itself.
164 result Enqueue(const Object& obj)
166 return Enqueue(const_cast< Object* >(&obj));
170 * Inserts an object at the end of this queue.
174 * @return An error code
175 * @param[in] pObj The pointer to object to add to this queue
176 * @exception E_SUCCESS The method is successful.
177 * @exception E_INVALID_ARG The specified input parameter is invalid.
178 * @remarks This method performs a shallow copy. It inserts just the pointer; not the element itself.
181 virtual result Enqueue(Object* pObj);
184 * Gets an enumerator of this queue.
188 * @return An enumerator (an instance of the IEnumerator derived class) of this queue, @n
189 * else @c null if an exception occurs
190 * @remarks The specific error code can be accessed using the GetLastResult() method.
193 virtual IEnumerator* GetEnumeratorN(void) const;
196 * Gets the element at the front of this queue without removing it.
200 * @return The element at the beginning of this queue, @n
201 * else @c null if this queue is empty
202 * @exception E_SUCCESS The method is successful.
203 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
204 * this queue is empty.
205 * @remarks The specific error code can be accessed using the GetLastResult() method.
207 virtual const Object* Peek(void) const;
210 * Removes all objects and their pointers in collection, when the @c deallocate parameter is set to @c true.
214 * @param[in] deallocate Set to @c true to deallocate all objects, @n
216 * @remarks This method can be called before deleting the collection.
218 virtual void RemoveAll(bool deallocate = false);
221 * Gets the number of objects currently stored in this queue.
225 * @return The number of objects currently stored in this queue
227 virtual int GetCount(void) const;
230 * Checks whether this queue contains the specified object.
234 * @return @c true if this queue contains the specified object, @n
236 * @param[in] obj The object to locate
238 virtual bool Contains(const Object& obj) const;
242 * Checks whether this queue contains all the elements in the specified collection.
244 * @brief <i> [Deprecated] </i>
245 * @deprecated This method is deprecated because it transfers a result of comparison in out-parameter form.
246 * The return type will be changed into boolean type and this method will return the result.
247 * Instead of using this method, use bool ContainsAll(const ICollection& collection).
250 * @return An error code
251 * @param[in] collection The collection to locate
252 * @param[out] out Set to @c true if this queue contains all the elements in the specified collection, @n
254 * @exception E_SUCCESS The method is successful.
255 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
256 * the @c collection is modified during the operation of this method.
259 result ContainsAll(const ICollection& collection, bool& out) const
261 out = ContainsAll(collection);
262 result r = GetLastResult();
267 * Checks whether this queue contains all the elements in the specified collection.
271 * @return @c true if this queue contains all the elements in the specified collection, @n
273 * @param[in] collection The collection to locate
274 * @exception E_SUCCESS The method is successful.
275 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
276 * the @c collection is modified during the operation of this method.
277 * @remarks The specific error code can be accessed using the GetLastResult() method.
279 virtual bool ContainsAll(const ICollection& collection) const;
282 * Compares the specified instance to the current instance for equality.
286 * @return @c true if the specified instance equals the current instance, @n
288 * @param[in] obj The object to compare with the current instance
289 * @remarks This method returns @c true if and only if the specified object is also an instance of %Queue class,
290 * both queues have the same size, and all corresponding pairs of elements in the two queues are equal.
291 * In other words, two queues are equal if they contain the same elements in the same order.
293 virtual bool Equals(const Object& obj) const;
296 * Gets the hash value of the current instance.
300 * @return The hash value of the current instance
301 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
302 * the used hash function must generate a random distribution for all inputs.
304 virtual int GetHashCode(void) const;
308 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
310 // @param[in] queue The specified instance of %Queue to initialize the current instance
312 Queue(const Queue& queue);
315 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
317 // @param[in] queue An instance of %Queue
319 Queue& operator =(const Queue& queue);
324 Object** __pObjArray;
326 static const int DEFAULT_CAPACITY = 10;
328 friend class _QueueEnumerator;
329 friend class _QueueImpl;
330 class _QueueImpl* __pQueueImpl;
334 }}} // Tizen::Collection
336 #endif // _FBASE_COL_QUEUE_H_