summaryrefslogtreecommitdiff
path: root/tests/glibmm_valuearray/main.cc
blob: ca1114320c801186e00bda8d07015fa62ec6b803 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Glib::ValueArray is deprecated, but let's keep the test.
// The recommended replacement is std::vector<> which requires no test here.
#undef GLIBMM_DISABLE_DEPRECATED

#include <glibmm.h>
#include <iostream>

#ifdef GLIBMM_DISABLE_DEPRECATED
int main(int, char**)
{
  // If glibmm is configured with --disable-deprecated-api, GLIBMM_DISABLE_DEPRECATED
  // is defined in glibmm.h (actually in glibmmconfig.h). The undef at the start of
  // this file has no effect.
  return 77; // Tell automake's test harness to skip this test.
}

#else

//Use this line if you want debug output:
//std::ostream& ostr = std::cout;

//This seems nicer and more useful than putting an ifdef around the use of ostr:
std::stringstream debug;
std::ostream& ostr = debug;

int on_compare(const Glib::ValueBase& v1, const Glib::ValueBase& v2)
{
  const Glib::Value<int>& intVal1 = static_cast< const Glib::Value<int>& >(v1);
  const Glib::Value<int>& intVal2 = static_cast< const Glib::Value<int>& >(v2);

  int int1 = intVal1.get();
  int int2 = intVal2.get();

  if(int1 < int2)
    return -1;
  else if(int1 == int2)
    return EXIT_SUCCESS;
  else
    return 1;
}

int main(int, char**)
{
  const int VALUES_COUNT = 10;

  Glib::init();

  Glib::Value<int> values[VALUES_COUNT];
  Glib::ValueArray array;

  for(int i = 0; i < VALUES_COUNT; i++)
  {
    values[i].init(Glib::Value<int>::value_type());
    values[i].set(i + 1); //  (i + 1) ==> Set to natural counting numbers.
    array.prepend(values[i]);
  }

  ostr << "Array members before sorting:" << std::endl;

  for(int i = 0; i < VALUES_COUNT; i++)
  {
    Glib::ValueBase value;

    if(!array.get_nth(i, value))
    {
      std::cerr << "Error getting element " << i << " of value array." <<
        std::endl;
      return EXIT_FAILURE;
      break;
    }

    auto int_val = static_cast< Glib::Value<int>& >(value);
    ostr << int_val.get() << " ";
  }
  ostr << std::endl; // End of line for list of array elements.

  // Sort array and remove last element:
  array.sort(sigc::ptr_fun(&on_compare)).remove(VALUES_COUNT - 1);

  ostr << "Array members after sorting without last element:" <<
    std::endl;

  for(int i = 0; i < VALUES_COUNT - 1; i++)
  {
    Glib::ValueBase value;

    if(!array.get_nth(i, value))
    {
      std::cerr << "Error getting element " << i << " of value array." <<
        std::endl;
      return EXIT_FAILURE;
      break;
    }

    auto int_val = static_cast< Glib::Value<int>& >(value);
    ostr << int_val.get() << " ";
  }
  ostr << std::endl; // End of line for list of array elements.

  return EXIT_SUCCESS;
}
#endif //GLIBMM_DISABLE_DEPRECATED