037fa3e1169e221a4950e69767050689e8ca3e72
[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;
231                         __currentObj.first = null;
232                         __currentObj.second = null,
233                         r, "[%s] It already reached the end.", GetErrorMessage(r));
234
235                 __currentObj.first = static_cast< K >(__pEnum->GetKey());
236                 __currentObj.second = static_cast< V >(__pEnum->GetValue());
237
238 CATCH:
239                 ++__index;
240                 return *this;
241         }
242
243         /**
244          * Moves to the next element of the collection and returns the previous state.
245          *
246          * @since       2.1
247          *
248          * @return              A %PairIteratorT instance
249          * @exception   E_SUCCESS                       The method is successful.
250          * @exception   E_OUT_OF_RANGE          The enumerator has passed the end of the collection.
251          * @exception   E_INVALID_OPERATION     The current state of the instance prohibits the execution of the specified operation, or
252          *                                                                      the collection is modified after the enumerator is created.
253          * @remarks             The specific error code can be accessed using GetLastResult() method.
254          */
255         PairIteratorT< K, V > operator ++(int)
256         {
257                 PairIteratorT< K, V > tempIter = *this;
258                 operator ++();
259                 return tempIter;
260         }
261
262         /**
263          *      Checks the two %PairIteratorT instances for equality.
264          *
265          *      @since          2.1
266          *
267          *      @return         @c true if every member of the specified %PairIteratorT instance equals the calling instance's members, @n
268          *                              else @c false
269          *      @param[in]      rhs     A reference to the %PairIteratorT instance on the right-hand side of the operator
270          */
271         bool operator ==(const PairIteratorT< K, V >& rhs) const
272         {
273                 if (__pMap != rhs.__pMap)
274                 {
275                         return false;
276                 }
277
278                 if (__pMultiMap != rhs.__pMultiMap)
279                 {
280                         return false;
281                 }
282
283                 if (__index != rhs.__index)
284                 {
285                         return false;
286                 }
287
288                 if (__isPostEnd != rhs.__isPostEnd)
289                 {
290                         return false;
291                 }
292                 else if (__isPostEnd && rhs.__isPostEnd)
293                 {
294                         // In this case, __currentObj state is invalid
295                         return true;
296                 }
297
298                 // If both this->__isPostEnd and rhs.__isPostEnd are false, then reach here. This means both iterators are in the middle of the list.
299                 return __currentObj == rhs.__currentObj;
300         }
301
302         /**
303          *      Checks the two %PairIteratorT instances for inequality.
304          *
305          *      @since          2.1
306          *
307          *      @return         @c true if any member of the specified %PairIteratorT instance is not equal to the calling instance's members, @n
308          *                              else @c false
309          *      @param[in]      rhs     A reference to the %PairIteratorT instance on the right-hand side of the operator
310          */
311         bool operator !=(const PairIteratorT< K, V >& rhs) const
312         {
313                 return !operator ==(rhs);
314         }
315
316         /**
317          *      Swaps values of the two %PairIteratorT instances.
318          *
319          *      @since          2.1
320          *
321          *      @param[in]      rhs     A reference to the %PairIteratorT instance to swap
322          */
323         void swap(PairIteratorT< K, V >& rhs)
324         {
325                 std::swap(__pMap, rhs.__pMap);
326                 std::swap(__pMultiMap, rhs.__pMultiMap);
327                 std::swap(__isPostEnd, rhs.__isPostEnd);
328                 std::swap(__index, rhs.__index);
329                 std::swap(__pEnum, rhs.__pEnum);
330                 std::swap(__currentObj, rhs.__currentObj);
331         }
332
333 private:
334         const IMap* __pMap;
335         const IMultiMap* __pMultiMap;
336         bool __isPostEnd;
337         int __index;
338         std::unique_ptr< IMapEnumerator > __pEnum;
339         std::pair< K, V > __currentObj;
340 };  // PairIteratorT
341
342 }}} // Tizen::Base::Collection
343
344 #endif //_FBASE_COL_PAIR_ITERATOR_T_H_