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 FBaseColStlConverter.h
20 * @brief This is the header file for the %StlConverter class.
22 * This header file contains the declarations of the %StlConverter class.
25 #ifndef _FBASE_COL_STL_CONVERTER_H_
26 #define _FBASE_COL_STL_CONVERTER_H_
29 #include <unique_ptr.h>
30 #include <FBaseColArrayList.h>
31 #include <FBaseColLinkedList.h>
32 #include <FBaseColHashMap.h>
33 #include <FBaseColMultiHashMap.h>
34 #include <FBaseColIteratorT.h>
35 #include <FBaseColPairIteratorT.h>
36 #include <FBaseColRandomIteratorT.h>
37 #include <FBaseColTypes.h>
39 namespace Tizen { namespace Base { namespace Collection
47 * @brief This class provides static methods to convert %Tizen Collection to STL Container and vice versa.
51 * The %StlConverter class provides static methods to convert %Tizen Collection to STL Container and vice versa.
52 * The following example demonstrates how to use the %StlConverter class.
57 * #include <FBaseCol.h>
59 * using namespace std;
60 * using namespace Tizen::Base;
61 * using namespace Tizen::Base::Collection;
64 * MyClass::StlConverterSample
66 * // The case of Collection to STL Container
67 * IList* pList1 = someNativeObject.GetSomeIntegerListN();
69 * // vector can be created through IteratorT
70 * vector< Integer* > vec1(
71 * StlConverter::GetBeginIterator< Integer* >(pList1),
72 * StlConverter::GetEndIterator< Integer* >(pList1));
74 * // vec1 can be used here.
76 * // The case of STL Container to Collection
77 * vector< Integer* > vec2;
79 * vec2.push_back(new Integer(1));
80 * vec2.push_back(new Integer(2));
81 * vec2.push_back(new Integer(3));
83 * unique_ptr< ArrayList > pList2(StlConverter::GetArrayListN(vec2.begin(), vec2.end(), SingleObjectDeleter));
85 * // call SomeNativeAPI(pList2.get());
94 * Gets the STL compatible iterator referring to the first element in the IList instance.
98 * @return An IteratorT instance
99 * @param[in] pList A pointer to the IList instance to convert
101 template < typename T >
102 static IteratorT< T > GetBeginIterator(const IList* pList)
104 return IteratorT< T >(*pList);
108 * Gets the STL compatible iterator referring to the post-end element in the IList instance.
112 * @return An IteratorT instance
113 * @param[in] pList A pointer to the IList instance to convert
115 template < typename T >
116 static IteratorT< T > GetEndIterator(const IList* pList)
118 return IteratorT< T >(*pList, true);
122 * Gets the STL compatible random iterator referring to the first element in the IList instance.
126 * @return A RandomIteratorT instance
127 * @param[in] pList A pointer to the IList instance to convert
129 template < typename T >
130 static RandomIteratorT< T > GetBeginRandomIterator(const IList* pList)
132 return RandomIteratorT< T >(*pList, 0);
136 * Gets the STL compatible random iterator referring to the post-end element in the IList instance.
140 * @return A RandomIteratorT instance
141 * @param[in] pList A pointer to the IList instance to convert
143 template < typename T >
144 static RandomIteratorT< T > GetEndRandomIterator(const IList* pList)
146 return RandomIteratorT< T >(*pList, pList->GetCount());
150 * Gets the STL compatible iterator referring to the first element in the IMap instance. @n
151 * This iterator can be used in algorithms that require paired iterators.
155 * @return A PairIteratorT instance
156 * @param[in] pMap A pointer to the IMap instance to convert
158 template < typename K, typename V >
159 static PairIteratorT< K, V > GetBeginPairIterator(const IMap* pMap)
161 return PairIteratorT< K, V >(*pMap);
165 * Gets the STL compatible iterator referring to the post-end element in the IMap instance. @n
166 * This iterator can be used in algorithms that require paired iterators.
170 * @return A PairIteratorT instance
171 * @param[in] pMap A pointer to the IMap instance to convert
173 template < typename K, typename V >
174 static PairIteratorT< K, V > GetEndPairIterator(const IMap* pMap)
176 return PairIteratorT< K, V >(*pMap, true);
180 * Gets the STL compatible iterator referring to the first element in the IMultiMap instance. @n
181 * This iterator can be used in algorithms that require paired iterators.
185 * @return A PairIteratorT instance
186 * @param[in] pMultiMap A pointer to the IMultiMap instance to convert
188 template < typename K, typename V >
189 static PairIteratorT< K, V > GetBeginPairIterator(const IMultiMap* pMultiMap)
191 return PairIteratorT< K, V >(*pMultiMap);
195 * Gets the STL compatible iterator referring to the post-end element in the IMultiMap instance. @n
196 * This iterator can be used in algorithms that require paired iterators.
200 * @return A PairIteratorT instance
201 * @param[in] pMultiMap A pointer to the IMultiMap instance to convert
203 template < typename K, typename V >
204 static PairIteratorT< K, V > GetEndPairIterator(const IMultiMap* pMultiMap)
206 return PairIteratorT< K, V >(*pMultiMap, true);
210 * Gets an ArrayList instance from the begin and end iterators of STL container.
214 * @return A std::unique_ptr to the ArrayList instance, @n
215 * else @c std::unique_ptr< ArrayList >() if error occurs
216 * @param[in] begin begin() of STL container
217 * @param[in] end end() of STL container
218 * @param[in] deleter The function pointer to type of the element deleter
219 * @exception E_SUCCESS The method is successful.
220 * @exception E_INVALID_ARG A specified input parameter is invalid.
221 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
222 * This gives the collection the ownership of elements and the collection can destroy elements. @n
223 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
224 * as @c NoOpDeleter is the default element deleter.
225 * That implies transfer of the ownership of elements to the collection is not required.
226 * @remarks The specific error code can be accessed using GetLastResult() method.
228 * @see SingleObjectDeleter()
229 * @see ArrayDeleter()
231 template < typename FwdIter >
232 static std::unique_ptr< ArrayList > GetArrayListN(FwdIter begin, FwdIter end, DeleterFunctionType deleter = NoOpDeleter)
234 std::unique_ptr< ArrayList > pArrayList(new (std::nothrow) ArrayList(deleter));
235 TryReturnResult(pArrayList, std::unique_ptr< ArrayList >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
237 result r = pArrayList->Construct();
238 TryReturnResult(r == E_SUCCESS, std::unique_ptr< ArrayList >(), r, "[%s] Propagating.", GetErrorMessage(r));
240 for (FwdIter iter = begin; iter != end; ++iter)
242 r = pArrayList->Add(*iter);
245 pArrayList->RemoveAll(false);
246 TryReturnResult(false, std::unique_ptr< ArrayList >(), r, "[%s] Propagating.", GetErrorMessage(r));
250 return std::move(pArrayList);
254 * Gets a LinkedList instance from the begin and end iterators of STL container.
258 * @return A std::unique_ptr to the LinkedList instance @n
259 * else @c std::unique_ptr< LinkedList >() if error occurs
260 * @param[in] begin begin() of STL container
261 * @param[in] end end() of STL container
262 * @param[in] deleter The function pointer to type of the element deleter
263 * @exception E_SUCCESS The method is successful.
264 * @exception E_INVALID_ARG A specified input parameter is invalid.
265 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
266 * This gives the collection the ownership of elements and the collection will destroy elements. @n
267 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
268 * as @c NoOpDeleter is the default element deleter.
269 * That implies transfer of the ownership of elements to the collection is not required.
270 * @remarks The specific error code can be accessed using GetLastResult() method.
272 * @see SingleObjectDeleter()
273 * @see ArrayDeleter()
275 template < typename FwdIter >
276 static std::unique_ptr< LinkedList > GetLinkedListN(FwdIter begin, FwdIter end, DeleterFunctionType deleter = NoOpDeleter)
278 std::unique_ptr< LinkedList > pLinkedList(new (std::nothrow) LinkedList(deleter));
279 TryReturnResult(pLinkedList, std::unique_ptr< LinkedList >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
281 for (FwdIter iter = begin; iter != end; ++iter)
283 result r = pLinkedList->Add(*iter);
286 pLinkedList->RemoveAll(false);
287 TryReturnResult(false, std::unique_ptr< LinkedList >(), r, "[%s] Propagating.", GetErrorMessage(r));
291 return std::move(pLinkedList);
295 * Gets a HashMap instance from the begin and end iterators of STL container.
299 * @return A std::unique_ptr to the HashMap instance @n
300 * else @c std::unique_ptr< HashMap >() if error occurs
301 * @param[in] begin begin() of STL container
302 * @param[in] end end() of STL container
303 * @param[in] deleter The function pointer to type of the element deleter
304 * @exception E_SUCCESS The method is successful.
305 * @exception E_INVALID_ARG A specified input parameter is invalid.
306 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
307 * This gives the collection the ownership of elements and the collection will destroy elements. @n
308 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
309 * as @c NoOpDeleter is the default element deleter.
310 * That implies transfer of the ownership of elements to the collection is not required.
311 * @remarks The specific error code can be accessed using GetLastResult() method.
313 * @see SingleObjectDeleter()
314 * @see ArrayDeleter()
316 template < typename PairedFwdIter >
317 static std::unique_ptr< HashMap > GetHashMapN(PairedFwdIter begin, PairedFwdIter end, DeleterFunctionType deleter = NoOpDeleter)
319 std::unique_ptr< HashMap > pMap(new (std::nothrow) HashMap(deleter));
320 TryReturnResult(pMap, std::unique_ptr< HashMap >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
322 result r = pMap->Construct();
323 TryReturnResult(r == E_SUCCESS, std::unique_ptr< HashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
325 for (PairedFwdIter pairIter = begin; pairIter != end; ++pairIter)
327 r = pMap->Add(pairIter->first, pairIter->second);
330 pMap->RemoveAll(false);
331 TryReturnResult(false, std::unique_ptr< HashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
335 return std::move(pMap);
339 * Gets a MultiHashMap instance from the begin and end iterators of STL container.
343 * @return A std::unique_ptr to the MultiHashMap instance @n
344 * else @c std::unique_ptr< MultiHashMap >() if error occurs
345 * @param[in] begin begin() of STL container
346 * @param[in] end end() of STL container
347 * @param[in] deleter The function pointer to type of the element deleter
348 * @exception E_SUCCESS The method is successful.
349 * @exception E_INVALID_ARG A specified input parameter is invalid.
350 * @remarks To create an owning collection, set the element deleter value as @c SingleObjectDeleter.
351 * This gives the collection the ownership of elements and the collection will destroy elements. @n
352 * On the other hand, to create a non-owning collection, you don't need to set the element deleter value,
353 * as @c NoOpDeleter is the default element deleter.
354 * That implies transfer of the ownership of elements to the collection is not required.
355 * @remarks The specific error code can be accessed using GetLastResult() method.
357 * @see SingleObjectDeleter()
358 * @see ArrayDeleter()
360 template < typename PairedFwdIter >
361 static std::unique_ptr< MultiHashMap > GetMultiHashMapN(PairedFwdIter begin, PairedFwdIter end, DeleterFunctionType deleter = NoOpDeleter)
363 std::unique_ptr< MultiHashMap > pMultiMap(new (std::nothrow) MultiHashMap(deleter));
364 TryReturnResult(pMultiMap, std::unique_ptr< MultiHashMap >(), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
366 result r = pMultiMap->Construct();
367 TryReturnResult(r == E_SUCCESS, std::unique_ptr< MultiHashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
369 for (PairedFwdIter pairIter = begin; pairIter != end; ++pairIter)
371 r = pMultiMap->Add(pairIter->first, pairIter->second);
374 pMultiMap->RemoveAll(false);
375 TryReturnResult(false, std::unique_ptr< MultiHashMap >(), r, "[%s] Propagating.", GetErrorMessage(r));
379 return std::move(pMultiMap);
384 // This default constructor is intentionally declared as private because this class is not constructible.
391 // This destructor is intentionally declared as private because this class is not constructible.
395 virtual ~StlConverter(void);
398 // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
402 // @param[in] rhs A reference to the %StlConverter instance
404 StlConverter(const StlConverter& rhs);
407 // The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
411 // @return A reference to the %StlConverter instance
412 // @param[in] rhs A reference to the %StlConverter instance
414 StlConverter& operator=(const StlConverter& rhs);
417 }}} // Tizen::Base::Collection
419 #endif //_FBASE_COL_STL_CONVERTER_H_