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 the requirements of the C++ standard library InputIterator concept due to the limitations of the %Tizen collection. @n
43 * This class can be used with the C++ standard library algorithms which require only the 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 The starting index
61 * @remarks %RandomIteratorT only supports random accessible collection for its 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 Either of the following conditions has occurred:
134 * - The specified @c index is outside the bounds of the data structure.
135 * - The specified @c index is either greater than or equal to the number of elements in the list.
136 * - The specified @c index is less than @c 0.
137 * @remarks The specific error code can be accessed using GetLastResult() method.
139 RandomIteratorT< T >& operator ++(void)
143 // GetAt() will return null if __index is out of range.
144 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
145 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
150 * Increases __index by 1 and returns the previous state.
154 * @return A %RandomIteratorT instance
155 * @exception E_SUCCESS The method is successful.
156 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
157 * - The specified @c index is outside the bounds of the data structure.
158 * - The specified @c index is either greater than or equal to the number of elements in the list.
159 * - The specified @c index is less than @c 0.
160 * @remarks The specific error code can be accessed using GetLastResult() method.
162 RandomIteratorT< T > operator ++(int)
164 RandomIteratorT< T > tempIter = *this;
170 * Decreases __index by 1.
174 * @return A reference to the %RandomIteratorT instance
175 * @exception E_SUCCESS The method is successful.
176 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
177 * - The specified @c index is outside the bounds of the data structure.
178 * - The specified @c index is either greater than or equal to the number of elements in the list.
179 * - The specified @c index is less than @c 0.
180 * @remarks The specific error code can be accessed using GetLastResult() method.
182 RandomIteratorT< T >& operator --(void)
186 // GetAt() will return null if __index is out of range.
187 __currentObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(__index)));
188 TryReturnResult(__currentObj != null, *this, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
193 * Decreases __index by 1 and returns the previous state.
197 * @return A %RandomIteratorT instance
198 * @exception E_SUCCESS The method is successful.
199 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
200 * - The specified @c index is outside the bounds of the data structure.
201 * - The specified @c index is either greater than or equal to the number of elements in the list.
202 * - The specified @c index is less than @c 0.
203 * @remarks The specific error code can be accessed using GetLastResult() method.
205 RandomIteratorT< T > operator --(int)
207 RandomIteratorT< T > tempIter = *this;
213 * Checks two %RandomIteratorT instances for equality.
217 * @return @c true if every member of the specified %RandomIteratorT instance equals the calling instance's members, @n
219 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
221 bool operator ==(const RandomIteratorT< T >& rhs) const
223 return ((__pList == rhs.__pList) && (__index == rhs.__index) && (__currentObj == rhs.__currentObj));
227 * Checks two %RandomIteratorT instances for inequality.
231 * @return @c true if any member of the specified %RandomIteratorT instance is not equal to the calling instance's members, @n
233 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
235 bool operator !=(const RandomIteratorT< T >& rhs) const
237 return !operator ==(rhs);
241 * Checks whether the l-value is less than the r-value.
245 * @return @c true if the l-value of the specified %RandomIteratorT instance is less than the calling instance's members, @n
247 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
249 bool operator <(const RandomIteratorT< T >& rhs) const
251 return __index < rhs.__index;
255 * Checks whether the l-value is greater than the r-value.
259 * @return @c true if the l-value of the specified %RandomIteratorT instance is greater than the calling instance's members, @n
261 * @param[in] rhs A reference to the %RandomIteratorT instance on the right-hand side of the operator
263 bool operator >(const RandomIteratorT< T >& rhs) const
265 return __index > rhs.__index;
269 * Increases __index as specified by the @c diff parameter.
273 * @return A %RandomIteratorT instance
274 * @param[in] diff The length to move forward
275 * @exception E_SUCCESS The method is successful.
276 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
277 * - The specified @c index is outside the bounds of the data structure.
278 * - The specified @c index is either greater than or equal to the number of elements in the list.
279 * - The specified @c index is less than @c 0.
280 * @remarks The specific error code can be accessed using GetLastResult() method.
282 RandomIteratorT< T > operator +(int diff)
284 RandomIteratorT< T > tempIter = *this;
285 tempIter.__index += diff;
287 // GetAt() will return null if __index is out of range.
288 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
289 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
294 * Decreases __index as specified by the @c diff parameter.
298 * @return A %RandomIteratorT instance
299 * @param[in] diff The length to move backward
300 * @exception E_SUCCESS The method is successful.
301 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
302 * - The specified @c index is outside the bounds of the data structure.
303 * - The specified @c index is either greater than or equal to the number of elements in the list.
304 * - The specified @c index is less than @c 0.
305 * @remarks The specific error code can be accessed using GetLastResult() method.
307 RandomIteratorT< T > operator -(int diff)
309 RandomIteratorT< T > tempIter = *this;
310 tempIter.__index -= diff;
312 // GetAt() will return null if __index is out of range.
313 tempIter.__currentObj = static_cast< T >(const_cast< Object* >(tempIter.__pList->GetAt(tempIter.__index)));
314 TryReturnResult(tempIter.__currentObj != null, tempIter, GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
318 int operator -(const RandomIteratorT< T >& rhs)
320 return __index - rhs.__index;
324 * This is the index operator for the %RandomIteratorT class.
328 * @return A reference to the T type instance
329 * @param[in] index The index to reach
330 * @exception E_SUCCESS The method is successful.
331 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
332 * - The specified @c index is outside the bounds of the data structure.
333 * - The specified @c index is either greater than or equal to the number of elements in the list.
334 * - The specified @c index is less than @c 0.
335 * @remarks The specific error code can be accessed using GetLastResult() method.
337 T& operator [](int index) const
339 // GetAt() will return null if __index is out of range.
340 const T& tempObj = static_cast< T >(const_cast< Object* >(__pList->GetAt(index)));
341 TryReturnResult(tempObj != null, const_cast< T& >(tempObj), GetLastResult(), "[%s] It is out of range.", GetErrorMessage(GetLastResult()));
342 return const_cast< T& >(tempObj);
346 * Swaps the values of the two %RandomIteratorT instances.
350 * @param[in] rhs A reference to the %RandomIteratorT instance to swap
352 void swap(RandomIteratorT< T >& rhs)
354 std::swap(__pList, rhs.__pList);
355 std::swap(__index, rhs.__index);
356 std::swap(__currentObj, rhs.__currentObj);
360 const IList* __pList;
363 }; // RandomIteratorT
365 }}} // Tizen::Base::Collection
367 #endif //_FBASE_COL_RANDOM_ITERATOR_T_H_