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 #include <cstdlib>
17 #include <ctime>
18 
19 #include <iostream>
20 
21 #include <glibmm.h>
22 
23 // Use this line if you want debug output:
24 // std::ostream& ostr = std::cout;
25 
26 // This seems nicer and more useful than putting an ifdef around the use of ostr:
27 std::stringstream debug;
28 std::ostream& ostr = debug;
29 
30 const unsigned int magic_limit(5);
31 
32 void
setup_rand()33 setup_rand()
34 {
35   static bool setup(false);
36 
37   if (!setup)
38   {
39     setup = true;
40     std::srand(std::time(nullptr));
41   }
42 }
43 
44 gboolean*
c_get_bool_array()45 c_get_bool_array()
46 {
47   gboolean* array(static_cast<gboolean*>(g_malloc((magic_limit + 1) * sizeof(gboolean))));
48 
49   setup_rand();
50   for (unsigned int iter(0); iter < magic_limit; ++iter)
51   {
52     array[iter] = std::rand() % 2 ? TRUE : FALSE;
53   }
54   array[magic_limit] = FALSE;
55   return array;
56 }
57 
58 void
c_print_bool_array(gboolean * array)59 c_print_bool_array(gboolean* array)
60 {
61   for (unsigned int iter(0); iter < magic_limit; ++iter)
62   {
63     ostr << iter << ": " << (array[iter] ? "TRUE" : "FALSE") << "\n";
64   }
65 }
66 
67 std::vector<bool>
cxx_get_bool_array()68 cxx_get_bool_array()
69 {
70   return Glib::ArrayHandler<bool>::array_to_vector(
71     c_get_bool_array(), magic_limit, Glib::OWNERSHIP_SHALLOW);
72 }
73 
74 void
cxx_print_bool_array(const std::vector<bool> & v)75 cxx_print_bool_array(const std::vector<bool>& v)
76 {
77   c_print_bool_array(const_cast<gboolean*>(Glib::ArrayHandler<bool>::vector_to_array(v).data()));
78 }
79 
80 int
main(int,char **)81 main(int, char**)
82 {
83   Glib::init();
84 
85   std::vector<bool> va(cxx_get_bool_array());
86 
87   cxx_print_bool_array(va);
88 
89   return EXIT_SUCCESS;
90 }
91