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 FBaseColPairIteratorT.h
20 * @brief This is the header file for the %PairIteratorT class.
22 * This header file contains the declarations of the %PairIteratorT class.
25 #ifndef _FBASE_COL_PAIR_ITERATOR_T_H_
26 #define _FBASE_COL_PAIR_ITERATOR_T_H_
28 #include <algorithm> // std::swap (Before C++11)
31 #include <unique_ptr.h>
33 #include <FBaseColIMap.h>
34 #include <FBaseColIMultiMap.h>
35 #include <FBaseColIMapEnumerator.h>
37 namespace Tizen { namespace Base { namespace Collection
40 * @class PairIteratorT
41 * @brief This class provides an iterator that is used to convert IMap or IMultiMap to STL containers. @n
42 * StlConverter provides static methods to get this iterator from %IMap or %IMultiMap.
46 * @remarks The %PairIteratorT class satisfies only requirements of C++ standard library InputIterator concept due to limitations of %Tizen Collection.
47 * So, this class can be used with C++ standard library algorithms which requires only InputIterator concept for their arguments.
49 * The %PairIteratorT class provides an iterator that is used to convert IMap or IMultiMap to STL containers.
50 * StlConverter provides static methods to get this iterator from IMap or IMultiMap.
53 template < typename K, typename V >
55 : public std::iterator< std::input_iterator_tag, std::pair< K, V > >
59 * Initializes this instance of %PairIteratorT class.
63 * @param[in] map A reference to the IMap instance to convert
64 * @param[in] isPostEnd A boolean value to check the end
66 explicit PairIteratorT(const IMap& map, bool isPostEnd = false)
69 , __isPostEnd(isPostEnd)
71 , __pEnum(__pMap->GetMapEnumeratorN())
74 if (__pMap->GetCount() != 0)
79 __currentObj.first = static_cast< K >(__pEnum->GetKey());
80 __currentObj.second = static_cast< V >(__pEnum->GetValue());
84 __index = __pMap->GetCount();
89 // Control reaches here intentionally because begin() should be equal to end()
95 * Initializes this instance of %PairIteratorT class.
99 * @param[in] multiMap A reference to the IMultiMap instance to convert
100 * @param[in] isPostEnd A boolean value to check the end
102 PairIteratorT(const IMultiMap& multiMap, bool isPostEnd = false)
104 , __pMultiMap(&multiMap)
105 , __isPostEnd(isPostEnd)
107 , __pEnum(__pMultiMap->GetMapEnumeratorN())
110 if (__pMultiMap->GetCount() != 0)
115 __currentObj.first = static_cast< K >(__pEnum->GetKey());
116 __currentObj.second = static_cast< V >(__pEnum->GetValue());
120 __index = __pMultiMap->GetCount();
125 // Control reaches here intentionally because begin() should be equal to end()
131 * This is the copy constructor of the %PairIteratorT class.
135 * @param[in] rhs A reference to the %PairIteratorT instance
136 * @exception E_SUCCESS The method is successful.
137 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
138 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
139 * the collection is modified after the enumerator is created.
140 * @remarks The specific error code can be accessed using GetLastResult() method.
142 PairIteratorT(const PairIteratorT< K, V >& rhs)
144 , __pMultiMap(rhs.__pMultiMap)
145 , __isPostEnd(rhs.__isPostEnd)
146 , __index(rhs.__index)
147 , __currentObj(rhs.__currentObj)
149 TryReturnVoidResult(__pMap != null || __pMultiMap != null, E_INVALID_ARG, "[%s] __pMap or __pMultiMap should not be null.", GetErrorMessage(E_INVALID_ARG));
153 __pEnum.reset(__pMap->GetMapEnumeratorN());
155 else if (__pMultiMap != null)
157 __pEnum.reset(__pMultiMap->GetMapEnumeratorN());
162 for (int i = 0; i <= __index; ++i)
170 * This is assignment operator of the %PairIteratorT class.
174 * @return A reference to the %PairIteratorT instance
175 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
176 * @exception E_SUCCESS The method is successful.
177 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
178 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
179 * the collection is modified after the enumerator is created.
180 * @remarks The specific error code can be accessed using GetLastResult() method.
182 PairIteratorT< K, V >& operator=(const PairIteratorT< K, V >& rhs)
184 PairIteratorT< K, V > tmp(rhs);
190 * This is the indirection operator for the %PairIteratorT class.
194 * @return A std::pair type reference with K and V type
196 std::pair< K, V >& operator*(void) const
198 AppAssertf(!__isPostEnd, "It is out of range.");
199 return const_cast< std::pair< K, V >& >(__currentObj);
203 * This is the const version structure dereference operator for the %PairIteratorT class.
207 * @return A std::pair type pointer equivalent to the pointer address
209 std::pair< K, V >* operator->(void) const
211 return &(operator*());
215 * Moves to the next element of the collection.
219 * @return A reference to the %PairIteratorT instance
220 * @exception E_SUCCESS The method is successful.
221 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
222 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
223 * the collection is modified after the enumerator is created.
224 * @remarks The specific error code can be accessed using GetLastResult() method.
226 PairIteratorT< K, V >& operator++(void)
228 TryReturnResult(!__isPostEnd, *this, E_OUT_OF_RANGE, "[%s] It already reached the end.", GetErrorMessage(E_OUT_OF_RANGE));
230 result r = __pEnum->MoveNext();
231 TryCatchResult(r == E_SUCCESS, __isPostEnd = true; __currentObj.first = null; __currentObj.second = null,
232 r, "[%s] It already reached the end.", GetErrorMessage(r));
234 __currentObj.first = static_cast< K >(__pEnum->GetKey());
235 __currentObj.second = static_cast< V >(__pEnum->GetValue());
243 * Moves to the next element of the collection and returns the previous state.
247 * @return A %PairIteratorT instance
248 * @exception E_SUCCESS The method is successful.
249 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
250 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
251 * the collection is modified after the enumerator is created.
252 * @remarks The specific error code can be accessed using GetLastResult() method.
254 PairIteratorT< K, V > operator++(int)
256 PairIteratorT< K, V > tempIter = *this;
262 * Checks the two %PairIteratorT instances for equality.
266 * @return @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
268 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
270 bool operator==(const PairIteratorT< K, V >& rhs) const
272 if (__pMap != rhs.__pMap)
277 if (__pMultiMap != rhs.__pMultiMap)
282 if (__index != rhs.__index)
287 if (__isPostEnd != rhs.__isPostEnd)
291 else if (__isPostEnd && rhs.__isPostEnd)
293 // In this case, __currentObj state is invalid
297 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
298 return __currentObj == rhs.__currentObj;
302 * Checks the two %PairIteratorT instances for inequality.
306 * @return @c true if any member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
308 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
310 bool operator!=(const PairIteratorT< K, V >& rhs) const
312 return !operator==(rhs);
316 * Swaps values of the two %PairIteratorT instances.
320 * @param[in] rhs A reference to the %PairIteratorT instance to swap
322 void swap(PairIteratorT< K, V >& rhs)
324 std::swap(__pMap, rhs.__pMap);
325 std::swap(__pMultiMap, rhs.__pMultiMap);
326 std::swap(__isPostEnd, rhs.__isPostEnd);
327 std::swap(__index, rhs.__index);
328 std::swap(__pEnum, rhs.__pEnum);
329 std::swap(__currentObj, rhs.__currentObj);
334 const IMultiMap* __pMultiMap;
337 std::unique_ptr< IMapEnumerator > __pEnum;
338 std::pair< K, V > __currentObj;
341 }}} // Tizen::Base::Collection
343 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_