1 /* Copyright (C) 2011 The glibmm Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <glibmm/vectorutils.h>
18 
19 namespace Glib
20 {
21 
22 namespace Container_Helpers
23 {
24 
25 gboolean*
create_bool_array(std::vector<bool>::const_iterator pbegin,std::size_t size)26 create_bool_array(std::vector<bool>::const_iterator pbegin, std::size_t size)
27 {
28   gboolean* const array(static_cast<gboolean*>(g_malloc((size + 1) * sizeof(gboolean))));
29   gboolean* const array_end(array + size);
30 
31   for (gboolean* pdest(array); pdest != array_end; ++pdest)
32   {
33     *pdest = *pbegin;
34     ++pbegin;
35   }
36 
37   *array_end = false;
38   return array;
39 }
40 
41 } // namespace Container_Helpers
42 
43 /**** Glib::ArrayHandler<bool> ************************/
44 
45 ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::VectorType
array_to_vector(const CType * array,std::size_t array_size,Glib::OwnershipType ownership)46 ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::array_to_vector(
47   const CType* array, std::size_t array_size, Glib::OwnershipType ownership)
48 {
49   if (array)
50   {
51     // it will handle destroying data depending on passed ownership.
52     ArrayKeeperType keeper(array, array_size, ownership);
53 #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS
54     return VectorType(ArrayIteratorType(array), ArrayIteratorType(array + array_size));
55 #else
56     VectorType temp;
57     temp.reserve(array_size);
58     Glib::Container_Helpers::fill_container(
59       temp, ArrayIteratorType(array), ArrayIteratorType(array + array_size));
60     return temp;
61 #endif
62   }
63   return VectorType();
64 }
65 
66 ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::VectorType
array_to_vector(const CType * array,Glib::OwnershipType ownership)67 ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::array_to_vector(
68   const CType* array, Glib::OwnershipType ownership)
69 {
70   return array_to_vector(array, Glib::Container_Helpers::compute_array_size2(array), ownership);
71 }
72 
73 ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::ArrayKeeperType
vector_to_array(const VectorType & vector)74 ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::vector_to_array(
75   const VectorType& vector)
76 {
77   return ArrayKeeperType(Glib::Container_Helpers::create_bool_array(vector.begin(), vector.size()),
78     vector.size(), Glib::OWNERSHIP_SHALLOW);
79 }
80 
81 } // namespace Glib
82