summaryrefslogtreecommitdiff
path: root/glib/glibmm/vectorutils.cc
blob: b4cac1e30774ef621af1312396f216d424daa66f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright (C) 2011 The glibmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glibmm/vectorutils.h>

namespace Glib
{

namespace Container_Helpers
{

gboolean*
create_bool_array(std::vector<bool>::const_iterator pbegin, std::size_t size)
{
  gboolean* const array(static_cast<gboolean*>(g_malloc((size + 1) * sizeof(gboolean))));
  gboolean* const array_end(array + size);

  for (gboolean* pdest(array); pdest != array_end; ++pdest)
  {
    *pdest = *pbegin;
    ++pbegin;
  }

  *array_end = false;
  return array;
}

} // namespace Container_Helpers

/**** Glib::ArrayHandler<bool> ************************/

ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::VectorType
ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::array_to_vector(
  const CType* array, std::size_t array_size, Glib::OwnershipType ownership)
{
  if (array)
  {
    // it will handle destroying data depending on passed ownership.
    ArrayKeeperType keeper(array, array_size, ownership);
#ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS
    return VectorType(ArrayIteratorType(array), ArrayIteratorType(array + array_size));
#else
    VectorType temp;
    temp.reserve(array_size);
    Glib::Container_Helpers::fill_container(
      temp, ArrayIteratorType(array), ArrayIteratorType(array + array_size));
    return temp;
#endif
  }
  return VectorType();
}

ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::VectorType
ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::array_to_vector(
  const CType* array, Glib::OwnershipType ownership)
{
  return array_to_vector(array, Glib::Container_Helpers::compute_array_size2(array), ownership);
}

ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::ArrayKeeperType
ArrayHandler<bool, Glib::Container_Helpers::TypeTraits<bool>>::vector_to_array(
  const VectorType& vector)
{
  return ArrayKeeperType(Glib::Container_Helpers::create_bool_array(vector.begin(), vector.size()),
    vector.size(), Glib::OWNERSHIP_SHALLOW);
}

} // namespace Glib