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.
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.
50 template < typename K, typename V >
52 : public std::iterator< std::input_iterator_tag, std::pair< K, V > >
56 * Initializes this instance of %PairIteratorT class.
60 * @param[in] map A reference to the IMap instance to convert
61 * @param[in] isPostEnd A boolean value to check the end
63 explicit PairIteratorT(const IMap& map, bool isPostEnd = false)
66 , __isPostEnd(isPostEnd)
68 , __pEnum(__pMap->GetMapEnumeratorN())
71 if (__pMap->GetCount() != 0)
76 __currentObj.first = static_cast< K >(__pEnum->GetKey());
77 __currentObj.second = static_cast< V >(__pEnum->GetValue());
81 __index = __pMap->GetCount();
86 // Control reaches here intentionally because begin() should be equal to end()
92 * Initializes this instance of %PairIteratorT class.
96 * @param[in] multiMap A reference to the IMultiMap instance to convert
97 * @param[in] isPostEnd A boolean value to check the end
99 PairIteratorT(const IMultiMap& multiMap, bool isPostEnd = false)
101 , __pMultiMap(&multiMap)
102 , __isPostEnd(isPostEnd)
104 , __pEnum(__pMultiMap->GetMapEnumeratorN())
107 if (__pMultiMap->GetCount() != 0)
112 __currentObj.first = static_cast< K >(__pEnum->GetKey());
113 __currentObj.second = static_cast< V >(__pEnum->GetValue());
117 __index = __pMultiMap->GetCount();
122 // Control reaches here intentionally because begin() should be equal to end()
128 * This is the copy constructor of the %PairIteratorT class.
132 * @param[in] rhs A reference to the %PairIteratorT instance
133 * @exception E_SUCCESS The method is successful.
134 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
135 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
136 * the collection is modified after the enumerator is created.
137 * @remarks The specific error code can be accessed using GetLastResult() method.
139 PairIteratorT(const PairIteratorT< K, V >& rhs)
141 , __pMultiMap(rhs.__pMultiMap)
142 , __isPostEnd(rhs.__isPostEnd)
143 , __index(rhs.__index)
144 , __currentObj(rhs.__currentObj)
146 TryReturnVoidResult(__pMap != null || __pMultiMap != null, E_INVALID_ARG, "[%s] __pMap or __pMultiMap should not be null.", GetErrorMessage(E_INVALID_ARG));
150 __pEnum.reset(__pMap->GetMapEnumeratorN());
152 else if (__pMultiMap != null)
154 __pEnum.reset(__pMultiMap->GetMapEnumeratorN());
159 for (int i = 0; i <= __index; ++i)
167 * This is assignment operator of the %PairIteratorT class.
171 * @return A reference to the %PairIteratorT instance
172 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
173 * @exception E_SUCCESS The method is successful.
174 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are @c null.
175 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
176 * the collection is modified after the enumerator is created.
177 * @remarks The specific error code can be accessed using GetLastResult() method.
179 PairIteratorT< K, V >& operator=(const PairIteratorT< K, V >& rhs)
181 PairIteratorT< K, V > tmp(rhs);
187 * This is the indirection operator for the %PairIteratorT class.
191 * @return A std::pair type reference with K and V type
193 std::pair< K, V >& operator*(void) const
195 AppAssertf(!__isPostEnd, "It is out of range.");
196 return const_cast< std::pair< K, V >& >(__currentObj);
200 * This is the const version structure dereference operator for the %PairIteratorT class.
204 * @return A std::pair type pointer equivalent to the pointer address
206 std::pair< K, V >* operator->(void) const
208 return &(operator*());
212 * Moves to the next element of the collection.
216 * @return A reference to the %PairIteratorT instance
217 * @exception E_SUCCESS The method is successful.
218 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
219 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
220 * the collection is modified after the enumerator is created.
221 * @remarks The specific error code can be accessed using GetLastResult() method.
223 PairIteratorT< K, V >& operator++(void)
225 TryReturnResult(!__isPostEnd, *this, E_OUT_OF_RANGE, "[%s] It already reached the end.", GetErrorMessage(E_OUT_OF_RANGE));
227 result r = __pEnum->MoveNext();
228 TryCatchResult(r == E_SUCCESS, __isPostEnd = true; __currentObj.first = null; __currentObj.second = null,
229 r, "[%s] It already reached the end.", GetErrorMessage(r));
231 __currentObj.first = static_cast< K >(__pEnum->GetKey());
232 __currentObj.second = static_cast< V >(__pEnum->GetValue());
240 * Moves to the next element of the collection and returns the previous state.
244 * @return A %PairIteratorT instance
245 * @exception E_SUCCESS The method is successful.
246 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
247 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
248 * the collection is modified after the enumerator is created.
249 * @remarks The specific error code can be accessed using GetLastResult() method.
251 PairIteratorT< K, V > operator++(int)
253 PairIteratorT< K, V > tempIter = *this;
259 * Checks the two %PairIteratorT instances for equality.
263 * @return @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
265 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
267 bool operator==(const PairIteratorT< K, V >& rhs) const
269 if (__pMap != rhs.__pMap)
274 if (__pMultiMap != rhs.__pMultiMap)
279 if (__index != rhs.__index)
284 if (__isPostEnd != rhs.__isPostEnd)
288 else if (__isPostEnd && rhs.__isPostEnd)
290 // In this case, __currentObj state is invalid
294 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
295 return __currentObj == rhs.__currentObj;
299 * Checks the two %PairIteratorT instances for inequality.
303 * @return @c true if any member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
305 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
307 bool operator!=(const PairIteratorT< K, V >& rhs) const
309 return !operator==(rhs);
313 * Swaps values of the two %PairIteratorT instances.
317 * @param[in] rhs A reference to the %PairIteratorT instance to swap
319 void swap(PairIteratorT< K, V >& rhs)
321 std::swap(__pMap, rhs.__pMap);
322 std::swap(__pMultiMap, rhs.__pMultiMap);
323 std::swap(__isPostEnd, rhs.__isPostEnd);
324 std::swap(__index, rhs.__index);
325 std::swap(__pEnum, rhs.__pEnum);
326 std::swap(__currentObj, rhs.__currentObj);
331 const IMultiMap* __pMultiMap;
334 std::unique_ptr< IMapEnumerator > __pEnum;
335 std::pair< K, V > __currentObj;
338 }}} // Tizen::Base::Collection
340 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_