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 FBaseColPairIteratorT.h
19 * @brief This is the header file for the %PairIteratorT class.
21 * This header file contains the declarations of the %PairIteratorT class.
24 #ifndef _FBASE_COL_PAIR_ITERATOR_T_H_
25 #define _FBASE_COL_PAIR_ITERATOR_T_H_
27 #include <algorithm> // std::swap (Before C++11)
30 #include <unique_ptr.h>
32 #include <FBaseColIMap.h>
33 #include <FBaseColIMultiMap.h>
34 #include <FBaseColIMapEnumerator.h>
36 namespace Tizen { namespace Base { namespace Collection
39 * @class PairIteratorT
40 * @brief This class provides an iterator that is used to convert %IMap or %IMultiMap to STL containers. @n
41 * %StlConverter provides static methods to get this iterator from %IMap or %IMultiMap.
45 * @remarks The %PairIteratorT class satisfies requirements of C++ standard library ForwardIterator which is not mutable.
46 * It does not satisfy the requirements of an OutputIterator which can be dereferenced as an lvalue.
48 * The %PairIteratorT class provides an iterator that is used to convert IMap or IMultiMap to STL containers.
49 * StlConverter provides static methods to get this iterator from IMap or IMultiMap.
52 template< typename K, typename V >
54 : public std::iterator< std::forward_iterator_tag, std::pair< K, V > >
57 typedef std::forward_iterator_tag iterator_category;
58 typedef typename std::iterator_traits< PairIteratorT< K, V > >::value_type value_type;
59 typedef typename std::iterator_traits< PairIteratorT< K, V > >::difference_type difference_type;
60 typedef typename std::iterator_traits< PairIteratorT< K, V > >::pointer pointer;
61 typedef typename std::iterator_traits< PairIteratorT< K, V > >::reference reference;
64 * This is the default constructor for this class.
79 * Initializes this instance of %PairIteratorT class.
83 * @param[in] map A reference to the IMap instance to convert
84 * @param[in] isPostEnd A boolean value to check the end
86 explicit PairIteratorT(const IMap& map, bool isPostEnd = false)
89 , __isPostEnd(isPostEnd)
91 , __pEnum(__pMap->GetMapEnumeratorN())
94 if (__pMap->GetCount() != 0)
99 __currentObj.first = static_cast< K >(__pEnum->GetKey());
100 __currentObj.second = static_cast< V >(__pEnum->GetValue());
104 __index = __pMap->GetCount();
109 // Control reaches here intentionally because begin() should be equal to end()
115 * Initializes this instance of %PairIteratorT class.
119 * @param[in] multiMap A reference to the IMultiMap instance to convert
120 * @param[in] isPostEnd A boolean value to check the end
122 PairIteratorT(const IMultiMap& multiMap, bool isPostEnd = false)
124 , __pMultiMap(&multiMap)
125 , __isPostEnd(isPostEnd)
127 , __pEnum(__pMultiMap->GetMapEnumeratorN())
130 if (__pMultiMap->GetCount() != 0)
135 __currentObj.first = static_cast< K >(__pEnum->GetKey());
136 __currentObj.second = static_cast< V >(__pEnum->GetValue());
140 __index = __pMultiMap->GetCount();
145 // Control reaches here intentionally because begin() should be equal to end()
151 * This is the copy constructor of the %PairIteratorT class.
155 * @param[in] rhs A reference to the %PairIteratorT instance
156 * @exception E_SUCCESS The method is successful.
157 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
158 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
159 * the collection is modified after the enumerator is created.
160 * @remarks The specific error code can be accessed using GetLastResult() method.
162 PairIteratorT(const PairIteratorT< K, V >& rhs)
164 , __pMultiMap(rhs.__pMultiMap)
165 , __isPostEnd(rhs.__isPostEnd)
166 , __index(rhs.__index)
167 , __currentObj(rhs.__currentObj)
169 TryReturnVoidResult(__pMap != null || __pMultiMap != null, E_INVALID_ARG, "[%s] __pMap or __pMultiMap should not be null.", GetErrorMessage(E_INVALID_ARG));
173 __pEnum.reset(__pMap->GetMapEnumeratorN());
175 else if (__pMultiMap != null)
177 __pEnum.reset(__pMultiMap->GetMapEnumeratorN());
182 for (int i = 0; i <= __index; ++i)
190 * This is assignment operator of the %PairIteratorT class.
194 * @return A reference to the %PairIteratorT instance
195 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
196 * @exception E_SUCCESS The method is successful.
197 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
198 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
199 * the collection is modified after the enumerator is created.
200 * @remarks The specific error code can be accessed using GetLastResult() method.
202 PairIteratorT< K, V >& operator =(const PairIteratorT< K, V >& rhs)
204 PairIteratorT< K, V > tmp(rhs);
210 * This is the indirection operator for the %PairIteratorT class.
214 * @return A std::pair type reference with K and V type
216 reference operator *(void) const
218 AppAssertf(!__isPostEnd, "It is out of range.");
219 return const_cast< std::pair< K, V >& >(__currentObj);
223 * This is the structure dereference operator for the %PairIteratorT class.
227 * @return A std::pair type pointer equivalent to the pointer address
229 pointer operator ->(void) const
231 return &(operator *());
235 * Moves to the next element of the collection.
239 * @return A reference to the %PairIteratorT instance
240 * @exception E_SUCCESS The method is successful.
241 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
242 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
243 * the collection is modified after the enumerator is created.
244 * @remarks The specific error code can be accessed using GetLastResult() method.
246 PairIteratorT< K, V >& operator ++(void)
248 TryReturnResult(!__isPostEnd, *this, E_OUT_OF_RANGE, "[%s] It already reached the end.", GetErrorMessage(E_OUT_OF_RANGE));
250 result r = __pEnum->MoveNext();
251 TryCatchResult(r == E_SUCCESS, __isPostEnd = true;
252 __currentObj.first = null;
253 __currentObj.second = null,
254 r, "[%s] It already reached the end.", GetErrorMessage(r));
256 __currentObj.first = static_cast< K >(__pEnum->GetKey());
257 __currentObj.second = static_cast< V >(__pEnum->GetValue());
265 * Moves to the next element of the collection and returns the previous state.
269 * @return A %PairIteratorT instance
270 * @exception E_SUCCESS The method is successful.
271 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
272 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
273 * the collection is modified after the enumerator is created.
274 * @remarks The specific error code can be accessed using GetLastResult() method.
276 PairIteratorT< K, V > operator ++(int)
278 PairIteratorT< K, V > tempIter = *this;
284 * Checks the two %PairIteratorT instances for equality.
288 * @return @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
290 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
292 bool operator ==(const PairIteratorT< K, V >& rhs) const
294 if (__pMap != rhs.__pMap)
299 if (__pMultiMap != rhs.__pMultiMap)
304 if (__index != rhs.__index)
309 if (__isPostEnd != rhs.__isPostEnd)
313 else if (__isPostEnd && rhs.__isPostEnd)
315 // In this case, __currentObj state is invalid
319 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
320 return __currentObj == rhs.__currentObj;
324 * Checks the two %PairIteratorT instances for inequality.
328 * @return @c true if any member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
330 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
332 bool operator !=(const PairIteratorT< K, V >& rhs) const
334 return !operator ==(rhs);
338 * Swaps values of the two %PairIteratorT instances.
342 * @param[in] rhs A reference to the %PairIteratorT instance to swap
344 void swap(PairIteratorT< K, V >& rhs)
346 std::swap(__pMap, rhs.__pMap);
347 std::swap(__pMultiMap, rhs.__pMultiMap);
348 std::swap(__isPostEnd, rhs.__isPostEnd);
349 std::swap(__index, rhs.__index);
350 std::swap(__pEnum, rhs.__pEnum);
351 std::swap(__currentObj, rhs.__currentObj);
356 const IMultiMap* __pMultiMap;
358 difference_type __index;
359 std::unique_ptr< IMapEnumerator > __pEnum;
360 value_type __currentObj;
363 }}} // Tizen::Base::Collection
365 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_