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 FBaseColRandomIteratorT.h
20 * @brief This is the header file for the %RandomIteratorT class.
22 * This header file contains the declarations of the %RandomIteratorT class.
25 #ifndef _FBASE_COL_RANDOM_ITERATOR_T_H_
26 #define _FBASE_COL_RANDOM_ITERATOR_T_H_
28 #include <algorithm> // std::swap (Before C++11)
30 #include <FBaseColIList.h>
32 #include <FBaseObject.h>
34 namespace Tizen { namespace Base { namespace Collection
37 * @class RandomIteratorT
38 * @brief This class provides an random iterator that is used to convert IList to STL containers.
39 * StlConverter provides static methods to get this random iterator from IList.
43 * @remarks This 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.
47 template < typename T >
49 : public std::iterator< std::input_iterator_tag, T >
53 * Initializes this instance of %RandomIteratorT class.
57 * @param[in] list A reference to the IList instance to convert
58 * @param[in] position A start position
59 * @remarks RandomIteratorT only supports random accessible collection for performance.
60 * @see Tizen::Base::Collection::IList::IsRandomAccessible()
62 explicit RandomIteratorT(const IList& list, int index = 0)
65 , __currentObj(static_cast< T >(const_cast< Object* >(__pList->GetAt(__index))))
67 AppAssertf(list.IsRandomAccessible(), "The list is not randomly accessible. RandomIteratorT only supports random accessible collection.");
71 * This is copy constructor of %RandomIteratorT class.
75 * @param[in] rhs A reference to the %RandomIteratorT instance
77 RandomIteratorT(const RandomIteratorT< T >& rhs)
78 : __pList(rhs.__pList)
79 , __index(rhs.__index)
80 , __currentObj(rhs.__currentObj)
85 * This is the assignment operator of %RandomIteratorT class.
89 * @return A reference to the %RandomIteratorT instance
90 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
92 RandomIteratorT< T >& operator=(const RandomIteratorT< T >& rhs)
94 RandomIteratorT< T > tmp(rhs);
100 * This is the indirection operator for %RandomIteratorT class.
104 * @return A T type reference
106 T& operator*(void) const
108 AppAssertf(__index >= 0 && __index < __pList->GetCount(), "It is out of range.");
109 return const_cast< T& >(__currentObj);
113 * This is the structure dereference operator for %RandomIteratorT class.
117 * @return A T type pointer equivalent to the pointer address
119 T* operator->(void) const
121 return &(operator*());
125 * Increases __index by 1.
129 * @return A reference to the %RandomIteratorT instance
130 * @exception E_SUCCESS The method is successful.
131 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
132 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
133 * @remarks The specific error code can be accessed using GetLastResult() method.
135 RandomIteratorT< T >& operator++(void)
139 // GetAt() will return null if __index is out of range.
140 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
141 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
146 * Increases __index by 1 and returns the previous state.
150 * @return A %RandomIteratorT instance
151 * @exception E_SUCCESS The method is successful.
152 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
153 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
154 * @remarks The specific error code can be accessed using GetLastResult() method.
156 RandomIteratorT< T > operator++(int)
158 RandomIteratorT< T > tempIter = *this;
164 * Decrease __index by 1.
168 * @return A reference to the %RandomIteratorT instance
169 * @exception E_SUCCESS The method is successful.
170 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
171 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
172 * @remarks The specific error code can be accessed using GetLastResult() method.
174 RandomIteratorT< T >& operator--(void)
178 // GetAt() will return null if __index is out of range.
179 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
180 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
185 * Decrease __index by 1 and returns the previous state.
189 * @return A %RandomIteratorT instance
190 * @exception E_SUCCESS The method is successful.
191 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
192 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
193 * @remarks The specific error code can be accessed using GetLastResult() method.
195 RandomIteratorT< T > operator--(int)
197 RandomIteratorT< T > tempIter = *this;
203 * Checks two %RandomIteratorT instances for equality
207 * @return @c true if every member of the specified %RandomIteratorT instance equals the calling instance's members, @n
209 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
211 bool operator==(const RandomIteratorT< T >& rhs) const
213 return ((__pList == rhs.__pList) && (__index == rhs.__index) && (__currentObj == rhs.__currentObj));
217 * Checks two %RandomIteratorT instances for inequality.
221 * @return @c true if every member of the specified %RandomIteratorT instance is not equal to the calling instance's members, @n
223 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
225 bool operator!=(const RandomIteratorT< T >& rhs) const
227 return !operator==(rhs);
231 * Checks l-value is less than r-value
235 * @return @c true if l-value of the specified %RandomIteratorT instance is less than the calling instance's members, @n
237 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
239 bool operator<(const RandomIteratorT< T >& rhs) const
241 return __index < rhs.__index;
245 * Checks whether l-value is greater than r-value
249 * @return @c true if l-value of the specified %RandomIteratorT instance is greater than the calling instance's members, @n
251 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
253 bool operator>(const RandomIteratorT< T >& rhs) const
255 return __index > rhs.__index;
259 * Increases __index as many as diff
263 * @return A %RandomIteratorT instance
264 * @param[in] diff The length to move forward
265 * @exception E_SUCCESS The method is successful.
266 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
267 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
268 * @remarks The specific error code can be accessed using GetLastResult() method.
270 RandomIteratorT< T > operator+(int diff)
272 RandomIteratorT< T > tempIter = *this;
273 tempIter.__index += diff;
275 // GetAt() will return null if __index is out of range.
276 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
277 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
282 * Decrease __index as many as diff
286 * @return A %RandomIteratorT instance
287 * @param[in] diff The length to move backward
288 * @exception E_SUCCESS The method is successful.
289 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
290 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
291 * @remarks The specific error code can be accessed using GetLastResult() method.
293 RandomIteratorT< T > operator-(int diff)
295 RandomIteratorT< T > tempIter = *this;
296 tempIter.__index -= diff;
298 // GetAt() will return null if __index is out of range.
299 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
300 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
304 int operator-(const RandomIteratorT< T >& rhs)
306 return __index - rhs.__index;
310 * This is the index operator for %RandomIteratorT class.
314 * @return A reference to the T type instance
315 * @param[in] index An index to reach
316 * @exception E_SUCCESS The method is successful.
317 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
318 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
319 * @remarks The specific error code can be accessed using GetLastResult() method.
321 T& operator[](int index) const
323 // GetAt() will return null if __index is out of range.
324 const T& tempObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(index)));
325 TryReturnResult(tempObj != null, const_cast< T& >(tempObj), GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
326 return const_cast< T& >(tempObj);
330 * Exchanges values of the two %RandomIteratorT instances
334 * @param[in] rhs A reference to the %RandomIteratorT instance to swap
336 void swap(RandomIteratorT< T >& rhs)
338 std::swap(__pList, rhs.__pList);
339 std::swap(__index, rhs.__index);
340 std::swap(__currentObj, rhs.__currentObj);
344 const IList* __pList;
347 }; // RandomIteratorT
349 }}} // Tizen::Base::Collection
351 #endif //_FBASE_COL_RANDOM_ITERATOR_T_H_