2 // Open Service Platform
3 // Copyright (c) 2013 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 FBaseColIteratorT.h
20 * @brief This is the header file for the %IteratorT class.
22 * This header file contains the declarations of the %IteratorT class.
25 #ifndef _FBASE_COL_ITERATOR_T_H_
26 #define _FBASE_COL_ITERATOR_T_H_
28 #include <algorithm> // std::swap (Before C++11)
30 #include <unique_ptr.h>
32 #include <FBaseColIList.h>
33 #include <FBaseColIBidirectionalEnumerator.h>
35 namespace Tizen { namespace Base { namespace Collection
39 * @brief This class provides an iterator that is used to convert IList to STL containers. @n
40 * StlConverter provides static methods to get this iterator from %IList.
44 * @remarks The %IteratorT class satisfies only requirements of C++ standard library InputIterator concept due to limitations of %Tizen collection.
45 * So, this class can be used with C++ standard library algorithms which requires only InputIterator concept for their arguments.
47 * The %IteratorT class provides an iterator that is used to convert IList to STL containers.
48 * StlConverter provides static methods to get this iterator from IList.
51 template < typename T >
53 : public std::iterator< std::input_iterator_tag, T >
57 * Initializes an instance of %IteratorT class.
61 * @param[in] list A reference to the IList instance to convert
62 * @param[in] isPostEnd A boolean value to check the end of a list
64 explicit IteratorT(const IList& list, bool isPostEnd = false)
66 , __isPostEnd(isPostEnd)
68 , __pEnum(__pList->GetBidirectionalEnumeratorN())
71 if (__pList->GetCount() != 0)
76 __currentObj = static_cast< T >(__pEnum->GetCurrent());
80 __index = __pList->GetCount();
81 __pEnum->MovePrevious();
86 // Control reaches here intentionally because begin() should be equal to end()
92 * This is the copy constructor of the %IteratorT class.
96 * @param[in] rhs A reference to the %IteratorT instance
98 IteratorT(const IteratorT< T >& rhs)
99 : __pList(rhs.__pList)
100 , __isPostEnd(rhs.__isPostEnd)
101 , __index(rhs.__index)
102 , __pEnum(__pList->GetBidirectionalEnumeratorN())
103 , __currentObj(rhs.__currentObj)
107 for (int i = 0; i <= __index; ++i)
114 __pEnum->MovePrevious();
119 * This is an assignment operator of the %IteratorT class.
123 * @return A reference to the %IteratorT instance
124 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
126 IteratorT< T >& operator=(const IteratorT< T >& rhs)
128 IteratorT< T > tmp(rhs);
134 * This is the indirection operator for the %IteratorT class.
138 * @return A T type reference
140 T& operator*(void) const
142 AppAssertf(!__isPostEnd && __index >= 0, "It is out of range.");
143 return const_cast< T& >(__currentObj);
147 * This is a structure dereference operator for the %IteratorT class.
151 * @return A T type pointer that is equivalent to the pointer address
153 T* operator->(void) const
155 return &(operator*());
159 * Moves to the next element in the collection.
163 * @return A reference to the %IteratorT type instance
164 * @exception E_SUCCESS The method is successful.
165 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
166 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
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; __currentObj = null, r, "[%s] It already reached the end.", GetErrorMessage(r));
181 __currentObj = static_cast< T >(__pEnum->GetCurrent());
189 * Moves to the next element of the collection and returns the previous state.
193 * @return An %IteratorT instance
194 * @exception E_SUCCESS The method is successful.
195 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
196 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
197 * the collection is modified after the enumerator is created.
198 * @remarks The specific error code can be accessed using GetLastResult() method.
200 IteratorT< T > operator++(int)
202 IteratorT< T > tempIter = *this;
208 * Moves to the previous element of the collection.
212 * @return A reference to the %IteratorT type instance
213 * @exception E_SUCCESS The method is successful.
214 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
215 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
216 * the collection is modified after the enumerator is created.
217 * @remarks The specific error code can be accessed using GetLastResult() method.
219 IteratorT< T >& operator--(void)
221 TryCatchResult(__index <= __pList->GetCount(), , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
225 result r = __pEnum->MovePrevious();
226 TryCatchResult(r == E_SUCCESS, __currentObj = null, r, "[%s] It already reached the front.", GetErrorMessage(r));
233 __currentObj = static_cast< T >(__pEnum->GetCurrent());
241 * Moves to the previous element of the collection and returns the previous state.
245 * @return An %IteratorT instance
246 * @exception E_SUCCESS The method is successful.
247 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
248 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
249 * the collection is modified after the enumerator is created.
250 * @remarks The specific error code can be accessed using GetLastResult() method.
252 IteratorT< T > operator--(int)
254 IteratorT< T > tempIter = *this;
260 * Checks the two %IteratorT instances for equality.
264 * @return @c true if every member of the specified %IteratorT instance equals the calling instance's members, @n
266 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
268 bool operator==(const IteratorT< T >& rhs) const
270 if (__pList != rhs.__pList)
275 if (__index != rhs.__index)
280 if (__isPostEnd != rhs.__isPostEnd)
284 else if (__isPostEnd && rhs.__isPostEnd)
286 // In this case, __currentObj state is invalid
290 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
291 return __currentObj == rhs.__currentObj;
295 * Checks the two %IteratorT instances for inequality.
299 * @return @c true if any member of the specified %IteratorT instance is not equal to the calling instance's members, @n
301 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
303 bool operator!=(const IteratorT< T >& rhs) const
305 return !operator==(rhs);
309 * Swaps values of two %IteratorT instances.
313 * @param[in] rhs A reference to a %IteratorT instance to swap
315 void swap(IteratorT< T >& rhs)
317 std::swap(__pList, rhs.__pList);
318 std::swap(__isPostEnd, rhs.__isPostEnd);
319 std::swap(__index, rhs.__index);
320 std::swap(__pEnum, rhs.__pEnum);
321 std::swap(__currentObj, rhs.__currentObj);
325 const IList* __pList;
328 std::unique_ptr< IBidirectionalEnumerator > __pEnum;
332 }}} // Tizen::Base::Collection
334 #endif //_FBASE_COL_ITERATOR_T_H_