summaryrefslogtreecommitdiff
path: root/tests/glibmm_value/main.cc
blob: 47da4645ad3e84b4b01354fcbd756ad8a0dd47be (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "../glibmm_object/test_derived_object.h"
#include <glibmm.h>
#include <cassert>

struct Foo
{
  int bar = 1;
};

namespace Gtk
{

class Widget {
};

}

void
test()
{
  {
    Foo foo;

    // custom copyable
    Glib::Value<Foo> value;
    value.init(Glib::Value<Foo>::value_type()); // TODO: Avoid this step?
    value.set(foo);

    const auto v = value.get();
    assert(v.bar == 1);
  }

  {
    Foo foo;

    // custom pointer
    Glib::Value<Foo*> value;
    value.init(Glib::Value<Foo*>::value_type()); // TODO: Avoid this step?
    value.set(&foo);

    const auto v = value.get();
    assert(v);
  }

  {
    Foo foo;

    Glib::Value<const Foo*> value;
    value.init(Glib::Value<const Foo*>::value_type()); // TODO: Avoid this step?
    value.set(&foo);

    const auto v = value.get();
    assert(v);
  }

  {
    Gtk::Widget widget;

    // Glib::Object pointer
    Glib::Value<Gtk::Widget*> value;
    value.init(Glib::Value<Gtk::Widget*>::value_type()); // TODO: Avoid this step?
    value.set(&widget);

    const auto v = value.get();
    assert(v);
  }

  {
    Gtk::Widget widget;

    Glib::Value<const Gtk::Widget*> value;
    value.init(Glib::Value<const Gtk::Widget*>::value_type()); // TODO: Avoid this step?
    value.set(&widget);

    const auto v = value.get();
    assert(v);
  }

  Glib::init();

  // RefPtr to Glib::ObjectBase-derived type:
  {
    GObject* gobject = G_OBJECT(g_object_new(TEST_TYPE_DERIVED, nullptr));
    auto derived = Glib::make_refptr_for_instance(new DerivedObject(gobject, 5));

    using ValueType = Glib::Value<Glib::RefPtr<DerivedObject>>;
    ValueType value;
    value.init(ValueType::value_type()); // TODO: Avoid this step?

    // Check that value_type() returns the type of the underlying GObjectBase,
    // not a custom GType for the Glib::RefPtr:
    assert(ValueType::value_type() == DerivedObject::get_base_type());

    value.set(derived);

    const auto v = value.get();
    assert(v);
  }

  {
    GObject* gobject = G_OBJECT(g_object_new(TEST_TYPE_DERIVED, nullptr));
    auto derived = Glib::make_refptr_for_instance(new DerivedObject(gobject, 5));

    using ValueType = Glib::Value<Glib::RefPtr<const DerivedObject>>;
    ValueType value;
    value.init(ValueType::value_type()); // TODO: Avoid this step?

    // Check that value_type() returns the type of the underlying GObjectBase,
    // not a custom GType for the Glib::RefPtr:
    assert(ValueType::value_type() == DerivedObject::get_base_type());

    value.set(derived);

    const auto v = value.get();
    assert(v);
  }
}

// Glib::Object RefPtr<>

// template Glib::Value< Glib::RefPtr<Gdk::Pixbuf> >;
// template Glib::Value< Glib::RefPtr<const Gdk::Pixbuf> >;
//

int main() {
  test();

  return EXIT_SUCCESS;
}