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 only requirements of C++ standard library InputIterator concept due to limitations of %Tizen Collection.
46 * So, this class can be used with C++ standard library algorithms which requires only InputIterator concept for their arguments.
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::input_iterator_tag, std::pair< K, V > >
58 * Initializes this instance of %PairIteratorT class.
62 * @param[in] map A reference to the IMap instance to convert
63 * @param[in] isPostEnd A boolean value to check the end
65 explicit PairIteratorT(const IMap& map, bool isPostEnd = false)
68 , __isPostEnd(isPostEnd)
70 , __pEnum(__pMap->GetMapEnumeratorN())
73 if (__pMap->GetCount() != 0)
78 __currentObj.first = static_cast< K >(__pEnum->GetKey());
79 __currentObj.second = static_cast< V >(__pEnum->GetValue());
83 __index = __pMap->GetCount();
88 // Control reaches here intentionally because begin() should be equal to end()
94 * Initializes this instance of %PairIteratorT class.
98 * @param[in] multiMap A reference to the IMultiMap instance to convert
99 * @param[in] isPostEnd A boolean value to check the end
101 PairIteratorT(const IMultiMap& multiMap, bool isPostEnd = false)
103 , __pMultiMap(&multiMap)
104 , __isPostEnd(isPostEnd)
106 , __pEnum(__pMultiMap->GetMapEnumeratorN())
109 if (__pMultiMap->GetCount() != 0)
114 __currentObj.first = static_cast< K >(__pEnum->GetKey());
115 __currentObj.second = static_cast< V >(__pEnum->GetValue());
119 __index = __pMultiMap->GetCount();
124 // Control reaches here intentionally because begin() should be equal to end()
130 * This is the copy constructor of the %PairIteratorT class.
134 * @param[in] rhs A reference to the %PairIteratorT instance
135 * @exception E_SUCCESS The method is successful.
136 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
137 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
138 * the collection is modified after the enumerator is created.
139 * @remarks The specific error code can be accessed using GetLastResult() method.
141 PairIteratorT(const PairIteratorT< K, V >& rhs)
143 , __pMultiMap(rhs.__pMultiMap)
144 , __isPostEnd(rhs.__isPostEnd)
145 , __index(rhs.__index)
146 , __currentObj(rhs.__currentObj)
148 TryReturnVoidResult(__pMap != null || __pMultiMap != null, E_INVALID_ARG, "[%s] __pMap or __pMultiMap should not be null.", GetErrorMessage(E_INVALID_ARG));
152 __pEnum.reset(__pMap->GetMapEnumeratorN());
154 else if (__pMultiMap != null)
156 __pEnum.reset(__pMultiMap->GetMapEnumeratorN());
161 for (int i = 0; i <= __index; ++i)
169 * This is assignment operator of the %PairIteratorT class.
173 * @return A reference to the %PairIteratorT instance
174 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
175 * @exception E_SUCCESS The method is successful.
176 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
177 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
178 * the collection is modified after the enumerator is created.
179 * @remarks The specific error code can be accessed using GetLastResult() method.
181 PairIteratorT< K, V >& operator=(const PairIteratorT< K, V >& rhs)
183 PairIteratorT< K, V > tmp(rhs);
189 * This is the indirection operator for the %PairIteratorT class.
193 * @return A std::pair type reference with K and V type
195 std::pair< K, V >& operator*(void) const
197 AppAssertf(!__isPostEnd, "It is out of range.");
198 return const_cast< std::pair< K, V >& >(__currentObj);
202 * This is the const version structure dereference operator for the %PairIteratorT class.
206 * @return A std::pair type pointer equivalent to the pointer address
208 std::pair< K, V >* operator->(void) const
210 return &(operator*());
214 * Moves to the next element of the collection.
218 * @return A reference to the %PairIteratorT instance
219 * @exception E_SUCCESS The method is successful.
220 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
221 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
222 * the collection is modified after the enumerator is created.
223 * @remarks The specific error code can be accessed using GetLastResult() method.
225 PairIteratorT< K, V >& operator++(void)
227 TryReturnResult(!__isPostEnd, *this, E_OUT_OF_RANGE, "[%s] It already reached the end.", GetErrorMessage(E_OUT_OF_RANGE));
229 result r = __pEnum->MoveNext();
230 TryCatchResult(r == E_SUCCESS, __isPostEnd = true; __currentObj.first = null; __currentObj.second = null,
231 r, "[%s] It already reached the end.", GetErrorMessage(r));
233 __currentObj.first = static_cast< K >(__pEnum->GetKey());
234 __currentObj.second = static_cast< V >(__pEnum->GetValue());
242 * Moves to the next element of the collection and returns the previous state.
246 * @return A %PairIteratorT instance
247 * @exception E_SUCCESS The method is successful.
248 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
249 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
250 * the collection is modified after the enumerator is created.
251 * @remarks The specific error code can be accessed using GetLastResult() method.
253 PairIteratorT< K, V > operator++(int)
255 PairIteratorT< K, V > tempIter = *this;
261 * Checks the two %PairIteratorT instances for equality.
265 * @return @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
267 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
269 bool operator==(const PairIteratorT< K, V >& rhs) const
271 if (__pMap != rhs.__pMap)
276 if (__pMultiMap != rhs.__pMultiMap)
281 if (__index != rhs.__index)
286 if (__isPostEnd != rhs.__isPostEnd)
290 else if (__isPostEnd && rhs.__isPostEnd)
292 // In this case, __currentObj state is invalid
296 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
297 return __currentObj == rhs.__currentObj;
301 * Checks the two %PairIteratorT instances for inequality.
305 * @return @c true if any member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
307 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
309 bool operator!=(const PairIteratorT< K, V >& rhs) const
311 return !operator==(rhs);
315 * Swaps values of the two %PairIteratorT instances.
319 * @param[in] rhs A reference to the %PairIteratorT instance to swap
321 void swap(PairIteratorT< K, V >& rhs)
323 std::swap(__pMap, rhs.__pMap);
324 std::swap(__pMultiMap, rhs.__pMultiMap);
325 std::swap(__isPostEnd, rhs.__isPostEnd);
326 std::swap(__index, rhs.__index);
327 std::swap(__pEnum, rhs.__pEnum);
328 std::swap(__currentObj, rhs.__currentObj);
333 const IMultiMap* __pMultiMap;
336 std::unique_ptr< IMapEnumerator > __pEnum;
337 std::pair< K, V > __currentObj;
340 }}} // Tizen::Base::Collection
342 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_