2 // Copyright (c) 2013 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 FBaseColIteratorT.h
19 * @brief This is the header file for the %IteratorT class.
21 * This header file contains the declarations of the %IteratorT class.
24 #ifndef _FBASE_COL_ITERATOR_T_H_
25 #define _FBASE_COL_ITERATOR_T_H_
27 #include <algorithm> // std::swap (Before C++11)
29 #include <unique_ptr.h>
31 #include <FBaseColIList.h>
32 #include <FBaseColIBidirectionalEnumerator.h>
34 namespace Tizen { namespace Base { namespace Collection
38 * @brief This class provides an iterator that is used to convert %IList to STL containers. @n
39 * %StlConverter provides static methods to get this iterator from IList.
43 * @remarks The %IteratorT class satisfies only the requirements of the C++ standard library InputIterator concept due to limitations of the %Tizen collection. @n
44 * This class can be used with the C++ standard library algorithms which require only the InputIterator concept for their arguments.
46 * The %IteratorT class provides an iterator that is used to convert IList to STL containers.
47 * StlConverter provides static methods to get this iterator from IList.
50 template< typename T >
52 : public std::iterator< std::input_iterator_tag, T >
56 * Initializes an instance of %IteratorT.
60 * @param[in] list A reference to the IList instance to convert
61 * @param[in] isPostEnd The boolean value that checks the end of the list
63 explicit IteratorT(const IList& list, bool isPostEnd = false)
65 , __isPostEnd(isPostEnd)
67 , __pEnum(__pList->GetBidirectionalEnumeratorN())
70 if (__pList->GetCount() != 0)
75 __currentObj = static_cast< T >(__pEnum->GetCurrent());
79 __index = __pList->GetCount();
80 __pEnum->MovePrevious();
85 // Control reaches here intentionally because begin() should be equal to end()
91 * This is the copy constructor of the %IteratorT class.
95 * @param[in] rhs A reference to the %IteratorT instance
97 IteratorT(const IteratorT< T >& rhs)
98 : __pList(rhs.__pList)
99 , __isPostEnd(rhs.__isPostEnd)
100 , __index(rhs.__index)
101 , __pEnum(__pList->GetBidirectionalEnumeratorN())
102 , __currentObj(rhs.__currentObj)
106 for (int i = 0; i <= __index; ++i)
113 __pEnum->MovePrevious();
118 * This is the assignment operator of the %IteratorT class.
122 * @return A reference to the %IteratorT instance
123 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
125 IteratorT< T >& operator =(const IteratorT< T >& rhs)
127 IteratorT< T > tmp(rhs);
133 * This is the indirection operator of the %IteratorT class.
137 * @return A T type reference
139 T& operator *(void) const
141 AppAssertf(!__isPostEnd && __index >= 0, "It is out of range.");
142 return const_cast< T& >(__currentObj);
146 * This is the structure dereference operator of the %IteratorT class.
150 * @return A T type pointer that is equivalent to the pointer address
152 T* operator ->(void) const
154 return &(operator *());
158 * Moves to the next element in the collection.
162 * @return A reference to the %IteratorT type instance
163 * @exception E_SUCCESS The method is successful.
164 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
165 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
166 * - The current state of the instance prohibits the execution of the specified operation.
167 * - The collection is modified after the enumerator is created.
168 * @remarks The specific error code can be accessed using GetLastResult() method.
170 IteratorT< T >& operator ++(void)
172 const int PRE_BEGIN_IDX = -1;
173 TryCatchResult(__index >= PRE_BEGIN_IDX, , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
175 if (__index != PRE_BEGIN_IDX)
177 result r = __pEnum->MoveNext();
178 TryCatchResult(r == E_SUCCESS, __isPostEnd = true;
179 __currentObj = null, r, "[%s] It already reached the end.", GetErrorMessage(r));
182 __currentObj = static_cast< T >(__pEnum->GetCurrent());
190 * Moves to the next element of the collection and returns the previous state.
194 * @return An %IteratorT instance
195 * @exception E_SUCCESS The method is successful.
196 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
197 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
198 * - The current state of the instance prohibits the execution of the specified operation.
199 * - The collection is modified after the enumerator is created.
200 * @remarks The specific error code can be accessed using GetLastResult() method.
202 IteratorT< T > operator ++(int)
204 IteratorT< T > tempIter = *this;
210 * Moves to the previous element of the collection.
214 * @return A reference to the %IteratorT type instance
215 * @exception E_SUCCESS The method is successful.
216 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
217 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
218 * - The current state of the instance prohibits the execution of the specified operation.
219 * - The collection is modified after the enumerator is created.
220 * @remarks The specific error code can be accessed using GetLastResult() method.
222 IteratorT< T >& operator --(void)
224 TryCatchResult(__index <= __pList->GetCount(), , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
228 result r = __pEnum->MovePrevious();
229 TryCatchResult(r == E_SUCCESS, __currentObj = null, r, "[%s] It already reached the front.", GetErrorMessage(r));
236 __currentObj = static_cast< T >(__pEnum->GetCurrent());
244 * Moves to the previous element of the collection and returns the previous state.
248 * @return An %IteratorT instance
249 * @exception E_SUCCESS The method is successful.
250 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
251 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
252 * - The current state of the instance prohibits the execution of the specified operation.
253 * - The collection is modified after the enumerator is created.
254 * @remarks The specific error code can be accessed using GetLastResult() method.
256 IteratorT< T > operator --(int)
258 IteratorT< T > tempIter = *this;
264 * Checks the two %IteratorT instances for equality.
268 * @return @c true if every member of the specified %IteratorT instance equals the calling instance's members, @n
270 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
272 bool operator ==(const IteratorT< T >& rhs) const
274 if (__pList != rhs.__pList)
279 if (__index != rhs.__index)
284 if (__isPostEnd != rhs.__isPostEnd)
288 else if (__isPostEnd && rhs.__isPostEnd)
290 // In this case, __currentObj state is invalid
294 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
295 return __currentObj == rhs.__currentObj;
299 * Checks the two %IteratorT instances for inequality.
303 * @return @c true if any member of the specified %IteratorT instance is not equal to the calling instance's members, @n
305 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
307 bool operator !=(const IteratorT< T >& rhs) const
309 return !operator ==(rhs);
313 * Swaps values of two %IteratorT instances.
317 * @param[in] rhs A reference to a %IteratorT instance to swap
319 void swap(IteratorT< T >& rhs)
321 std::swap(__pList, rhs.__pList);
322 std::swap(__isPostEnd, rhs.__isPostEnd);
323 std::swap(__index, rhs.__index);
324 std::swap(__pEnum, rhs.__pEnum);
325 std::swap(__currentObj, rhs.__currentObj);
329 const IList* __pList;
332 std::unique_ptr< IBidirectionalEnumerator > __pEnum;
336 }}} // Tizen::Base::Collection
338 #endif //_FBASE_COL_ITERATOR_T_H_