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 This 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())
74 __currentObj.first = static_cast< K >(__pEnum->GetKey());
75 __currentObj.second = static_cast< V >(__pEnum->GetValue());
79 __index = __pMap->GetCount();
84 * Initializes this instance of %PairIteratorT class.
88 * @param[in] multiMap A reference to the IMultiMap instance to convert
89 * @param[in] isPostEnd A boolean value to check the end
91 PairIteratorT(const IMultiMap& multiMap, bool isPostEnd = false)
93 , __pMultiMap(&multiMap)
94 , __isPostEnd(isPostEnd)
96 , __pEnum(__pMultiMap->GetMapEnumeratorN())
102 __currentObj.first = static_cast< K >(__pEnum->GetKey());
103 __currentObj.second = static_cast< V >(__pEnum->GetValue());
107 __index = __pMultiMap->GetCount();
112 * This is copy constructor of %PairIteratorT class.
116 * @param[in] rhs A reference to the %PairIteratorT instance
117 * @exception E_SUCCESS The method is successful.
118 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are null.
119 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
120 * the collection is modified after the enumerator is created.
121 * @remarks The specific error code can be accessed using GetLastResult() method.
123 PairIteratorT(const PairIteratorT< K, V >& rhs)
125 , __pMultiMap(rhs.__pMultiMap)
126 , __isPostEnd(rhs.__isPostEnd)
127 , __index(rhs.__index)
128 , __currentObj(rhs.__currentObj)
130 TryReturnVoidResult(__pMap != null || __pMultiMap != null, E_INVALID_ARG, "[%s] __pMap or __pMultiMap should not be null.", GetErrorMessage(E_INVALID_ARG));
134 __pEnum.reset(__pMap->GetMapEnumeratorN());
136 else if (__pMultiMap != null)
138 __pEnum.reset(__pMultiMap->GetMapEnumeratorN());
143 for (int i = 0; i <= __index; ++i)
151 * This is assignment operator of %PairIteratorT class.
155 * @return A reference to the %PairIteratorT instance
156 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
157 * @exception E_SUCCESS The method is successful.
158 * @exception E_INVALID_ARG Both @c __pMap and @c __pMultiMap are null.
159 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation or
160 * the collection is modified after the enumerator is created.
161 * @remarks The specific error code can be accessed using GetLastResult() method.
163 PairIteratorT< K, V >& operator=(const PairIteratorT< K, V >& rhs)
165 PairIteratorT< K, V > tmp(rhs);
171 * This is the indirection operator for %PairIteratorT class.
175 * @return A std::pair type reference with K and V type
177 std::pair< K, V >& operator*(void) const
179 AppAssertf(!__isPostEnd, "It is out of range.");
180 return const_cast< std::pair< K, V >& >(__currentObj);
184 * This is the const version structure dereference operator for %PairIteratorT class.
188 * @return A std::pair type pointer equivalent to the pointer address
190 std::pair< K, V >* operator->(void) const
192 return &(operator*());
196 * Moves to the next element of the collection.
200 * @return A reference to the %PairIteratorT instance
201 * @exception E_SUCCESS The method is successful.
202 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
203 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation or
204 * the collection is modified after the enumerator is created.
205 * @remarks The specific error code can be accessed using GetLastResult() method.
207 PairIteratorT< K, V >& operator++(void)
209 TryReturnResult(!__isPostEnd, *this, E_OUT_OF_RANGE, "[%s] It already reached the end.", GetErrorMessage(E_OUT_OF_RANGE));
211 result r = __pEnum->MoveNext();
212 TryCatchResult(r == E_SUCCESS, __isPostEnd = true; __currentObj.first = null; __currentObj.second = null,
213 r, "[%s] It already reached the end.", GetErrorMessage(r));
215 __currentObj.first = static_cast< K >(__pEnum->GetKey());
216 __currentObj.second = static_cast< V >(__pEnum->GetValue());
224 * Moves to the next element of the collection and returns the previous state.
228 * @return A %PairIteratorT instance
229 * @exception E_SUCCESS The method is successful.
230 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
231 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation or
232 * the collection is modified after the enumerator is created.
233 * @remarks The specific error code can be accessed using GetLastResult() method.
235 PairIteratorT< K, V > operator++(int)
237 PairIteratorT< K, V > tempIter = *this;
243 * Checks the two %PairIteratorT instances for equality
247 * @return @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
249 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
251 bool operator==(const PairIteratorT< K, V >& rhs) const
253 if (__pMap != rhs.__pMap)
258 if (__pMultiMap != rhs.__pMultiMap)
263 if (__index != rhs.__index)
268 if (__isPostEnd != rhs.__isPostEnd)
272 else if (__isPostEnd && rhs.__isPostEnd)
274 // In this case, __currentObj state is invalid
278 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
279 return __currentObj == rhs.__currentObj;
283 * Checks the two %PairIteratorT instances for inequality.
287 * @return @c true if every member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
289 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
291 bool operator!=(const PairIteratorT< K, V >& rhs) const
293 return !operator==(rhs);
297 * Exchanges values of the two %PairIteratorT instances
301 * @param[in] rhs A reference to the %PairIteratorT instance to swap
303 void swap(PairIteratorT< K, V >& rhs)
305 std::swap(__pMap, rhs.__pMap);
306 std::swap(__pMultiMap, rhs.__pMultiMap);
307 std::swap(__isPostEnd, rhs.__isPostEnd);
308 std::swap(__index, rhs.__index);
309 std::swap(__pEnum, rhs.__pEnum);
310 std::swap(__currentObj, rhs.__currentObj);
315 const IMultiMap* __pMultiMap;
318 std::unique_ptr< IMapEnumerator > __pEnum;
319 std::pair< K, V > __currentObj;
322 }}} // Tizen::Base::Collection
324 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_