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 FBaseColStackT.h
19 * @brief This is the header file for the %StackT class.
21 * This header file contains the declarations of the %StackT class.
24 #ifndef _FBASE_COL_STACK_T_H_
25 #define _FBASE_COL_STACK_T_H_
27 #include <FBaseObject.h>
28 #include <FBaseResult.h>
29 #include <FBaseColICollectionT.h>
31 namespace Tizen { namespace Base { namespace Collection
34 template< class Type > class __StackEnumeratorT;
38 * @brief This class represents a template-based stack (a last-in-first-out collection of objects).
42 * The %StackT class represents a template-based stack (a last-in-first-out collection of objects).
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 %StackT class.
51 * using namespace Tizen::Base;
52 * using namespace Tizen::Base::Collection;
55 * MyClass::StackTSample(void)
57 * StackT< String > stack;
60 * String str1(L"First");
61 * String str2(L"Second");
62 * String str3(L"Third");
68 * // Reads the element at the top
70 * stack.Peek(temp); // temp: "Third", stack.GetCount(): 3
72 * // Reads and removes the element at the top
73 * stack.Pop(temp); // temp: "Third", stack.GetCount(): 2
77 template< class Type >
79 : public virtual ICollectionT< Type >
84 * The object is not fully constructed after this constructor is called. For full construction, @n
85 * the Construct() method must be called right after calling this constructor.
98 * This destructor overrides Tizen::Base::Object::~Object().
102 virtual ~StackT(void)
109 * Initializes this instance of %StackT with the specified @c capacity.
113 * @return An error code
114 * @param[in] capacity The number of elements @n
115 * The default capacity is @c 10.
116 * @exception E_SUCCESS The method is successful.
117 * @exception E_INVALID_ARG The specified input parameter is invalid, or
118 * the specified @c capacity is negative.
120 result Construct(int capacity = DEFAULT_CAPACITY)
122 TryReturn(capacity >= 0, E_INVALID_ARG, "[%s] The capacity(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_INVALID_ARG), capacity);
126 __pObjArray = new Type[capacity];
127 TryReturn(__pObjArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
129 __capacity = capacity;
135 * Initializes this instance of %StackT that contains the elements of the specified @c collection. @n
136 * The capacity of the stack is the same as the number of elements copied.
140 * @return An error code
141 * @param[in] collection The collection to copy elements from
142 * @exception E_SUCCESS The method is successful.
143 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
144 * the specified @c collection is modified during the operation of this method.
147 result Construct(const ICollectionT< Type >& collection)
149 result r = E_SUCCESS;
151 IEnumeratorT< Type >* pEnum = null;
152 if (collection.GetCount() > 0)
154 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
155 pEnum = pCol->GetEnumeratorN();
156 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
162 r = pEnum->MoveNext();
163 // enumerator is reached to the end of collection
164 if (E_OUT_OF_RANGE == r)
169 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
171 r = pEnum->GetCurrent(temp);
172 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
175 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
196 * Gets an enumerator of this stack.
200 * @return An enumerator (an instance of the IEnumeratorT derived class) of this stack, @n
201 * else @c null if an exception occurs
202 * @exception E_SUCCESS The method is successful.
203 * @exception E_OUT_OF_MEMORY The memory is insufficient.
204 * @remarks The specific error code can be accessed using the GetLastResult() method.
205 * @see Tizen::Base::Collection::IEnumeratorT
207 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
211 result r = E_SUCCESS;
213 __StackEnumeratorT< Type >* pEnum = new __StackEnumeratorT< Type >(*this, __modCount);
214 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
224 * Reads the element at the beginning of this stack without removing it.
228 * @return An error code
229 * @param[out] obj The element at the beginning of this stack
230 * @exception E_SUCCESS The method is successful.
231 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
232 * this stack is empty.
234 virtual result Peek(Type& obj) const
241 obj = __pObjArray[__index];
247 * Pops the element from the beginning of this stack.
251 * @return An error code
252 * @param[out] obj The element at the beginning of this stack
253 * @exception E_SUCCESS The method is successful.
254 * @exception E_UNDERFLOW The operation (arithmetic/casting/conversion) has caused an underflow, or
255 * this stack is empty.
258 virtual result Pop(Type& obj)
267 obj = __pObjArray[__index];
275 * Pushes an object at the top of this stack.
279 * @return An error code
280 * @param[in] obj The object to add to this stack
281 * @exception E_SUCCESS The method is successful.
282 * @exception E_OUT_OF_MEMORY The memory is insufficient.
285 virtual result Push(const Type& obj)
288 if (null == __pObjArray)
290 __pObjArray = new Type[DEFAULT_CAPACITY];
291 TryReturn(__pObjArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
293 __capacity = DEFAULT_CAPACITY;
295 else if ((__index + 1) >= __capacity)
297 Type* pArrayTemp = new Type[__capacity + DEFAULT_CAPACITY];
298 TryReturn(pArrayTemp != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
300 for (int i = 0; i <= __index; i++)
302 pArrayTemp[i] = __pObjArray[i];
305 delete[] __pObjArray;
307 __pObjArray = pArrayTemp;
308 __capacity += DEFAULT_CAPACITY;
314 __pObjArray[++__index] = obj;
320 * Removes all elements in this stack.
324 virtual void RemoveAll(void)
326 if (__pObjArray != null)
328 delete[] __pObjArray;
339 * Gets the number of objects currently stored in this stack.
343 * @return The number of objects currently stored in this stack
345 virtual int GetCount(void) const
351 * Checks whether this stack contains the specified object.
355 * @return @c true if this stack contains the specified object, @n
357 * @param[in] obj The object to locate
359 virtual bool Contains(const Type& obj) const
365 for (int i = 0; i <= __index; i++)
367 if (__pObjArray[i] == obj)
379 * Checks whether this stack contains all of the elements in the specified @c collection.
383 * @return An error code
384 * @param[in] collection The collection to locate
385 * @param[out] out Set to @c true if this stack contains all of the elements in the specified @c collection, @n
387 * @exception E_SUCCESS The method is successful.
388 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
389 * the specified @c collection is modified during the operation of this method.
391 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
393 result r = E_SUCCESS;
395 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
396 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
397 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
403 r = pEnum->MoveNext();
404 // enumerator has reached the end of collection
405 if (E_OUT_OF_RANGE == r)
411 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
413 r = pEnum->GetCurrent(temp);
414 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
416 if (false == Contains(temp))
438 * Checks whether the specified instance equals the current instance.
442 * @return @c true if the specified instance equals the current instance, @n
444 * @param[in] obj The object to compare with the current instance
445 * @remarks This method returns @c true only if the specified object is also an instance of the Stack class,
446 * both stacks have the same size, and all the corresponding pairs of elements in the two stacks are equal.
447 * In other words, two stacks are equal if they contain the same elements in the same order.
449 virtual bool Equals(const Object& obj) const
453 const StackT< Type >* other = dynamic_cast< const StackT< Type >* >(&obj);
454 if (null == other) // obj is not a StackT<Type> instance
458 else if (other == this)
462 else if (__index != other->__index)
468 for (int i = 0; i <= __index; i++)
470 if (!(__pObjArray[i] == other->__pObjArray[i]))
483 * Gets the hash value of the current instance.
487 * @return The hash value of the current instance
488 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
489 * the used hash function must generate a random distribution for all inputs.
491 virtual int GetHashCode(void) const
494 for (int i = 0; i <= __index; i++)
496 if (&__pObjArray[i] != null)
498 hash += reinterpret_cast< int >(&__pObjArray[i]);
507 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
509 * @param[in] stack The other instance of StackT
511 StackT(const StackT< Type >& stack);
514 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
516 * @param[in] stack An instance of %StackT
518 StackT< Type >& operator =(const StackT< Type >& stack);
524 static const int DEFAULT_CAPACITY = 10;
526 friend class __StackEnumeratorT< Type >;
531 // @class __StackEnumeratorT
532 // @brief This class is an implementation of IEnumeratorT for %StackT.
535 template< class Type >
536 class __StackEnumeratorT
537 : public IEnumeratorT< Type >
542 * This is the constructor for this class.
546 * @param[in] stack A stack to enumerate
547 * @param[in] modCount The modification count to detect the change in the stack
549 __StackEnumeratorT(const StackT< Type >& stack, int modCount)
551 , __modCount(modCount)
557 * This is the destructor for this class.
561 virtual ~__StackEnumeratorT(void)
566 * Gets the current object in the stack.
570 * @return An error code
571 * @param[out] obj The current object
572 * @exception E_INVALID_OPERATION Either of the following conditions has occurred: @n
573 * - The current state of the instance prohibits the execution of the specified operation. @n
574 * - This enumerator is currently positioned before the first element or
575 * past the last element. @n
576 * - The stack is modified after this enumerator is created.
577 * @exception E_SUCCESS The method is successful.
579 result GetCurrent(Type& obj) const
581 TryReturn(__modCount == __stack.__modCount, E_INVALID_OPERATION,
582 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
583 TryReturn(((__position >= 0) && (__position <= __stack.__index)), E_INVALID_OPERATION,
584 "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
586 obj = __stack.__pObjArray[__position];
592 * Moves this enumerator to the next element of the stack. @n
593 * When this enumerator is first created or after a call to Reset(),
594 * the first call to %MoveNext() positions this enumerator to the first element in the stack.
598 * @return An error code
599 * @exception E_SUCCESS The method is successful.
600 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
601 * the stack is modified after this enumerator is created.
602 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the stack.
605 result MoveNext(void)
607 TryReturn((__modCount == __stack.__modCount), E_INVALID_OPERATION,
608 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
610 result r = E_SUCCESS;
611 if (__position >= __stack.__index)
613 // Do not log the E_OUT_OF_RANGE, because it is normal or trivial in most cases.
625 * Positions this enumerator before the first element in the stack.
629 * @return An error code
630 * @exception E_SUCCESS The method is successful.
631 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
632 * the stack is modified after this enumerator is created.
636 TryReturn((__modCount == __stack.__modCount), E_INVALID_OPERATION,
637 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
645 const StackT< Type >& __stack;
649 }; // __StackEnumeratorT
651 }}} // Tizen::Base::Collection
653 #endif //_FBASE_COL_STACK_T_H_