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 FBaseColStlConverter.h
19 * @brief This is the header file for the %StlConverter class.
21 * This header file contains the declarations of the %StlConverter class.
24 #ifndef _FBASE_COL_STL_CONVERTER_H_
25 #define _FBASE_COL_STL_CONVERTER_H_
28 #include <unique_ptr.h>
29 #include <FBaseColArrayList.h>
30 #include <FBaseColLinkedList.h>
31 #include <FBaseColHashMap.h>
32 #include <FBaseColMultiHashMap.h>
33 #include <FBaseColIteratorT.h>
34 #include <FBaseColPairIteratorT.h>
35 #include <FBaseColRandomIteratorT.h>
36 #include <FBaseColTypes.h>
38 namespace Tizen { namespace Base { namespace Collection
46 * @brief This class provides static methods to convert %Tizen Collection to STL Container and vice versa.
50 * The %StlConverter class provides static methods to convert %Tizen Collection to STL Container and vice versa.
51 * The following example demonstrates how to use the %StlConverter class.
56 * #include <FBaseCol.h>
58 * using namespace std;
59 * using namespace Tizen::Base;
60 * using namespace Tizen::Base::Collection;
63 * MyClass::StlConverterSample
65 * // The case of Collection to STL Container
66 * IList* pList1 = someNativeObject.GetSomeIntegerListN();
68 * // vector can be created through IteratorT
69 * vector< Integer* > vec1(
70 * StlConverter::GetBeginIterator< Integer* >(pList1),
71 * StlConverter::GetEndIterator< Integer* >(pList1));
73 * // vec1 can be used here.
75 * // The case of STL Container to Collection
76 * vector< Integer* > vec2;
78 * vec2.push_back(new Integer(1));
79 * vec2.push_back(new Integer(2));
80 * vec2.push_back(new Integer(3));
82 * unique_ptr< ArrayList > pList2(StlConverter::GetArrayListN(vec2.begin(), vec2.end(), SingleObjectDeleter));
84 * // call SomeNativeAPI(pList2.get());
93 * Gets the STL compatible iterator referring to the first element in the IList instance.
97 * @return An IteratorT instance
98 * @param[in] pList A pointer to the IList instance to convert
100 template < typename T >
101 static IteratorT< T > GetBeginIterator(const IList* pList)
103 return IteratorT< T >(*pList);
107 * Gets the STL compatible iterator referring to the post-end element in the IList instance.
111 * @return An IteratorT instance
112 * @param[in] pList A pointer to the IList instance to convert
114 template < typename T >
115 static IteratorT< T > GetEndIterator(const IList* pList)
117 return IteratorT< T >(*pList, true);
121 * Gets the STL compatible random iterator referring to the first element in the IList instance.
125 * @return A RandomIteratorT instance
126 * @param[in] pList A pointer to the IList instance to convert
128 template < typename T >
129 static RandomIteratorT< T > GetBeginRandomIterator(const IList* pList)
131 return RandomIteratorT< T >(*pList, 0);
135 * Gets the STL compatible random iterator referring to the post-end element in the IList instance.
139 * @return A RandomIteratorT instance
140 * @param[in] pList A pointer to the IList instance to convert
142 template < typename T >
143 static RandomIteratorT< T > GetEndRandomIterator(const IList* pList)
145 return RandomIteratorT< T >(*pList, pList->GetCount());
149 * Gets the STL compatible iterator referring to the first element in the IMap instance. @n
150 * This iterator can be used in algorithms that require paired iterators.
154 * @return A PairIteratorT instance
155 * @param[in] pMap A pointer to the IMap instance to convert
157 template < typename K, typename V >
158 static PairIteratorT< K, V > GetBeginPairIterator(const IMap* pMap)
160 return PairIteratorT< K, V >(*pMap);
164 * Gets the STL compatible iterator referring to the post-end element in the IMap instance. @n
165 * This iterator can be used in algorithms that require paired iterators.
169 * @return A PairIteratorT instance
170 * @param[in] pMap A pointer to the IMap instance to convert
172 template < typename K, typename V >
173 static PairIteratorT< K, V > GetEndPairIterator(const IMap* pMap)
175 return PairIteratorT< K, V >(*pMap, true);
179 * Gets the STL compatible iterator referring to the first element in the IMultiMap instance. @n
180 * This iterator can be used in algorithms that require paired iterators.
184 * @return A PairIteratorT instance
185 * @param[in] pMultiMap A pointer to the IMultiMap instance to convert
187 template < typename K, typename V >
188 static PairIteratorT< K, V > GetBeginPairIterator(const IMultiMap* pMultiMap)
190 return PairIteratorT< K, V >(*pMultiMap);
194 * Gets the STL compatible iterator referring to the post-end element in the IMultiMap instance. @n
195 * This iterator can be used in algorithms that require paired iterators.
199 * @return A PairIteratorT instance
200 * @param[in] pMultiMap A pointer to the IMultiMap instance to convert
202 template < typename K, typename V >
203 static PairIteratorT< K, V > GetEndPairIterator(const IMultiMap* pMultiMap)
205 return PairIteratorT< K, V >(*pMultiMap, true);
209 * Gets an ArrayList instance from the begin and end iterators of STL container.
213 * @return A std::unique_ptr to the ArrayList instance, @n
214 * else @c std::unique_ptr< ArrayList >() if error occurs
215 * @param[in] begin begin() of STL container
216 * @param[in] end end() of STL container
217 * @param[in] deleter The function pointer to type of the element deleter
218 * @exception E_SUCCESS The method is successful.
219 * @exception E_INVALID_ARG A specified input parameter is invalid.
220 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
221 * This gives the collection the ownership of elements and the collection can destroy elements. @n
222 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
223 * as @c NoOpDeleter is the default element deleter.
224 * That implies transfer of the ownership of elements to the collection is not required.
225 * @remarks The specific error code can be accessed using GetLastResult() method.
227 * @see SingleObjectDeleter()
228 * @see ArrayDeleter()
230 template < typename FwdIter >
231 static std::unique_ptr< ArrayList > GetArrayListN(FwdIter begin, FwdIter end, DeleterFunctionType deleter = NoOpDeleter)
233 std::unique_ptr< ArrayList > pArrayList(new (std::nothrow) ArrayList(deleter));
234 TryReturnResult(pArrayList, std::unique_ptr< ArrayList >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
236 result r = pArrayList->Construct();
237 TryReturnResult(r == E_SUCCESS, std::unique_ptr< ArrayList >(), r, "[%s] Propagating.", GetErrorMessage(r));
239 for (FwdIter iter = begin; iter != end; ++iter)
241 r = pArrayList->Add(*iter);
244 pArrayList->RemoveAll(false);
245 TryReturnResult(false, std::unique_ptr< ArrayList >(), r, "[%s] Propagating.", GetErrorMessage(r));
249 return std::move(pArrayList);
253 * Gets a LinkedList instance from the begin and end iterators of STL container.
257 * @return A std::unique_ptr to the LinkedList instance @n
258 * else @c std::unique_ptr< LinkedList >() if error occurs
259 * @param[in] begin begin() of STL container
260 * @param[in] end end() of STL container
261 * @param[in] deleter The function pointer to type of the element deleter
262 * @exception E_SUCCESS The method is successful.
263 * @exception E_INVALID_ARG A specified input parameter is invalid.
264 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
265 * This gives the collection the ownership of elements and the collection will destroy elements. @n
266 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
267 * as @c NoOpDeleter is the default element deleter.
268 * That implies transfer of the ownership of elements to the collection is not required.
269 * @remarks The specific error code can be accessed using GetLastResult() method.
271 * @see SingleObjectDeleter()
272 * @see ArrayDeleter()
274 template < typename FwdIter >
275 static std::unique_ptr< LinkedList > GetLinkedListN(FwdIter begin, FwdIter end, DeleterFunctionType deleter = NoOpDeleter)
277 std::unique_ptr< LinkedList > pLinkedList(new (std::nothrow) LinkedList(deleter));
278 TryReturnResult(pLinkedList, std::unique_ptr< LinkedList >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
280 for (FwdIter iter = begin; iter != end; ++iter)
282 result r = pLinkedList->Add(*iter);
285 pLinkedList->RemoveAll(false);
286 TryReturnResult(false, std::unique_ptr< LinkedList >(), r, "[%s] Propagating.", GetErrorMessage(r));
290 return std::move(pLinkedList);
294 * Gets a HashMap instance from the begin and end iterators of STL container.
298 * @return A std::unique_ptr to the HashMap instance @n
299 * else @c std::unique_ptr< HashMap >() if error occurs
300 * @param[in] begin begin() of STL container
301 * @param[in] end end() of STL container
302 * @param[in] deleter The function pointer to type of the element deleter
303 * @exception E_SUCCESS The method is successful.
304 * @exception E_INVALID_ARG A specified input parameter is invalid.
305 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
306 * This gives the collection the ownership of elements and the collection will destroy elements. @n
307 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
308 * as @c NoOpDeleter is the default element deleter.
309 * That implies transfer of the ownership of elements to the collection is not required.
310 * @remarks The specific error code can be accessed using GetLastResult() method.
312 * @see SingleObjectDeleter()
313 * @see ArrayDeleter()
315 template < typename PairedFwdIter >
316 static std::unique_ptr< HashMap > GetHashMapN(PairedFwdIter begin, PairedFwdIter end, DeleterFunctionType deleter = NoOpDeleter)
318 std::unique_ptr< HashMap > pMap(new (std::nothrow) HashMap(deleter));
319 TryReturnResult(pMap, std::unique_ptr< HashMap >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
321 result r = pMap->Construct();
322 TryReturnResult(r == E_SUCCESS, std::unique_ptr< HashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
324 for (PairedFwdIter pairIter = begin; pairIter != end; ++pairIter)
326 r = pMap->Add(pairIter->first, pairIter->second);
329 pMap->RemoveAll(false);
330 TryReturnResult(false, std::unique_ptr< HashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
334 return std::move(pMap);
338 * Gets a MultiHashMap instance from the begin and end iterators of STL container.
342 * @return A std::unique_ptr to the MultiHashMap instance @n
343 * else @c std::unique_ptr< MultiHashMap >() if error occurs
344 * @param[in] begin begin() of STL container
345 * @param[in] end end() of STL container
346 * @param[in] deleter The function pointer to type of the element deleter
347 * @exception E_SUCCESS The method is successful.
348 * @exception E_INVALID_ARG A specified input parameter is invalid.
349 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
350 * This gives the collection the ownership of elements and the collection will destroy elements. @n
351 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
352 * as @c NoOpDeleter is the default element deleter.
353 * That implies transfer of the ownership of elements to the collection is not required.
354 * @remarks The specific error code can be accessed using GetLastResult() method.
356 * @see SingleObjectDeleter()
357 * @see ArrayDeleter()
359 template < typename PairedFwdIter >
360 static std::unique_ptr< MultiHashMap > GetMultiHashMapN(PairedFwdIter begin, PairedFwdIter end, DeleterFunctionType deleter = NoOpDeleter)
362 std::unique_ptr< MultiHashMap > pMultiMap(new (std::nothrow) MultiHashMap(deleter));
363 TryReturnResult(pMultiMap, std::unique_ptr< MultiHashMap >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
365 result r = pMultiMap->Construct();
366 TryReturnResult(r == E_SUCCESS, std::unique_ptr< MultiHashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
368 for (PairedFwdIter pairIter = begin; pairIter != end; ++pairIter)
370 r = pMultiMap->Add(pairIter->first, pairIter->second);
373 pMultiMap->RemoveAll(false);
374 TryReturnResult(false, std::unique_ptr< MultiHashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
378 return std::move(pMultiMap);
383 // This default constructor is intentionally declared as private because this class is not constructible.
390 // This destructor is intentionally declared as private because this class is not constructible.
394 virtual ~StlConverter(void);
397 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
401 // @param[in] rhs A reference to the %StlConverter instance
403 StlConverter(const StlConverter& rhs);
406 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
410 // @return A reference to the %StlConverter instance
411 // @param[in] rhs A reference to the %StlConverter instance
413 StlConverter& operator=(const StlConverter& rhs);
416 }}} // Tizen::Base::Collection
418 #endif //_FBASE_COL_STL_CONVERTER_H_