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 FBaseColRandomIteratorT.h
19 * @brief This is the header file for the %RandomIteratorT class.
21 * This header file contains the declarations of the %RandomIteratorT class.
24 #ifndef _FBASE_COL_RANDOM_ITERATOR_T_H_
25 #define _FBASE_COL_RANDOM_ITERATOR_T_H_
27 #include <algorithm> // std::swap (Before C++11)
29 #include <FBaseColIList.h>
31 #include <FBaseObject.h>
33 namespace Tizen { namespace Base { namespace Collection
36 * @class RandomIteratorT
37 * @brief This class provides a random iterator that is used to convert %IList to STL containers. @n
38 * %StlConverter provides static methods to get this random iterator from %IList.
42 * @remarks The %RandomIteratorT class satisfies only requirements of C++ standard library InputIterator concept due to limitations of %Tizen collection.
43 * So, this class can be used with C++ standard library algorithms which requires only InputIterator concept for their arguments.
45 * The %RandomIteratorT class provides a random iterator that is used to convert IList to STL containers.
46 * StlConverter provides static methods to get this random iterator from IList.
49 template< typename T >
51 : public std::iterator< std::input_iterator_tag, T >
55 * Initializes this instance of %RandomIteratorT class.
59 * @param[in] list A reference to the IList instance to convert
60 * @param[in] index A start index
61 * @remarks %RandomIteratorT only supports random accessible collection for performance.
62 * @see Tizen::Base::Collection::IList::IsRandomAccessible()
64 explicit RandomIteratorT(const IList& list, int index = 0)
67 , __currentObj(static_cast< T >(const_cast< Object* >(__pList->GetAt(__index))))
69 AppAssertf(list.IsRandomAccessible(), "The list is not randomly accessible. RandomIteratorT only supports random accessible collection.");
73 * This is the copy constructor of the %RandomIteratorT class.
77 * @param[in] rhs A reference to the %RandomIteratorT instance
79 RandomIteratorT(const RandomIteratorT< T >& rhs)
80 : __pList(rhs.__pList)
81 , __index(rhs.__index)
82 , __currentObj(rhs.__currentObj)
87 * This is the assignment operator of the %RandomIteratorT class.
91 * @return A reference to the %RandomIteratorT instance
92 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
94 RandomIteratorT< T >& operator =(const RandomIteratorT< T >& rhs)
96 RandomIteratorT< T > tmp(rhs);
102 * This is the indirection operator for the %RandomIteratorT class.
106 * @return A T type reference
108 T& operator *(void) const
110 AppAssertf(__index >= 0 && __index < __pList->GetCount(), "It is out of range.");
111 return const_cast< T& >(__currentObj);
115 * This is the structure dereference operator for the %RandomIteratorT class.
119 * @return A T type pointer equivalent to the pointer address
121 T* operator ->(void) const
123 return &(operator *());
127 * Increases __index by 1.
131 * @return A reference to the %RandomIteratorT instance
132 * @exception E_SUCCESS The method is successful.
133 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
134 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
135 * @remarks The specific error code can be accessed using GetLastResult() method.
137 RandomIteratorT< T >& operator ++(void)
141 // GetAt() will return null if __index is out of range.
142 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
143 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
148 * Increases __index by 1 and returns the previous state.
152 * @return A %RandomIteratorT instance
153 * @exception E_SUCCESS The method is successful.
154 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
155 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
156 * @remarks The specific error code can be accessed using GetLastResult() method.
158 RandomIteratorT< T > operator ++(int)
160 RandomIteratorT< T > tempIter = *this;
166 * Decrease __index by 1.
170 * @return A reference to the %RandomIteratorT instance
171 * @exception E_SUCCESS The method is successful.
172 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
173 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
174 * @remarks The specific error code can be accessed using GetLastResult() method.
176 RandomIteratorT< T >& operator --(void)
180 // GetAt() will return null if __index is out of range.
181 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
182 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
187 * Decrease __index by 1 and returns the previous state.
191 * @return A %RandomIteratorT instance
192 * @exception E_SUCCESS The method is successful.
193 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
194 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
195 * @remarks The specific error code can be accessed using GetLastResult() method.
197 RandomIteratorT< T > operator --(int)
199 RandomIteratorT< T > tempIter = *this;
205 * Checks two %RandomIteratorT instances for equality.
209 * @return @c true if every member of the specified %RandomIteratorT instance equals the calling instance's members, @n
211 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
213 bool operator ==(const RandomIteratorT< T >& rhs) const
215 return ((__pList == rhs.__pList) && (__index == rhs.__index) && (__currentObj == rhs.__currentObj));
219 * Checks two %RandomIteratorT instances for inequality.
223 * @return @c true if any member of the specified %RandomIteratorT instance is not equal to the calling instance's members, @n
225 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
227 bool operator !=(const RandomIteratorT< T >& rhs) const
229 return !operator ==(rhs);
233 * Checks l-value is less than r-value.
237 * @return @c true if l-value of the specified %RandomIteratorT instance is less than the calling instance's members, @n
239 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
241 bool operator <(const RandomIteratorT< T >& rhs) const
243 return __index < rhs.__index;
247 * Checks whether l-value is greater than r-value.
251 * @return @c true if l-value of the specified %RandomIteratorT instance is greater than the calling instance's members, @n
253 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
255 bool operator >(const RandomIteratorT< T >& rhs) const
257 return __index > rhs.__index;
261 * Increases __index as specified by the diff parameter.
265 * @return A %RandomIteratorT instance
266 * @param[in] diff The length to move forward
267 * @exception E_SUCCESS The method is successful.
268 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
269 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
270 * @remarks The specific error code can be accessed using GetLastResult() method.
272 RandomIteratorT< T > operator +(int diff)
274 RandomIteratorT< T > tempIter = *this;
275 tempIter.__index += diff;
277 // GetAt() will return null if __index is out of range.
278 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
279 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
284 * Decrease __index as specified by the diff parameter.
288 * @return A %RandomIteratorT instance
289 * @param[in] diff The length to move backward
290 * @exception E_SUCCESS The method is successful.
291 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
292 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
293 * @remarks The specific error code can be accessed using GetLastResult() method.
295 RandomIteratorT< T > operator -(int diff)
297 RandomIteratorT< T > tempIter = *this;
298 tempIter.__index -= diff;
300 // GetAt() will return null if __index is out of range.
301 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
302 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
306 int operator -(const RandomIteratorT< T >& rhs)
308 return __index - rhs.__index;
312 * This is the index operator for the %RandomIteratorT class.
316 * @return A reference to the T type instance
317 * @param[in] index An index to reach
318 * @exception E_SUCCESS The method is successful.
319 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
320 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
321 * @remarks The specific error code can be accessed using GetLastResult() method.
323 T& operator [](int index) const
325 // GetAt() will return null if __index is out of range.
326 const T& tempObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(index)));
327 TryReturnResult(tempObj != null, const_cast< T& >(tempObj), GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
328 return const_cast< T& >(tempObj);
332 * Swaps values of the two %RandomIteratorT instances.
336 * @param[in] rhs A reference to the %RandomIteratorT instance to swap
338 void swap(RandomIteratorT< T >& rhs)
340 std::swap(__pList, rhs.__pList);
341 std::swap(__index, rhs.__index);
342 std::swap(__currentObj, rhs.__currentObj);
346 const IList* __pList;
349 }; // RandomIteratorT
351 }}} // Tizen::Base::Collection
353 #endif //_FBASE_COL_RANDOM_ITERATOR_T_H_