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 requirements of the C++ standard library mutable BidirectionalIterator.
44 * It satisfies the requirements of an OutputIterator which can be dereferenced as an lvalue.
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::bidirectional_iterator_tag, T >
55 typedef std::bidirectional_iterator_tag iterator_category;
56 typedef typename std::iterator_traits< IteratorT< T > >::value_type value_type;
57 typedef typename std::iterator_traits< IteratorT< T > >::difference_type difference_type;
58 typedef typename std::iterator_traits< IteratorT< T > >::pointer pointer;
59 typedef typename std::iterator_traits< IteratorT< T > >::reference reference;
62 * This is the default constructor for this class.
75 * Initializes an instance of %IteratorT class.
79 * @param[in] list A reference to the IList instance to convert
80 * @param[in] isPostEnd A boolean value to check the end of a list
82 explicit IteratorT(IList& list, bool isPostEnd = false)
84 , __isPostEnd(isPostEnd)
86 , __pEnum(__pList->GetBidirectionalEnumeratorN())
88 if (__pList->GetCount() != 0)
96 __index = __pList->GetCount();
97 __pEnum->MovePrevious();
102 // Control reaches here intentionally because begin() should be equal to end()
108 * This is the copy constructor of the %IteratorT class.
112 * @param[in] rhs A reference to the %IteratorT instance
114 IteratorT(const IteratorT< T >& rhs)
115 : __pList(rhs.__pList)
116 , __isPostEnd(rhs.__isPostEnd)
117 , __index(rhs.__index)
118 , __pEnum(__pList->GetBidirectionalEnumeratorN())
122 for (int i = 0; i <= __index; ++i)
129 __pEnum->MovePrevious();
134 * This is an assignment operator of the %IteratorT class.
138 * @return A reference to the %IteratorT instance
139 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
141 IteratorT< T >& operator =(const IteratorT< T >& rhs)
143 IteratorT< T > tmp(rhs);
149 * This is the indirection operator for the %IteratorT class.
153 * @return A T type reference
155 reference operator *(void) const
157 AppAssertf(!__isPostEnd && __index >= 0, "It is out of range.");
158 return reinterpret_cast< T& >(__pEnum->GetCurrentRef());
162 * This is a structure dereference operator for the %IteratorT class.
166 * @return A T type pointer that is equivalent to the pointer address
168 pointer operator ->(void) const
170 return &(operator *());
174 * Moves to the next element in the collection.
178 * @return A reference to the %IteratorT type instance
179 * @exception E_SUCCESS The method is successful.
180 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
181 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
182 * the collection is modified after the enumerator is created.
183 * @remarks The specific error code can be accessed using GetLastResult() method.
185 IteratorT< T >& operator ++(void)
187 const int PRE_BEGIN_IDX = -1;
188 TryCatchResult(__index >= PRE_BEGIN_IDX, , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
190 if (__index != PRE_BEGIN_IDX)
192 result r = __pEnum->MoveNext();
193 TryCatchResult(r == E_SUCCESS, __isPostEnd = true, r, "[%s] It already reached the end.", GetErrorMessage(r));
202 * Moves to the next element of the collection and returns the previous state.
206 * @return An %IteratorT instance
207 * @exception E_SUCCESS The method is successful.
208 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
209 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
210 * the collection is modified after the enumerator is created.
211 * @remarks The specific error code can be accessed using GetLastResult() method.
213 IteratorT< T > operator ++(int)
215 IteratorT< T > tempIter = *this;
221 * Moves to the previous element of the collection.
225 * @return A reference to the %IteratorT type instance
226 * @exception E_SUCCESS The method is successful.
227 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
228 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
229 * the collection is modified after the enumerator is created.
230 * @remarks The specific error code can be accessed using GetLastResult() method.
232 IteratorT< T >& operator --(void)
234 TryCatchResult(__index <= __pList->GetCount(), , E_OUT_OF_RANGE, "[%s] It is out of range.", GetErrorMessage(E_OUT_OF_RANGE));
238 result r = __pEnum->MovePrevious();
239 TryCatchResult(r == E_SUCCESS, , r, "[%s] It already reached the front.", GetErrorMessage(r));
252 * Moves to the previous element of the collection and returns the previous state.
256 * @return An %IteratorT instance
257 * @exception E_SUCCESS The method is successful.
258 * @exception E_OUT_OF_RANGE The iterator is outside the bounds of the list.
259 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
260 * the collection is modified after the enumerator is created.
261 * @remarks The specific error code can be accessed using GetLastResult() method.
263 IteratorT< T > operator --(int)
265 IteratorT< T > tempIter = *this;
271 * Checks the two %IteratorT instances for equality.
275 * @return @c true if every member of the specified %IteratorT instance equals the calling instance's members, @n
277 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
279 bool operator ==(const IteratorT< T >& rhs) const
281 if (__pList != rhs.__pList)
286 if (__index != rhs.__index)
291 if (__isPostEnd != rhs.__isPostEnd)
295 else if (__isPostEnd && rhs.__isPostEnd)
297 // In this case, the current object which the iterator refers to is invalid.
301 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
302 return __pEnum->GetCurrentRef() == rhs.__pEnum->GetCurrentRef();
306 * Checks the two %IteratorT instances for inequality.
310 * @return @c true if any member of the specified %IteratorT instance is not equal to the calling instance's members, @n
312 * @param[in] rhs A reference to the %IteratorT instance on the right-hand side of the operator
314 bool operator !=(const IteratorT< T >& rhs) const
316 return !operator ==(rhs);
320 * Swaps values of two %IteratorT instances.
324 * @param[in] rhs A reference to a %IteratorT instance to swap
326 void swap(IteratorT< T >& rhs)
328 std::swap(__pList, rhs.__pList);
329 std::swap(__isPostEnd, rhs.__isPostEnd);
330 std::swap(__index, rhs.__index);
331 std::swap(__pEnum, rhs.__pEnum);
337 difference_type __index;
338 std::unique_ptr< IBidirectionalEnumerator > __pEnum;
341 }}} // Tizen::Base::Collection
343 #endif //_FBASE_COL_ITERATOR_T_H_