3d7164655c9fabdcaacf351a5d4d7398997529af
[platform/framework/native/appfw.git] / inc / FBaseColPairIteratorT.h
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FBaseColPairIteratorT.h
19  * @brief               This is the header file for the %PairIteratorT class.
20  *
21  * This header file contains the declarations of the %PairIteratorT class.
22  */
23
24 #ifndef _FBASE_COL_PAIR_ITERATOR_T_H_
25 #define _FBASE_COL_PAIR_ITERATOR_T_H_
26
27 #include <algorithm>    // std::swap (Before C++11)
28 #include <iterator>
29 #include <utility>
30 #include <unique_ptr.h>
31 #include <FBaseLog.h>
32 #include <FBaseColIMap.h>
33 #include <FBaseColIMultiMap.h>
34 #include <FBaseColIMapEnumerator.h>
35
36 namespace Tizen { namespace Base { namespace Collection
37 {
38 /**
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.
42  *
43  * @since       2.1
44  *
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.
47  *
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.
50  */
51
52 template < typename K, typename V >
53 class PairIteratorT
54         : public std::iterator< std::input_iterator_tag, std::pair< K, V > >
55 {
56 public:
57         /**
58          * Initializes this instance of %PairIteratorT class.
59          *
60          * @since               2.1
61          *
62          * @param[in]   map                     A reference to the IMap instance to convert
63          * @param[in]   isPostEnd       A boolean value to check the end
64          */
65         explicit PairIteratorT(const IMap& map, bool isPostEnd = false)
66                 : __pMap(&map)
67                 , __pMultiMap(null)
68                 , __isPostEnd(isPostEnd)
69                 , __index(0)
70                 , __pEnum(__pMap->GetMapEnumeratorN())
71                 , __currentObj()
72         {
73                 if (__pMap->GetCount() != 0)
74                 {
75                         if (!__isPostEnd)
76                         {
77                                 __pEnum->MoveNext();
78                                 __currentObj.first = static_cast< K >(__pEnum->GetKey());
79                                 __currentObj.second = static_cast< V >(__pEnum->GetValue());
80                         }
81                         else
82                         {
83                                 __index = __pMap->GetCount();
84                         }
85                 }
86                 else
87                 {
88                         // Control reaches here intentionally because begin() should be equal to end()
89                         __isPostEnd = true;
90                 }
91         }
92
93         /**
94          * Initializes this instance of %PairIteratorT class.
95          *
96          * @since               2.1
97          *
98          * @param[in]   multiMap        A reference to the IMultiMap instance to convert
99          * @param[in]   isPostEnd       A boolean value to check the end
100          */
101         PairIteratorT(const IMultiMap& multiMap, bool isPostEnd = false)
102                 : __pMap(null)
103                 , __pMultiMap(&multiMap)
104                 , __isPostEnd(isPostEnd)
105                 , __index(0)
106                 , __pEnum(__pMultiMap->GetMapEnumeratorN())
107                 , __currentObj()
108         {
109                 if (__pMultiMap->GetCount() != 0)
110                 {
111                         if (!__isPostEnd)
112                         {
113                                 __pEnum->MoveNext();
114                                 __currentObj.first = static_cast< K >(__pEnum->GetKey());
115                                 __currentObj.second = static_cast< V >(__pEnum->GetValue());
116                         }
117                         else
118                         {
119                                 __index = __pMultiMap->GetCount();
120                         }
121                 }
122                 else
123                 {
124                         // Control reaches here intentionally because begin() should be equal to end()
125                         __isPostEnd = true;
126                 }
127         }
128
129         /**
130          * This is the copy constructor of the %PairIteratorT class.
131          *
132          * @since               2.1
133          *
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.
140          */
141         PairIteratorT(const PairIteratorT< K, V >& rhs)
142                 : __pMap(rhs.__pMap)
143                 , __pMultiMap(rhs.__pMultiMap)
144                 , __isPostEnd(rhs.__isPostEnd)
145                 , __index(rhs.__index)
146                 , __currentObj(rhs.__currentObj)
147         {
148                 TryReturnVoidResult(__pMap != null || __pMultiMap != null, E_INVALID_ARG, "[%s] __pMap or __pMultiMap should not be null.", GetErrorMessage(E_INVALID_ARG));
149
150                 if (__pMap != null)
151                 {
152                         __pEnum.reset(__pMap->GetMapEnumeratorN());
153                 }
154                 else if (__pMultiMap != null)
155                 {
156                         __pEnum.reset(__pMultiMap->GetMapEnumeratorN());
157                 }
158
159                 if (!__isPostEnd)
160                 {
161                         for (int i = 0; i <= __index; ++i)
162                         {
163                                 __pEnum->MoveNext();
164                         }
165                 }
166         }
167
168         /**
169          * This is assignment operator of the %PairIteratorT class.
170          *
171          * @since               2.1
172          *
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.
180          */
181         PairIteratorT< K, V >& operator=(const PairIteratorT< K, V >& rhs)
182         {
183                 PairIteratorT< K, V > tmp(rhs);
184                 tmp.swap(*this);
185                 return *this;
186         }
187
188         /**
189          * This is the indirection operator for the %PairIteratorT class.
190          *
191          * @since               2.1
192          *
193          * @return              A std::pair type reference with K and V type
194          */
195         std::pair< K, V >& operator*(void) const
196         {
197                 AppAssertf(!__isPostEnd, "It is out of range.");
198                 return const_cast< std::pair< K, V >& >(__currentObj);
199         }
200
201         /**
202          * This is the const version structure dereference operator for the %PairIteratorT class.
203          *
204          * @since               2.1
205          *
206          * @return              A std::pair type pointer equivalent to the pointer address
207          */
208         std::pair< K, V >* operator->(void) const
209         {
210                 return &(operator*());
211         }
212
213         /**
214          * Moves to the next element of the collection.
215          *
216          * @since               2.1
217          *
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.
224          */
225         PairIteratorT< K, V >& operator++(void)
226         {
227                 TryReturnResult(!__isPostEnd, *this, E_OUT_OF_RANGE, "[%s] It already reached the end.", GetErrorMessage(E_OUT_OF_RANGE));
228
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));
232
233                 __currentObj.first = static_cast< K >(__pEnum->GetKey());
234                 __currentObj.second = static_cast< V >(__pEnum->GetValue());
235
236 CATCH:
237                 ++__index;
238                 return *this;
239         }
240
241         /**
242          * Moves to the next element of the collection and returns the previous state.
243          *
244          * @since       2.1
245          *
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.
252          */
253         PairIteratorT< K, V > operator++(int)
254         {
255                 PairIteratorT< K, V > tempIter = *this;
256                 operator++();
257                 return tempIter;
258         }
259
260         /**
261          *      Checks the two %PairIteratorT instances for equality.
262          *
263          *      @since          2.1
264          *
265          *      @return         @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
266          *                              else @c false
267          *      @param[in]      rhs     A reference to the %PairIteratorT instance on the right-hand side of the operator
268          */
269         bool operator==(const PairIteratorT< K, V >& rhs) const
270         {
271                 if (__pMap != rhs.__pMap)
272                 {
273                         return false;
274                 }
275
276                 if (__pMultiMap != rhs.__pMultiMap)
277                 {
278                         return false;
279                 }
280
281                 if (__index != rhs.__index)
282                 {
283                         return false;
284                 }
285
286                 if (__isPostEnd != rhs.__isPostEnd)
287                 {
288                         return false;
289                 }
290                 else if (__isPostEnd && rhs.__isPostEnd)
291                 {
292                         // In this case, __currentObj state is invalid
293                         return true;
294                 }
295
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;
298         }
299
300         /**
301          *      Checks the two %PairIteratorT instances for inequality.
302          *
303          *      @since          2.1
304          *
305          *      @return         @c true if any member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
306          *                              else @c false
307          *      @param[in]      rhs     A reference to the %PairIteratorT instance on the right-hand side of the operator
308          */
309         bool operator!=(const PairIteratorT< K, V >& rhs) const
310         {
311                 return !operator==(rhs);
312         }
313
314         /**
315          *      Swaps values of the two %PairIteratorT instances.
316          *
317          *      @since          2.1
318          *
319          *      @param[in]      rhs     A reference to the %PairIteratorT instance to swap
320          */
321         void swap(PairIteratorT< K, V >& rhs)
322         {
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);
329         }
330
331 private:
332         const IMap* __pMap;
333         const IMultiMap* __pMultiMap;
334         bool __isPostEnd;
335         int __index;
336         std::unique_ptr< IMapEnumerator > __pEnum;
337         std::pair< K, V > __currentObj;
338 };      // PairIteratorT
339
340 }}} // Tizen::Base::Collection
341
342 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_