summaryrefslogtreecommitdiff
path: root/tests/glibmm_value/main.cc
blob: 4aea37ccd055324ab2852ca82088c0683c301687 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#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);

    // Make a copy
    Glib::Value<Foo> value2;
    value2.init(Glib::Value<Foo>::value_type()); // TODO: Avoid this step?
    value2 = value;
    const auto v2 = value2.get();
    assert(v2.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();

  // TODO: Put this test, of internal stuff, somewhere else.
  static_assert(Glib::Traits::HasGetBaseType<DerivedObject, GType()>::value,
    "DerivedObject has no get_base_type().");

  // 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);
  }

  {
    auto foo = std::make_shared<Foo>();

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

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

  {
    auto foo = std::make_shared<Foo>();

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

    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;
}