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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
#include <glibmm.h>
#include <glibmm/private/interface_p.h>
#include <glibmm/private/object_p.h>
#include <iostream>
#include <stdlib.h>
// A basic derived GInterface, just to test Glib::Interface
G_DECLARE_INTERFACE(TestIface, test_Iface, TEST, IFACE, GObject)
struct _TestIface
{
GTypeInterface g_iface;
};
static void test_Iface_init(gpointer)
{
}
GType
test_Iface_get_type(void)
{
// Avoid compiler warnings about unused functions.
// TODO: With C++17, use [[maybe unused]].
#ifndef _MSC_VER
(void)TEST_IFACE;
(void)TEST_IS_IFACE;
(void)TEST_IFACE_GET_IFACE;
(void)glib_autoptr_cleanup_TestIface;
#endif
static GType type = 0;
if (!type)
{
const GTypeInfo info = {
sizeof(TestIface), // class_size
test_Iface_init, // base_init
nullptr, // base_finalize
nullptr, // class_init
nullptr, // class_finalize
nullptr, // class_data
0, // instance_size
0, // n_preallocs
nullptr, // instance_init
nullptr // value_table
};
type = g_type_register_static(G_TYPE_INTERFACE, "TestIface", &info, GTypeFlags(0));
}
return type;
}
#define TEST_TYPE_IFACE (test_Iface_get_type())
// A basic derived GObject, just to test Glib::Object.
typedef struct
{
GObject parent;
} TestDerived;
typedef struct
{
GObjectClass parent;
} TestDerivedClass;
#define TEST_TYPE_DERIVED (test_derived_get_type())
#define TEST_DERIVED(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), TEST_TYPE_DERIVED, TestDerived))
#define TEST_DERIVED_CLASS(cls) \
(G_TYPE_CHECK_CLASS_CAST((cls), TEST_TYPE_DERIVED, TestDerivedClass))
#define TEST_DERIVED_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), TEST_TYPE_DERIVED, TestDerivedClass))
static void
test_derived_class_init(TestDerivedClass*)
{
}
static void
test_derived_init(TestDerived*)
{
}
G_DEFINE_TYPE_EXTENDED(TestDerived, test_derived, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE(TEST_TYPE_IFACE, test_Iface_init))
class TestInterface;
class TestInterface_Class : public Glib::Interface_Class
{
public:
using BaseClassType = TestInterface;
friend class TestInterface;
const Glib::Interface_Class& init()
{
if (!gtype_) // create the GType if necessary
{
// Glib::Interface_Class has to know the interface init function
// in order to add interfaces to implementing types.
class_init_func_ = &TestInterface_Class::iface_init_function;
// We can not derive from another interface, and it is not necessary anyway.
gtype_ = test_Iface_get_type();
}
return *this;
}
static void iface_init_function(void* /* g_iface */, void* /* iface_data */) {}
// static Glib::ObjectBase* wrap_new(GObject*);
};
class TestInterface : public Glib::Interface
{
protected:
using CppClassType = TestInterface_Class;
TestInterface() : Glib::Interface(derived_interface_class_.init()), i_(0) {}
public:
// A real application would never make the constructor public.
// It would instead have a protected constructor and a public create() method.
TestInterface(GObject* gobject, int i) : Glib::Interface(gobject), i_(i) {}
static void add_interface(GType gtype_implementer)
{
derived_interface_class_.init().add_interface(gtype_implementer);
}
TestInterface(const TestInterface& src) = delete;
TestInterface& operator=(const TestInterface& src) = delete;
TestInterface(TestInterface&& src) noexcept : Glib::Interface(std::move(src)),
i_(std::move(src.i_))
{
}
TestInterface& operator=(TestInterface&& src) noexcept
{
Glib::Interface::operator=(std::move(src));
i_ = std::move(src.i_);
return *this;
}
int i_;
private:
friend class TestInterface_Class;
static CppClassType derived_interface_class_;
};
class DerivedObject_Class : public Glib::Class
{
public:
using BaseClassType = GObjectClass;
using CppClassParent = Glib::Object_Class;
static void class_init_function(void* g_class, void* class_data)
{
const auto klass = static_cast<BaseClassType*>(g_class);
CppClassParent::class_init_function(klass, class_data);
}
const Glib::Class& init()
{
if (!gtype_) // create the GType if necessary
{
// Glib::Class has to know the class init function to clone custom types.
class_init_func_ = &DerivedObject_Class::class_init_function;
// This is actually just optimized away, apparently with no harm.
// Make sure that the parent type has been created.
// CppClassParent::CppObjectType::get_type();
// Create the wrapper type, with the same class/instance size as the base type.
register_derived_type(test_derived_get_type());
// Add derived versions of interfaces, if the C type implements any interfaces:
TestInterface::add_interface(get_type());
}
return *this;
}
};
TestInterface::CppClassType TestInterface::derived_interface_class_; // initialize static member
class DerivedObject : public Glib::Object, public TestInterface
{
public:
using CppClassType = DerivedObject_Class;
// A real application would never make the constructor public.
// It would instead have a protected constructor and a public create() method.
explicit DerivedObject(int i)
: Glib::ObjectBase(nullptr),
Glib::Object(Glib::ConstructParams(derived_object_class_.init())),
i_(i)
{
}
DerivedObject(const DerivedObject& src) = delete;
DerivedObject& operator=(const DerivedObject& src) = delete;
DerivedObject(DerivedObject&& src) noexcept : Glib::Object(std::move(src)),
TestInterface(std::move(src)),
i_(std::move(src.i_))
{
}
DerivedObject& operator=(DerivedObject&& src) noexcept
{
Glib::Object::operator=(std::move(src));
TestInterface::operator=(std::move(src));
i_ = std::move(src.i_);
return *this;
}
int i_;
private:
friend class DerivedObject_Class;
static CppClassType derived_object_class_;
};
DerivedObject::CppClassType DerivedObject::derived_object_class_; // initialize static member
/* Shouldn't this work too?
* No, because Glib::Interface::Interface(Interface&& src) does not call
* Glib::ObjectBase::initialize_move(), and Glib::Interface::operator=(Interface&& src)
* does not call Glib::ObjectBase::operator=(std::move(src)).
static
void test_interface_move_constructor()
{
GObject *gobject = G_OBJECT(g_object_new(TEST_TYPE_DERIVED, nullptr));
g_object_ref(gobject);
TestInterface derived(gobject, 5);
std::cout << "debug: gobj(): " << derived.gobj() << std::endl;
g_assert(derived.gobj() == gobject);
TestInterface derived2(std::move(derived));
g_assert_cmpint(derived2.i_, ==, 5);
std::cout << "debug: gobj(): " << derived2.gobj() << std::endl;
g_assert(derived2.gobj() == gobject);
}
static
void test_interface_move_assignment_operator()
{
GObject *gobject = G_OBJECT(g_object_new(TEST_TYPE_DERIVED, nullptr));
g_object_ref(gobject);
TestInterface derived(gobject, 5);
//std::cout << "debug: gobj(): " << derived.gobj() << std::endl;
g_assert(derived.gobj() == gobject);
TestInterface derived2 = std::move(derived);
g_assert_cmpint(derived2.i_, ==, 5);
//std::cout << "debug: gobj(): " << derived2.gobj() << std::endl;
g_assert(derived2.gobj() == gobject);
}
*/
static void
test_object_with_interface_move_constructor()
{
DerivedObject derived(5);
g_assert_cmpint(derived.i_, ==, 5);
GObject* gobject = derived.gobj();
g_assert(derived.gobj() == gobject);
DerivedObject derived2(std::move(derived));
g_assert_cmpint(derived2.i_, ==, 5);
g_assert(derived2.gobj() == gobject);
g_assert(derived.gobj() == nullptr);
}
static void
test_object_with_interface_move_assignment_operator()
{
DerivedObject derived(5);
g_assert_cmpint(derived.i_, ==, 5);
GObject* gobject = derived.gobj();
g_assert(derived.gobj() == gobject);
DerivedObject derived2(6);
derived2 = std::move(derived);
g_assert_cmpint(derived2.i_, ==, 5);
g_assert(derived2.gobj() == gobject);
g_assert(derived.gobj() == nullptr);
}
int
main(int, char**)
{
Glib::init();
// test_interface_move_constructor();
// test_interface_move_assignment_operator();
test_object_with_interface_move_constructor();
test_object_with_interface_move_assignment_operator();
return EXIT_SUCCESS;
}
|