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 FBaseColStackT.h
20 * @brief This is the header file for the %StackT class.
22 * This header file contains the declarations of the %StackT class.
25 #ifndef _FBASE_COL_STACK_T_H_
26 #define _FBASE_COL_STACK_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 __StackEnumeratorT;
40 * @brief This class represents a template-based stack (a last-in-first-out collection of objects).
44 * The %StackT class represents a template-based stack (a last-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 %StackT class.
53 * using namespace Tizen::Base;
54 * using namespace Tizen::Base::Collection;
57 * MyClass::StackTSample(void)
59 * StackT<String> stack;
62 * String str1(L"First");
63 * String str2(L"Second");
64 * String str3(L"Third");
70 * // Reads the element at the top
72 * stack.Peek(temp); // temp: "Third", stack.GetCount(): 3
74 * // Reads and removes the element at the top
75 * stack.Pop(temp); // temp: "Third", stack.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.
100 * This destructor overrides Tizen::Base::Object::~Object().
104 virtual ~StackT(void)
111 * Initializes this instance of %StackT with the specified capacity.
115 * @return An error code
116 * @param[in] capacity The number of elements @n
117 * The default capacity is @c 10.
118 * @exception E_SUCCESS The method is successful.
119 * @exception E_INVALID_ARG A specified input parameter is invalid, or
120 * the specified @c capacity is negative.
122 result Construct(int capacity = DEFAULT_CAPACITY)
124 TryReturn(capacity >= 0, E_INVALID_ARG, "[%s] The capacity(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_INVALID_ARG), capacity);
128 __pObjArray = new Type[capacity];
129 TryReturn(__pObjArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
131 __capacity = capacity;
137 * Initializes this instance of %StackT that contains the elements of the specified collection. @n
138 * The capacity of the stack is the same as the number of elements copied.
142 * @return An error code
143 * @param[in] collection The collection to copy elements from
144 * @exception E_SUCCESS The method is successful.
145 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
146 * the specified @c collection is modified during the operation of this method.
149 result Construct(const ICollectionT< Type >& collection)
151 result r = E_SUCCESS;
153 IEnumeratorT< Type >* pEnum = null;
154 if (collection.GetCount() > 0)
156 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
157 pEnum = pCol->GetEnumeratorN();
158 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
164 r = pEnum->MoveNext();
165 // enumerator is reached to the end of collection
166 if (E_OUT_OF_RANGE == r)
171 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
173 r = pEnum->GetCurrent(temp);
174 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
177 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
198 * Gets an enumerator of this stack.
202 * @return An enumerator (an instance of the IEnumeratorT derived class) of this stack, @n
203 * else @c null if an exception occurs
204 * @exception E_SUCCESS The method is successful.
205 * @exception E_OUT_OF_MEMORY The memory is insufficient.
206 * @remarks The specific error code can be accessed using the GetLastResult() method.
207 * @see Tizen::Base::Collection::IEnumeratorT
209 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
213 result r = E_SUCCESS;
215 __StackEnumeratorT< Type >* pEnum = new __StackEnumeratorT< Type >(*this, __modCount);
216 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
226 * Reads the element at the beginning of this stack without removing it.
230 * @return An error code
231 * @param[out] obj The element at the beginning of this stack
232 * @exception E_SUCCESS The method is successful.
233 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
234 * this stack is empty.
236 virtual result Peek(Type& obj) const
243 obj = __pObjArray[__index];
249 * Pops the element from the beginning of this stack.
253 * @return An error code
254 * @param[out] obj The element at the beginning of this stack
255 * @exception E_SUCCESS The method is successful.
256 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
257 * this stack is empty.
260 virtual result Pop(Type& obj)
269 obj = __pObjArray[__index];
277 * Pushes an object at the top of this stack.
281 * @return An error code
282 * @param[in] obj The object to add to this stack
283 * @exception E_SUCCESS The method is successful.
284 * @exception E_OUT_OF_MEMORY The memory is insufficient.
287 virtual result Push(const Type& obj)
290 if (null == __pObjArray)
292 __pObjArray = new Type[DEFAULT_CAPACITY];
293 TryReturn(__pObjArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
295 __capacity = DEFAULT_CAPACITY;
297 else if ((__index + 1) >= __capacity)
299 Type* pArrayTemp = new Type[__capacity + DEFAULT_CAPACITY];
300 TryReturn(pArrayTemp != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
302 for (int i = 0; i <= __index; i++)
304 pArrayTemp[i] = __pObjArray[i];
307 delete[] __pObjArray;
309 __pObjArray = pArrayTemp;
310 __capacity += DEFAULT_CAPACITY;
316 __pObjArray[++__index] = obj;
322 * Removes all elements in this stack.
326 virtual void RemoveAll(void)
328 if (__pObjArray != null)
330 delete[] __pObjArray;
341 * Gets the number of objects currently stored in this stack.
345 * @return The number of objects currently stored in this stack
347 virtual int GetCount(void) const
353 * Checks whether this stack contains the specified object.
357 * @return @c true if this stack contains the specified object, @n
359 * @param[in] obj The object to locate
361 virtual bool Contains(const Type& obj) const
367 for (int i = 0; i <= __index; i++)
369 if (__pObjArray[i] == obj)
381 * Checks whether this stack contains all of the elements in the specified collection.
385 * @return An error code
386 * @param[in] collection The collection to locate
387 * @param[out] out Set to @c true if this stack contains all of the elements in the specified collection, @n
389 * @exception E_SUCCESS The method is successful.
390 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
391 * the specified @c collection is modified during the operation of this method.
393 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
395 result r = E_SUCCESS;
397 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
398 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
399 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
405 r = pEnum->MoveNext();
406 // enumerator has reached the end of collection
407 if (E_OUT_OF_RANGE == r)
413 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
415 r = pEnum->GetCurrent(temp);
416 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
418 if (false == Contains(temp))
440 * Checks whether the specified instance equals the current instance.
444 * @return @c true if the specified instance equals the current instance, @n
446 * @param[in] obj The object to compare with the current instance
447 * @remarks This method returns @c true only if the specified object is also an instance of the Stack class,
448 * both stacks have the same size, and all the corresponding pairs of elements in the two stacks are equal.
449 * In other words, two stacks are equal if they contain the same elements in the same order.
451 virtual bool Equals(const Object& obj) const
455 const StackT< Type >* other = dynamic_cast< const StackT< Type >* >(&obj);
456 if (null == other) // obj is not a StackT<Type> instance
460 else if (other == this)
464 else if (__index != other->__index)
470 for (int i = 0; i <= __index; i++)
472 if (!(__pObjArray[i] == other->__pObjArray[i]))
485 * Gets the hash value of the current instance.
489 * @return The hash value of the current instance
490 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
491 * the used hash function must generate a random distribution for all inputs.
493 virtual int GetHashCode(void) const
496 for (int i = 0; i <= __index; i++)
498 if (&__pObjArray[i] != null)
500 hash += reinterpret_cast< int >(&__pObjArray[i]);
509 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
511 * @param[in] stack The other instance of StackT
513 StackT(const StackT< Type >& stack);
516 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
518 * @param[in] stack An instance of %StackT
520 StackT< Type >& operator =(const StackT< Type >& stack);
526 static const int DEFAULT_CAPACITY = 10;
528 friend class __StackEnumeratorT< Type >;
533 // @class __StackEnumeratorT
534 // @brief This class is an implementation of IEnumeratorT for %StackT.
537 template< class Type >
538 class __StackEnumeratorT
539 : public IEnumeratorT< Type >
544 * This is the constructor for this class.
548 * @param[in] stack A stack to enumerate
549 * @param[in] modCount The modification count to detect the change in the stack
551 __StackEnumeratorT(const StackT< Type >& stack, int modCount)
553 , __modCount(modCount)
559 * This is the destructor for this class.
563 virtual ~__StackEnumeratorT(void)
568 * Gets the current object in the stack.
572 * @return An error code
573 * @param[out] obj The current object
574 * @exception E_INVALID_OPERATION Either of the following conditions has occurred: @n
575 * - The current state of the instance prohibits the execution of the specified operation. @n
576 * - This enumerator is currently positioned before the first element or
577 * past the last element. @n
578 * - The stack is modified after this enumerator is created.
579 * @exception E_SUCCESS The method is successful.
581 result GetCurrent(Type& obj) const
583 TryReturn(__modCount == __stack.__modCount, E_INVALID_OPERATION,
584 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
585 TryReturn(((__position >= 0) && (__position <= __stack.__index)), E_INVALID_OPERATION,
586 "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
588 obj = __stack.__pObjArray[__position];
594 * Moves this enumerator to the next element of the stack. @n
595 * When this enumerator is first created or after a call to Reset(),
596 * the first call to MoveNext() positions this enumerator to the first element in the stack.
600 * @return An error code
601 * @exception E_SUCCESS The method is successful.
602 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
603 * the stack is modified after this enumerator is created.
604 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the stack.
607 result MoveNext(void)
609 TryReturn((__modCount == __stack.__modCount), E_INVALID_OPERATION,
610 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
612 result r = E_SUCCESS;
613 if (__position >= __stack.__index)
615 // Do not log the E_OUT_OF_RANGE, because it is normal or trivial in most cases.
627 * Positions this enumerator before the first element in the stack.
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 stack is modified after this enumerator is created.
638 TryReturn((__modCount == __stack.__modCount), E_INVALID_OPERATION,
639 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
647 const StackT< Type >& __stack;
651 }; // __StackEnumeratorT
653 }}} // Tizen::Base::Collection
655 #endif //_FBASE_COL_STACK_T_H_