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 FBaseColStack.h
20 * @brief This is the header file for the %Stack class.
22 * This header file contains the declarations of the %Stack class.
25 #ifndef _FBASE_COL_STACK_H_
26 #define _FBASE_COL_STACK_H_
28 #include <FBaseObject.h>
29 #include <FBaseTypes.h>
30 #include <FBaseColICollection.h>
33 namespace Tizen { namespace Base { namespace Collection
38 * @brief This class represents a simple last-in-first-out collection of objects, that is, a stack.
42 * The %Stack class represents a simple last-in-first-out collection of objects, that is, a stack.
44 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/queue_stack.htm">Stack and Queue</a>.
46 * The following example demonstrates how to use the %Stack class.
51 * using namespace Tizen::Base;
52 * using namespace Tizen::Base::Collection;
55 * MyClass::StackSample(void)
58 * stack.Construct(3); // capacity = 3
60 * stack.Push(new String(L"First"));
61 * stack.Push(new String(L"Second"));
62 * stack.Push(new String(L"Third"));
64 * // Reads the element at the top
65 * const Object* pObj = stack.Peek(); // pObj: "Third", stack.GetCount(): 3
67 * // Reads and removes the element at the top
68 * String* pStr = static_cast< String* > (stack.Pop()); // pStr: "Third", stack.GetCount(): 2
70 * delete pStr; // Because the stack does not have the ownership of this pStr after popping
72 * // Deallocates all objects
73 * stack.RemoveAll(true);
77 class _OSP_EXPORT_ Stack
78 : public virtual ICollection
83 * The object is not fully constructed after this constructor is called. For full construction, @n
84 * the Construct() method must be called right after calling this constructor.
92 * This destructor overrides Tizen::Base::Object::~Object().
100 * Initializes this instance of %Stack with the specified capacity.
104 * @return An error code
105 * @param[in] capacity The number of elements @n
106 * The default capacity is @c 10.
107 * @exception E_SUCCESS The method is successful.
108 * @exception E_INVALID_ARG The specified input parameter is invalid, or
109 * the specified @c capacity is negative.
110 * @remarks If the number of elements added to the stack reaches the current capacity,
111 * the capacity is automatically increased by memory reallocation. @n
112 * Therefore, if the size of the stack can be estimated,
113 * specifying the initial capacity eliminates the need to perform a number of
114 * resizing operations while adding elements to the stack.
117 result Construct(int capacity = DEFAULT_CAPACITY);
120 * Initializes this instance of %Stack by copying the elements of the specified collection. @n
121 * The capacity of the stack is the same as the number of elements copied.
125 * @return An error code
126 * @param[in] collection The collection to copy elements from
127 * @exception E_SUCCESS The method is successful.
128 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
129 * the @c collection is modified during the operation of this method.
130 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
133 result Construct(const ICollection& collection);
136 * Gets an enumerator of this stack.
140 * @return An enumerator (an instance of the IEnumerator derived class) of this stack, @n
141 * else @c null if an exception occurs
142 * @remarks The specific error code can be accessed using the GetLastResult() method.
145 virtual IEnumerator* GetEnumeratorN(void) const;
148 * Gets the element at the top of this stack without removing it.
152 * @return The element at the top of this stack, @n
153 * else @c null if this stack is empty
154 * @exception E_SUCCESS The method is successful.
155 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
156 * this stack is empty.
157 * @remarks The specific error code can be accessed using the GetLastResult() method.
159 virtual const Object* Peek(void) const;
162 * Pops the element from the top of this stack.
166 * @return The element at the top of this stack, @n
167 * else @c null if this stack is empty
168 * @exception E_SUCCESS The method is successful.
169 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
170 * this stack is empty.
171 * @remarks The specific error code can be accessed using the GetLastResult() method.
174 virtual Object* Pop(void);
178 * Pushes an object to the top of this stack.
180 * @brief <i> [Deprecated] </i>
181 * @deprecated This method is deprecated because it has a problem of const reference argument.
182 * Instead of using this method, use Push(Object* pObj).
185 * @return An error code
186 * @param[in] obj The object to add to this stack
187 * @exception E_SUCCESS The method is successful.
188 * @exception E_OUT_OF_MEMORY The memory is insufficient.
189 * @remarks This method performs a shallow copy. It inserts just the pointer; not the element itself.
193 result Push(const Object& obj)
195 return Push(const_cast< Object* >(&obj));
199 * Pushes an object to the top of this stack.
203 * @return An error code
204 * @param[in] pObj The pointer to object to add to this stack
205 * @exception E_SUCCESS The method is successful.
206 * @exception E_INVALID_ARG The specified input parameter is invalid.
207 * @remarks This method performs a shallow copy. It inserts just the pointer; not the element itself.
210 virtual result Push(Object* pObj);
213 * Removes all objects and their pointers in collection, when the @c deallocate parameter is set to @c true. @n
214 * This method can be called before deleting the objects in a collection.
218 * @param[in] deallocate Set to @c true to deallocate all objects, @n
221 virtual void RemoveAll(bool deallocate = false);
224 * Gets the number of objects currently stored in this stack.
228 * @return The number of objects currently stored in this stack
230 virtual int GetCount(void) const;
233 * Checks whether this stack contains the specified object.
237 * @return @c true if this stack contains the specified object, @n
239 * @param[in] obj The object to locate
241 virtual bool Contains(const Object& obj) const;
245 * Checks whether this stack contains all of the elements in the specified collection.
247 * @brief <i> [Deprecated] </i>
248 * @deprecated This method is deprecated because it transfers a result of comparison in out-parameter form.
249 * The return type will be changed into boolean type and this method will return the result.
250 * Instead of using this method, use bool Contains(const ICollection& collection).
253 * @return An error code
254 * @param[in] collection The collection to locate
255 * @param[out] out Set to @c true if this stack contains all the elements in the specified collection, @n
257 * @exception E_SUCCESS The method is successful.
258 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
259 * the @c collection is modified during the operation of this method.
262 result ContainsAll(const ICollection& collection, bool& out) const
264 out = ContainsAll(collection);
265 result r = GetLastResult();
270 * Checks whether this stack contains all of the elements in the specified collection.
274 * @return @c true if this stack contains all the elements in the specified collection, @n
276 * @param[in] collection The collection to locate
277 * @exception E_SUCCESS The method is successful.
278 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
279 * the @c collection is modified during the operation of this method.
280 * @remarks The specific error code can be accessed using the GetLastResult() method.
282 virtual bool ContainsAll(const ICollection& collection) const;
285 * Checks whether the specified instance equals the current instance.
289 * @return @c true if the specified instance equals the current instance, @n
291 * @param[in] obj The object to compare with the current instance
292 * @remarks This method returns @c true only if the specified object is also an instance of %Stack class,
293 * both stacks have the same size, and all corresponding pairs of elements in the two stacks are equal.
294 * In other words, two stacks are equal if they contain the same elements in the same order.
296 virtual bool Equals(const Object& obj) const;
299 * Gets the hash value of the current instance.
303 * @return The hash value of the current instance
304 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
305 * the used hash function must generate a random distribution for all inputs.
307 virtual int GetHashCode(void) const;
311 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
313 * @param[in] stack The other instance of Stack
315 Stack(const Stack& stack);
318 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
322 * @param[in] stack An instance of %Stack
324 Stack& operator =(const Stack& stack);
328 Object** __pObjArray;
330 static const int DEFAULT_CAPACITY = 10;
332 friend class _StackEnumerator;
333 class _StackImpl* __pStackImpl;
334 friend class _StackImpl;
338 }}} // Tizen::Base::Collection
340 #endif // _FBASE_COL_STACK_H_