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.
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.
48 template < typename T >
50 : public std::iterator< std::input_iterator_tag, T >
54 * Initializes an instance of %IteratorT class.
58 * @param[in] list A reference to the IList instance to convert
59 * @param[in] isPostEnd A boolean value to check the end of a list
61 explicit IteratorT(const IList& list, bool isPostEnd = false)
63 , __isPostEnd(isPostEnd)
65 , __pEnum(__pList->GetBidirectionalEnumeratorN())
68 if (__pList->GetCount() != 0)
73 __currentObj = static_cast< T >(__pEnum->GetCurrent());
77 __index = __pList->GetCount();
78 __pEnum->MovePrevious();
83 // Control reaches here intentionally because begin() should be equal to end()
89 * This is the copy constructor of the %IteratorT class.
93 * @param[in] rhs A reference to the %IteratorT instance
95 IteratorT(const IteratorT< T >& rhs)
96 : __pList(rhs.__pList)
97 , __isPostEnd(rhs.__isPostEnd)
98 , __index(rhs.__index)
99 , __pEnum(__pList->GetBidirectionalEnumeratorN())
100 , __currentObj(rhs.__currentObj)
104 for (int i = 0; i <= __index; ++i)
111 __pEnum->MovePrevious();
116 * This is an assignment operator of the %IteratorT class.
120 * @return A reference to the %IteratorT instance
121 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
123 IteratorT< T >& operator=(const IteratorT< T >& rhs)
125 IteratorT< T > tmp(rhs);
131 * This is the indirection operator for the %IteratorT class.
135 * @return A T type reference
137 T& operator*(void) const
139 AppAssertf(!__isPostEnd && __index >= 0, "It is out of range.");
140 return const_cast< T& >(__currentObj);
144 * This is a structure dereference operator for the %IteratorT class.
148 * @return A T type pointer that is equivalent to the pointer address
150 T* operator->(void) const
152 return &(operator*());
156 * Moves to the next element in the collection.
160 * @return A reference to the %IteratorT type instance
161 * @exception E_SUCCESS The method is successful.
162 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
163 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
164 * the collection is modified after the enumerator is created.
165 * @remarks The specific error code can be accessed using GetLastResult() method.
167 IteratorT< T >& operator++(void)
169 const int PRE_BEGIN_IDX = -1;
170 TryCatchResult(__index >= PRE_BEGIN_IDX, , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
172 if (__index != PRE_BEGIN_IDX)
174 result r = __pEnum->MoveNext();
175 TryCatchResult(r == E_SUCCESS, __isPostEnd = true; __currentObj = null, r, "[%s] It already reached the end.", GetErrorMessage(r));
178 __currentObj = static_cast< T >(__pEnum->GetCurrent());
186 * Moves to the next element of the collection and returns the previous state.
190 * @return An %IteratorT instance
191 * @exception E_SUCCESS The method is successful.
192 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
193 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
194 * the collection is modified after the enumerator is created.
195 * @remarks The specific error code can be accessed using GetLastResult() method.
197 IteratorT< T > operator++(int)
199 IteratorT< T > tempIter = *this;
205 * Moves to the previous element of the collection.
209 * @return A reference to the %IteratorT type instance
210 * @exception E_SUCCESS The method is successful.
211 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
212 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
213 * the collection is modified after the enumerator is created.
214 * @remarks The specific error code can be accessed using GetLastResult() method.
216 IteratorT< T >& operator--(void)
218 TryCatchResult(__index <= __pList->GetCount(), , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
222 result r = __pEnum->MovePrevious();
223 TryCatchResult(r == E_SUCCESS, __currentObj = null, r, "[%s] It already reached the front.", GetErrorMessage(r));
230 __currentObj = static_cast< T >(__pEnum->GetCurrent());
238 * Moves to the previous element of the collection and returns the previous state.
242 * @return An %IteratorT instance
243 * @exception E_SUCCESS The method is successful.
244 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
245 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
246 * the collection is modified after the enumerator is created.
247 * @remarks The specific error code can be accessed using GetLastResult() method.
249 IteratorT< T > operator--(int)
251 IteratorT< T > tempIter = *this;
257 * Checks the two %IteratorT instances for equality.
261 * @return @c true if every member of the specified %IteratorT instance equals the calling instance's members, @n
263 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
265 bool operator==(const IteratorT< T >& rhs) const
267 if (__pList != rhs.__pList)
272 if (__index != rhs.__index)
277 if (__isPostEnd != rhs.__isPostEnd)
281 else if (__isPostEnd && rhs.__isPostEnd)
283 // In this case, __currentObj state is invalid
287 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
288 return __currentObj == rhs.__currentObj;
292 * Checks the two %IteratorT instances for inequality.
296 * @return @c true if any member of the specified %IteratorT instance is not equal to the calling instance's members, @n
298 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
300 bool operator!=(const IteratorT< T >& rhs) const
302 return !operator==(rhs);
306 * Swaps values of two %IteratorT instances.
310 * @param[in] rhs A reference to a %IteratorT instance to swap
312 void swap(IteratorT< T >& rhs)
314 std::swap(__pList, rhs.__pList);
315 std::swap(__isPostEnd, rhs.__isPostEnd);
316 std::swap(__index, rhs.__index);
317 std::swap(__pEnum, rhs.__pEnum);
318 std::swap(__currentObj, rhs.__currentObj);
322 const IList* __pList;
325 std::unique_ptr< IBidirectionalEnumerator > __pEnum;
329 }}} // Tizen::Base::Collection
331 #endif //_FBASE_COL_ITERATOR_T_H_