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 requirements of C++ standard library InputIterator concept due to limitations of %Tizen collection.
44 * So, this class can be used with C++ standard library algorithms which requires only 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 class.
60 * @param[in] list A reference to the IList instance to convert
61 * @param[in] isPostEnd A boolean value to check the end of a 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 an 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 for 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 a structure dereference operator for 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 The current state of the instance prohibits the execution of the specified operation, or
166 * the collection is modified after the enumerator is created.
167 * @remarks The specific error code can be accessed using GetLastResult() method.
169 IteratorT< T >& operator++(void)
171 const int PRE_BEGIN_IDX = -1;
172 TryCatchResult(__index >= PRE_BEGIN_IDX, , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
174 if (__index != PRE_BEGIN_IDX)
176 result r = __pEnum->MoveNext();
177 TryCatchResult(r == E_SUCCESS, __isPostEnd = true; __currentObj = null, r, "[%s] It already reached the end.", GetErrorMessage(r));
180 __currentObj = static_cast< T >(__pEnum->GetCurrent());
188 * Moves to the next element of the collection and returns the previous state.
192 * @return An %IteratorT instance
193 * @exception E_SUCCESS The method is successful.
194 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
195 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
196 * the collection is modified after the enumerator is created.
197 * @remarks The specific error code can be accessed using GetLastResult() method.
199 IteratorT< T > operator++(int)
201 IteratorT< T > tempIter = *this;
207 * Moves to the previous element of the collection.
211 * @return A reference to the %IteratorT type instance
212 * @exception E_SUCCESS The method is successful.
213 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
214 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
215 * the collection is modified after the enumerator is created.
216 * @remarks The specific error code can be accessed using GetLastResult() method.
218 IteratorT< T >& operator--(void)
220 TryCatchResult(__index <= __pList->GetCount(), , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
224 result r = __pEnum->MovePrevious();
225 TryCatchResult(r == E_SUCCESS, __currentObj = null, r, "[%s] It already reached the front.", GetErrorMessage(r));
232 __currentObj = static_cast< T >(__pEnum->GetCurrent());
240 * Moves to the previous element of the collection and returns the previous state.
244 * @return An %IteratorT instance
245 * @exception E_SUCCESS The method is successful.
246 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
247 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
248 * the collection is modified after the enumerator is created.
249 * @remarks The specific error code can be accessed using GetLastResult() method.
251 IteratorT< T > operator--(int)
253 IteratorT< T > tempIter = *this;
259 * Checks the two %IteratorT instances for equality.
263 * @return @c true if every member of the specified %IteratorT instance equals the calling instance's members, @n
265 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
267 bool operator==(const IteratorT< T >& rhs) const
269 if (__pList != rhs.__pList)
274 if (__index != rhs.__index)
279 if (__isPostEnd != rhs.__isPostEnd)
283 else if (__isPostEnd && rhs.__isPostEnd)
285 // In this case, __currentObj state is invalid
289 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
290 return __currentObj == rhs.__currentObj;
294 * Checks the two %IteratorT instances for inequality.
298 * @return @c true if any member of the specified %IteratorT instance is not equal to the calling instance's members, @n
300 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
302 bool operator!=(const IteratorT< T >& rhs) const
304 return !operator==(rhs);
308 * Swaps values of two %IteratorT instances.
312 * @param[in] rhs A reference to a %IteratorT instance to swap
314 void swap(IteratorT< T >& rhs)
316 std::swap(__pList, rhs.__pList);
317 std::swap(__isPostEnd, rhs.__isPostEnd);
318 std::swap(__index, rhs.__index);
319 std::swap(__pEnum, rhs.__pEnum);
320 std::swap(__currentObj, rhs.__currentObj);
324 const IList* __pList;
327 std::unique_ptr< IBidirectionalEnumerator > __pEnum;
331 }}} // Tizen::Base::Collection
333 #endif //_FBASE_COL_ITERATOR_T_H_