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 a random iterator that is used to convert IList to STL containers. @n
39 * StlConverter provides static methods to get this random iterator from %IList.
43 * @remarks The %RandomIteratorT 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.
46 * The %RandomIteratorT class provides a random iterator that is used to convert IList to STL containers.
47 * StlConverter provides static methods to get this random iterator from IList.
50 template < typename T >
52 : public std::iterator< std::input_iterator_tag, T >
56 * Initializes this instance of %RandomIteratorT class.
60 * @param[in] list A reference to the IList instance to convert
61 * @param[in] index A start index
62 * @remarks %RandomIteratorT only supports random accessible collection for performance.
63 * @see Tizen::Base::Collection::IList::IsRandomAccessible()
65 explicit RandomIteratorT(const IList& list, int index = 0)
68 , __currentObj(static_cast< T >(const_cast< Object* >(__pList->GetAt(__index))))
70 AppAssertf(list.IsRandomAccessible(), "The list is not randomly accessible. RandomIteratorT only supports random accessible collection.");
74 * This is the copy constructor of the %RandomIteratorT class.
78 * @param[in] rhs A reference to the %RandomIteratorT instance
80 RandomIteratorT(const RandomIteratorT< T >& rhs)
81 : __pList(rhs.__pList)
82 , __index(rhs.__index)
83 , __currentObj(rhs.__currentObj)
88 * This is the assignment operator of the %RandomIteratorT class.
92 * @return A reference to the %RandomIteratorT instance
93 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
95 RandomIteratorT< T >& operator=(const RandomIteratorT< T >& rhs)
97 RandomIteratorT< T > tmp(rhs);
103 * This is the indirection operator for the %RandomIteratorT class.
107 * @return A T type reference
109 T& operator*(void) const
111 AppAssertf(__index >= 0 && __index < __pList->GetCount(), "It is out of range.");
112 return const_cast< T& >(__currentObj);
116 * This is the structure dereference operator for the %RandomIteratorT class.
120 * @return A T type pointer equivalent to the pointer address
122 T* operator->(void) const
124 return &(operator*());
128 * Increases __index by 1.
132 * @return A reference to the %RandomIteratorT instance
133 * @exception E_SUCCESS The method is successful.
134 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
135 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
136 * @remarks The specific error code can be accessed using GetLastResult() method.
138 RandomIteratorT< T >& operator++(void)
142 // GetAt() will return null if __index is out of range.
143 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
144 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
149 * Increases __index by 1 and returns the previous state.
153 * @return A %RandomIteratorT instance
154 * @exception E_SUCCESS The method is successful.
155 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
156 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
157 * @remarks The specific error code can be accessed using GetLastResult() method.
159 RandomIteratorT< T > operator++(int)
161 RandomIteratorT< T > tempIter = *this;
167 * Decrease __index by 1.
171 * @return A reference to the %RandomIteratorT instance
172 * @exception E_SUCCESS The method is successful.
173 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
174 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
175 * @remarks The specific error code can be accessed using GetLastResult() method.
177 RandomIteratorT< T >& operator--(void)
181 // GetAt() will return null if __index is out of range.
182 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
183 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
188 * Decrease __index by 1 and returns the previous state.
192 * @return A %RandomIteratorT instance
193 * @exception E_SUCCESS The method is successful.
194 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
195 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
196 * @remarks The specific error code can be accessed using GetLastResult() method.
198 RandomIteratorT< T > operator--(int)
200 RandomIteratorT< T > tempIter = *this;
206 * Checks two %RandomIteratorT instances for equality.
210 * @return @c true if every member of the specified %RandomIteratorT instance equals the calling instance's members, @n
212 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
214 bool operator==(const RandomIteratorT< T >& rhs) const
216 return ((__pList == rhs.__pList) && (__index == rhs.__index) && (__currentObj == rhs.__currentObj));
220 * Checks two %RandomIteratorT instances for inequality.
224 * @return @c true if any member of the specified %RandomIteratorT instance is not equal to the calling instance's members, @n
226 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
228 bool operator!=(const RandomIteratorT< T >& rhs) const
230 return !operator==(rhs);
234 * Checks l-value is less than r-value.
238 * @return @c true if l-value of the specified %RandomIteratorT instance is less than the calling instance's members, @n
240 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
242 bool operator<(const RandomIteratorT< T >& rhs) const
244 return __index < rhs.__index;
248 * Checks whether l-value is greater than r-value.
252 * @return @c true if l-value of the specified %RandomIteratorT instance is greater than the calling instance's members, @n
254 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
256 bool operator>(const RandomIteratorT< T >& rhs) const
258 return __index > rhs.__index;
262 * Increases __index as specified by the diff parameter.
266 * @return A %RandomIteratorT instance
267 * @param[in] diff The length to move forward
268 * @exception E_SUCCESS The method is successful.
269 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
270 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
271 * @remarks The specific error code can be accessed using GetLastResult() method.
273 RandomIteratorT< T > operator+(int diff)
275 RandomIteratorT< T > tempIter = *this;
276 tempIter.__index += diff;
278 // GetAt() will return null if __index is out of range.
279 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
280 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
285 * Decrease __index as specified by the diff parameter.
289 * @return A %RandomIteratorT instance
290 * @param[in] diff The length to move backward
291 * @exception E_SUCCESS The method is successful.
292 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
293 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
294 * @remarks The specific error code can be accessed using GetLastResult() method.
296 RandomIteratorT< T > operator-(int diff)
298 RandomIteratorT< T > tempIter = *this;
299 tempIter.__index -= diff;
301 // GetAt() will return null if __index is out of range.
302 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
303 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
307 int operator-(const RandomIteratorT< T >& rhs)
309 return __index - rhs.__index;
313 * This is the index operator for the %RandomIteratorT class.
317 * @return A reference to the T type instance
318 * @param[in] index An index to reach
319 * @exception E_SUCCESS The method is successful.
320 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure,
321 * or the specified index is either equal to or greater than the number of elements in the list or less than 0.
322 * @remarks The specific error code can be accessed using GetLastResult() method.
324 T& operator[](int index) const
326 // GetAt() will return null if __index is out of range.
327 const T& tempObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(index)));
328 TryReturnResult(tempObj != null, const_cast< T& >(tempObj), GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
329 return const_cast< T& >(tempObj);
333 * Swaps values of the two %RandomIteratorT instances.
337 * @param[in] rhs A reference to the %RandomIteratorT instance to swap
339 void swap(RandomIteratorT< T >& rhs)
341 std::swap(__pList, rhs.__pList);
342 std::swap(__index, rhs.__index);
343 std::swap(__currentObj, rhs.__currentObj);
347 const IList* __pList;
350 }; // RandomIteratorT
352 }}} // Tizen::Base::Collection
354 #endif //_FBASE_COL_RANDOM_ITERATOR_T_H_