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 the C++ standard library InputIterator concept due to the limitations of the %Tizen Collection. @n
46 * This class can be used with the C++ standard library algorithms which require only the 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 the %PairIteratorT class.
62 * @param[in] map A reference to the IMap instance to convert
63 * @param[in] isPostEnd The boolean value to check the end of the @c map
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 the %PairIteratorT class.
98 * @param[in] multiMap A reference to the IMultiMap instance to convert
99 * @param[in] isPostEnd The boolean value to check the end of the @c multiMap
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 Either of the following conditions has occurred:
138 * - The current state of the instance prohibits the execution of the specified operation.
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 the 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 Either of the following conditions has occurred:
179 * - The current state of the instance prohibits the execution of the specified operation.
180 * - The collection is modified after the enumerator is created.
181 * @remarks The specific error code can be accessed using GetLastResult() method.
183 PairIteratorT< K, V >& operator =(const PairIteratorT< K, V >& rhs)
185 PairIteratorT< K, V > tmp(rhs);
191 * This is the indirection operator for the %PairIteratorT class.
195 * @return A std::pair type reference with K and V types
197 std::pair< K, V >& operator *(void) const
199 AppAssertf(!__isPostEnd, "It is out of range.");
200 return const_cast< std::pair< K, V >& >(__currentObj);
204 * This is the constant version structure dereference operator for the %PairIteratorT class.
208 * @return A std::pair type pointer equivalent to the pointer address
210 std::pair< K, V >* operator ->(void) const
212 return &(operator *());
216 * Moves to the next element of the collection.
220 * @return A reference to the %PairIteratorT instance
221 * @exception E_SUCCESS The method is successful.
222 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
223 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
224 * - The current state of the instance prohibits the execution of the specified operation.
225 * - The collection is modified after the enumerator is created.
226 * @remarks The specific error code can be accessed using GetLastResult() method.
228 PairIteratorT< K, V >& operator ++(void)
230 TryReturnResult(!__isPostEnd, *this, E_OUT_OF_RANGE, "[%s] It already reached the end.", GetErrorMessage(E_OUT_OF_RANGE));
232 result r = __pEnum->MoveNext();
233 TryCatchResult(r == E_SUCCESS, __isPostEnd = true;
234 __currentObj.first = null;
235 __currentObj.second = null,
236 r, "[%s] It already reached the end.", GetErrorMessage(r));
238 __currentObj.first = static_cast< K >(__pEnum->GetKey());
239 __currentObj.second = static_cast< V >(__pEnum->GetValue());
247 * Moves to the next element of the collection and returns the previous state.
251 * @return A %PairIteratorT instance
252 * @exception E_SUCCESS The method is successful.
253 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the collection.
254 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
255 * - The current state of the instance prohibits the execution of the specified operation.
256 * - The collection is modified after the enumerator is created.
257 * @remarks The specific error code can be accessed using GetLastResult() method.
259 PairIteratorT< K, V > operator ++(int)
261 PairIteratorT< K, V > tempIter = *this;
267 * Checks the two %PairIteratorT instances for equality.
271 * @return @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
273 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
275 bool operator ==(const PairIteratorT< K, V >& rhs) const
277 if (__pMap != rhs.__pMap)
282 if (__pMultiMap != rhs.__pMultiMap)
287 if (__index != rhs.__index)
292 if (__isPostEnd != rhs.__isPostEnd)
296 else if (__isPostEnd && rhs.__isPostEnd)
298 // In this case, __currentObj state is invalid
302 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
303 return __currentObj == rhs.__currentObj;
307 * Checks the two %PairIteratorT instances for inequality.
311 * @return @c true if any member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
313 * @param[in] rhs A reference to the %PairIteratorT instance on the right-hand side of the operator
315 bool operator !=(const PairIteratorT< K, V >& rhs) const
317 return !operator ==(rhs);
321 * Swaps values of the two %PairIteratorT instances.
325 * @param[in] rhs A reference to the %PairIteratorT instance to swap
327 void swap(PairIteratorT< K, V >& rhs)
329 std::swap(__pMap, rhs.__pMap);
330 std::swap(__pMultiMap, rhs.__pMultiMap);
331 std::swap(__isPostEnd, rhs.__isPostEnd);
332 std::swap(__index, rhs.__index);
333 std::swap(__pEnum, rhs.__pEnum);
334 std::swap(__currentObj, rhs.__currentObj);
339 const IMultiMap* __pMultiMap;
342 std::unique_ptr< IMapEnumerator > __pEnum;
343 std::pair< K, V > __currentObj;
346 }}} // Tizen::Base::Collection
348 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_