Debian Codegraph @

test/main.cpp

raw symbols SCIP
1 #include <array>
2 #include <iostream>
3 #include <memory>
4 #include <unordered_map>
5 #include <vector>
6
7 // always enable assert
8 #undef NDEBUG
9 #include "assert.h"
10
11 #define GI_CLASS_IMPL_PRAGMA
12 #define GI_TEST 1
13 #include "gi.hpp"
14
15 #include "test/test_boxed.h"
16 #include "test/test_object.h"
17
18 using namespace gi;
19 namespace GLib = gi::repository::GLib;
20 namespace GObject_ = gi::repository::GObject;
21
22 #define BOXED_COPY 0
23 #if BOXED_COPY
24 namespace gi
25 {
26 namespace repository
27 {
28 GI_ENABLE_BOXED_COPY(GBExample)
29 }
30 } // namespace gi
31 #endif
32
33 // test examples;
34 // GBoxed
35 class GBoxedExampleBase
36 : public detail::GBoxedWrapperBase<GBoxedExampleBase, GBExample>
37 {
38 typedef detail::GBoxedWrapperBase<GBoxedExampleBase, GBExample> super_type;
39
40 public:
41 static GType get_type_() { return GI_CPP_TYPE_BOXED_EXAMPLE; }
42 GBoxedExampleBase(std::nullptr_t = nullptr) : super_type() {}
43 };
44
45 class GBoxedExampleRef;
46
47 class GBoxedExample : public detail::GBoxedWrapper<GBoxedExample, GBExample,
48 GBoxedExampleBase, GBoxedExampleRef>
49 {
50 typedef detail::GBoxedWrapper<GBoxedExample, GBExample, GBoxedExampleBase,
51 GBoxedExampleRef>
52 super_type;
53 using super_type::super_type;
54 };
55
56 class GBoxedExampleRef : public detail::GBoxedRefWrapper<GBoxedExample,
57 GBExample, GBoxedExampleBase>
58 {
59 typedef detail::GBoxedRefWrapper<GBoxedExample, GBExample, GBoxedExampleBase>
60 super_type;
61 using super_type::super_type;
62 };
63
64 // plain C
65 class CBoxedExampleBase
66 : public detail::CBoxedWrapperBase<CBoxedExampleBase, CBExample>
67 {
68 typedef detail::CBoxedWrapperBase<CBoxedExampleBase, CBExample> super_type;
69
70 public:
71 CBoxedExampleBase(std::nullptr_t = nullptr) : super_type() {}
72 };
73
74 class CBoxedExampleRef;
75
76 class CBoxedExample : public detail::CBoxedWrapper<CBoxedExample, CBExample,
77 CBoxedExampleBase, CBoxedExampleRef>
78 {
79 typedef detail::CBoxedWrapper<CBoxedExample, CBExample, CBoxedExampleBase,
80 CBoxedExampleRef>
81 super_type;
82 using super_type::super_type;
83 };
84
85 class CBoxedExampleRef : public detail::CBoxedRefWrapper<CBoxedExample,
86 CBExample, CBoxedExampleBase>
87 {
88 typedef detail::CBoxedRefWrapper<CBoxedExample, CBExample, CBoxedExampleBase>
89 super_type;
90 using super_type::super_type;
91 };
92
93 enum class CppEnum { VALUE_0 = ENUM_VALUE_0, VALUE_1 = ENUM_VALUE_1 };
94
95 enum class CppFlags { VALUE_0 = FLAG_VALUE_0, VALUE_1 = FLAG_VALUE_1 };
96
97 GI_FLAG_OPERATORS(CppFlags)
98
99 namespace gi
100 {
101 namespace repository
102 {
103 template<>
104 struct declare_cpptype_of<GBExample>
105 {
106 typedef GBoxedExample type;
107 };
108
109 template<>
110 struct declare_cpptype_of<CBExample>
111 {
112 typedef CBoxedExample type;
113 };
114
115 template<>
116 struct declare_ctype_of<CppEnum>
117 {
118 typedef CEnum type;
119 };
120
121 template<>
122 struct declare_cpptype_of<CEnum>
123 {
124 typedef CppEnum type;
125 };
126
127 template<>
128 struct declare_gtype_of<CppEnum>
129 {
130 static GType get_type() { return GI_CPP_TYPE_ENUM; }
131 };
132
133 template<>
134 struct is_enumeration<CppEnum> : public std::true_type
135 {};
136
137 template<>
138 struct declare_ctype_of<CppFlags>
139 {
140 typedef CFlags type;
141 };
142
143 template<>
144 struct declare_cpptype_of<CFlags>
145 {
146 typedef CppFlags type;
147 };
148
149 template<>
150 struct declare_gtype_of<CppFlags>
151 {
152 static GType get_type() { return GI_CPP_TYPE_FLAGS; }
153 };
154
155 template<>
156 struct is_bitfield<CppFlags> : public std::true_type
157 {};
158
159 } // namespace repository
160
161 } // namespace gi
162
163 #define DECLARE_PROPERTY(pname, ptype) \
164 property_proxy<ptype> property_##pname() \
165 { \
166 return property_proxy<ptype>(*this, #pname); \
167 } \
168 const property_proxy<ptype> property_##pname() const \
169 { \
170 return property_proxy<ptype>(*this, #pname); \
171 }
172 #define DECLARE_SIGNAL(name, sig) \
173 signal_proxy<sig> signal_##name() { return signal_proxy<sig>(*this, #name); }
174
175 // simulate override construction
176 class Derived;
177
178 typedef GICppExample CDerived;
179
180 namespace base
181 {
182 class DerivedBase : public GObject_::Object
183 {
184 typedef Object super_type;
185
186 public:
187 typedef Derived self;
188 typedef CDerived BaseObjectType;
189
190 DerivedBase(std::nullptr_t = nullptr) : super_type() {}
191 BaseObjectType *gobj_() { return (BaseObjectType *)super_type::gobj_(); }
192 const BaseObjectType *gobj_() const
193 {
194 return (const BaseObjectType *)super_type::gobj_();
195 }
196 BaseObjectType *gobj_copy_() const
197 {
198 return (BaseObjectType *)super_type::gobj_copy_();
199 }
200
201 static GType get_type_() { return GI_CPP_TYPE_EXAMPLE; }
202
203 DECLARE_PROPERTY(number, int)
204 DECLARE_PROPERTY(fnumber, double)
205 DECLARE_PROPERTY(data, std::string)
206 DECLARE_PROPERTY(present, gboolean)
207 DECLARE_PROPERTY(object, Object)
208 DECLARE_PROPERTY(choice, CppEnum)
209 DECLARE_PROPERTY(flags, CppFlags)
210 DECLARE_PROPERTY(error, GLib::Error)
211
212 DECLARE_SIGNAL(
213 to_int, int(Derived, Object, bool, gboolean, const std::string &))
214 DECLARE_SIGNAL(to_string, std::string(Derived, int, gint64))
215 DECLARE_SIGNAL(to_void, void(Derived, double, CppEnum, CppFlags))
216 DECLARE_SIGNAL(to_output_int,
217 void(Derived, unsigned &,
218 gi::Collection<::GPtrArray, char *, gi::transfer_none_t>,
219 gi::Collection<::GSList, char *, gi::transfer_none_t>))
220 };
221
222 } // namespace base
223
224 class Derived : public base::DerivedBase
225 {
226 typedef base::DerivedBase super;
227 using super::super;
228 };
229
230 namespace gi
231 {
232 namespace repository
233 {
234 template<>
235 struct declare_cpptype_of<CDerived>
236 {
237 typedef Derived type;
238 };
239
240 } // namespace repository
241
242 } // namespace gi
243
244 void
245 test_trait()
246 {
247 static_assert(traits::is_basic<int>::value, "");
248 static_assert(traits::is_basic<float>::value, "");
249 static_assert(traits::is_basic<void *>::value, "");
250 static_assert(!traits::is_basic<char *>::value, "");
251
252 static_assert(traits::is_boxed<GBoxedExample>::value, "");
253 static_assert(traits::is_object<Derived>::value, "");
254 static_assert(!traits::is_object<int>::value, "");
255
256 static_assert(traits::is_reftype<GBoxedExampleRef>::value, "");
257 static_assert(!traits::is_reftype<GBoxedExample>::value, "");
258 static_assert(!traits::is_reftype<int>::value, "");
259
260 static_assert(traits::has_ctype_member<Derived>::value, "");
261 static_assert(!traits::has_ctype_member<int>::value, "");
262
263 static_assert(
264 std::is_same<traits::ctype<Derived>::type, CDerived *>::value, "");
265 static_assert(
266 std::is_same<traits::ctype<const Derived>::type, const CDerived *>::value,
267 "");
268
269 static_assert(
270 std::is_same<traits::cpptype<CDerived *>::type, Derived>::value, "");
271 static_assert(std::is_same<traits::cpptype<const CDerived *>::type,
272 const Derived>::value,
273 "");
274
275 static_assert(std::is_same<traits::ctype<CppEnum>::type, CEnum>::value, "");
276 static_assert(std::is_same<traits::cpptype<CEnum>::type, CppEnum>::value, "");
277
278 static_assert(
279 std::is_same<traits::ctype<CBoxedExample>::type, CBExample *>::value, "");
280 static_assert(
281 std::is_same<traits::cpptype<CBExample>::type, CBoxedExample>::value, "");
282 static_assert(std::is_same<traits::reftype<CBoxedExample>::type,
283 CBoxedExampleRef>::value,
284 "");
285
286 static_assert(
287 std::is_same<traits::ctype<GBoxedExample>::type, GBExample *>::value, "");
288 static_assert(
289 std::is_same<traits::cpptype<GBExample>::type, GBoxedExample>::value, "");
290 static_assert(std::is_same<traits::reftype<GBoxedExample>::type,
291 GBoxedExampleRef>::value,
292 "");
293 static_assert(
294 std::is_same<traits::cpptype<GBExample *, transfer_none_t>::type,
295 GBoxedExampleRef>::value,
296 "");
297
298 static_assert(
299 std::is_same<traits::ctype<GObject_::Object>::type, _GObject *>::value,
300 "");
301 static_assert(
302 std::is_same<traits::cpptype<_GObject>::type, GObject_::Object>::value,
303 "");
304
305 static_assert(std::is_same<traits::cpptype<bool>::type, bool>::value, "");
306
307 static_assert(traits::gtype<int>::value, "");
308 static_assert(traits::gtype<std::string>::value, "");
309
310 static_assert(traits::gvalue<int>::value, "");
311 static_assert(traits::gvalue<CppEnum>::value, "");
312
313 static_assert(traits::is_enumeration<CppEnum>::value, "");
314 static_assert(!traits::is_bitfield<CppEnum>::value, "");
315 static_assert(traits::is_bitfield<CppFlags>::value, "");
316
317 static_assert(
318 std::is_pointer<GLib::DestroyNotify::cfunction_type>::value, "");
319
320 static_assert(!detail::allocator<int>::value, "");
321 static_assert(detail::allocator<GObject_::Value>::value, "");
322 static_assert(detail::allocator<GBoxedExample>::value, "");
323
324 static_assert(traits::is_type_complete<GBExample>::value, "");
325 struct BlackBox;
326 static_assert(!traits::is_type_complete<BlackBox>::value, "");
327
328 static_assert(
329 std::is_copy_constructible<GBoxedExample>::value == BOXED_COPY, "");
330 static_assert(
331 std::is_copy_assignable<GBoxedExample>::value == BOXED_COPY, "");
332 static_assert(std::is_move_constructible<GBoxedExample>::value, "");
333 static_assert(std::is_move_assignable<GBoxedExample>::value, "");
334
335 static_assert(std::is_copy_constructible<GBoxedExampleRef>::value, "");
336 static_assert(std::is_copy_assignable<GBoxedExampleRef>::value, "");
337
338 static_assert(
339 std::is_copy_constructible<CBoxedExample>::value == BOXED_COPY, "");
340 static_assert(
341 std::is_copy_assignable<CBoxedExample>::value == BOXED_COPY, "");
342 static_assert(std::is_move_constructible<CBoxedExample>::value, "");
343 static_assert(std::is_move_assignable<CBoxedExample>::value, "");
344
345 static_assert(std::is_copy_constructible<CBoxedExampleRef>::value, "");
346 static_assert(std::is_copy_assignable<CBoxedExampleRef>::value, "");
347
348 // internal
349 static_assert(detail::_traits::is_basic_argument<int>::value, "");
350 static_assert(!detail::_traits::is_basic_argument<int &>::value, "");
351 static_assert(!detail::_traits::is_basic_argument<std::string>::value, "");
352 static_assert(
353 detail::_traits::is_basic_argument<char *const *const>::value, "");
354
355 static_assert(
356 detail::_traits::is_const_lvalue<const std::string &>::value, "");
357 static_assert(!detail::_traits::is_const_lvalue<GBoxedExample &>::value, "");
358 }
359
360 // test helper
361 template<typename T>
362 int
363 refcount(T *obj)
364 {
365 return (((GObject *)(obj))->ref_count);
366 }
367
368 void
369 test_wrap()
370 {
371 { // plain boxed
372 CBExample *ex = gi_cpp_cbexample_new();
373 auto cb = wrap(ex, transfer_full);
374 assert(cb.gobj_() == ex);
375 auto cb2 = wrap(ex, transfer_none);
376 assert(cb2 == cb);
377 assert(cb.gobj_() == cb2.gobj_());
378 auto cb3 = cb2;
379 assert(cb3 == cb2);
380 assert(cb2.gobj_() == cb3.gobj_());
381
382 auto cbn = CBoxedExample::new_();
383 assert(cbn.gobj_() != nullptr);
384 assert(cbn.gobj_()->data == 0);
385 // a peek
386 auto ex2 = unwrap(cbn, transfer_none);
387 assert(ex2 == cbn.gobj_());
388 // a fresh copy
389 auto ex3 = unwrap(std::move(cbn), transfer_full);
390 assert(ex3 != cbn.gobj_());
391 gi_cpp_cbexample_free(ex3);
392
393 cbn = CBoxedExample();
394 assert(cbn == nullptr);
395 assert(cbn.gobj_() == nullptr);
396 assert(!cbn);
397 auto ex4 = unwrap(std::move(cbn), transfer_full);
398 assert(ex4 == nullptr);
399
400 // various basic operations
401 CBoxedExample cbm(std::move(cbn));
402 cbm = std::move(cbn);
403 assert(!cbn);
404 std::swap(cbm, cbn);
405 assert(!cbm);
406
407 // compile check
408 CBoxedExample f = CBoxedExample::new_();
409 f = nullptr;
410
411 // ref
412 CBoxedExampleRef r(f);
413 assert(r.gobj_() == f.gobj_());
414 r = nullptr;
415 assert(r.gobj_() == nullptr);
416 r = f;
417 assert(r.gobj_() == f.gobj_());
418 // ref wrap
419 auto r2 = wrap(ex, transfer_none);
420 static_assert(std::is_same<decltype(r2), CBoxedExampleRef>::value, "");
421 r = r2;
422 assert(r.gobj_() == ex);
423 auto ro = unwrap(r, transfer_none);
424 auto ri = unwrap(r, transfer_none);
425 assert(ri == ro);
426 }
427
428 { // similarly so gboxed
429 GBExample *exc = gi_cpp_gbexample_new();
430 GBoxedExample w1 = wrap(exc, transfer_full);
431 GBoxedExample w2 = std::move(w1);
432 assert(!w1);
433 assert(w2.gobj_() == exc);
434 GBoxedExample w3 = w2.copy_();
435 assert(w3);
436 assert(w3 != w2);
437 assert(w3.gobj_() != w2.gobj_());
438 w3 = nullptr;
439 // ref
440 GBoxedExampleRef r(w2);
441 assert(r.gobj_() == w2.gobj_());
442 r = nullptr;
443 assert(r.gobj_() == nullptr);
444 r = w2;
445 assert(r.gobj_() == w2.gobj_());
446 // ref wrap
447 r = wrap(exc, transfer_none);
448 assert(r.gobj_() == exc);
449 auto ro = unwrap(r, transfer_none);
450 auto ri = unwrap(r, transfer_none);
451 assert(ri == ro);
452 auto rc = r.copy_();
453 assert(rc != r);
454
455 #if BOXED_COPY
456 {
457 GBoxedExample ww{w3};
458 ww = w3;
459 r = w3;
460 ww = r;
461 ww = std::move(r);
462 }
463 #endif
464 }
465
466 { // string
467 const gchar *str{};
468 std::string s1 = wrap(str, transfer_none);
469 assert(s1.empty());
470 str = "TEST";
471 std::string s2 = wrap(str, transfer_none);
472 assert(s2 == str);
473 auto *cs = unwrap(s2, transfer_none);
474 assert(g_strcmp0(cs, str) == 0);
475 // now something we should be able to free
476 cs = unwrap(s2, transfer_full);
477 g_free((gpointer)cs);
478 // unwrap empty optional
479 const std::string &emptytest = "";
480 auto cempty = unwrap(emptytest, transfer_none);
481 assert(*cempty == 0);
482 auto ocempty = unwrap(
483 static_cast<const detail::optional_string &>(emptytest), transfer_none);
484 assert(ocempty == nullptr);
485 }
486
487 { // some more plain cases
488 wrap(5);
489 unwrap(5, transfer_none);
490
491 const double ex = 5.0;
492 auto w = wrap(ex);
493 auto ex2 = unwrap(w);
494 assert(ex == ex2);
495
496 CEnum ec{};
497 CppEnum ecpp{};
498 ecpp = wrap(ec);
499 unwrap(ecpp, transfer_none);
500 }
501
502 { // object
503 CDerived *ob = gi_cpp_example_new();
504 assert(refcount(ob) == 1);
505 auto wo = wrap(ob, transfer_none);
506 assert(refcount(ob) == 2);
507 assert(wo.gobj_() == ob);
508 {
509 auto wo2 = wo;
510 assert(wo2 == wo);
511 assert(wo2.gobj_() == ob);
512 assert(refcount(ob) == 3);
513 // cast to own type should work fine
514 auto wo3 = object_cast<Derived>(wo2);
515 assert(wo3);
516 assert(refcount(ob) == 4);
517 }
518 assert(refcount(ob) == 2);
519 auto ob2 = unwrap(wo, transfer_none);
520 assert(ob2 == ob);
521 assert(refcount(ob) == 2);
522 ob2 = unwrap(wo, transfer_full);
523 assert(ob2 == ob);
524 assert(refcount(ob) == 3);
525 g_object_unref(ob2);
526 ob2 = nullptr;
527 assert(refcount(ob) == 2);
528
529 // no change for full transfer
530 auto wo2 = wrap(ob, transfer_full);
531 assert(wo2.gobj_() == ob);
532 assert(refcount(ob) == 2);
533 // compensate
534 g_object_ref(ob);
535 wo2 = Derived();
536 assert(wo2.gobj_() == nullptr);
537 assert(refcount(ob) == 2);
538 wo2 = std::move(wo);
539 assert(wo.gobj_() == nullptr);
540 assert(wo2.gobj_() == ob);
541 wo2 = wo;
542 assert(refcount(ob) == 1);
543 g_object_unref(ob);
544
545 Derived wo3(wo2);
546 if (wo3)
547 wo3 = nullptr;
548 }
549
550 {
551 // object creation
552 Derived ob = GObject_::Object::new_<Derived>();
553 ob = GObject_::Object::new_<Derived>(
554 NAME_NUMBER, (int)5, NAME_PRESENT, true);
555 assert(ob.property_number().get() == 5);
556 assert(ob.property_present().get() == true);
557
558 Derived ob2 = GObject_::Object::new_<Derived>();
559 std::swap(ob2, ob);
560 }
561
562 {
563 // paramspec
564 auto pspec = GObject_::ParamSpec::new_<int>("p", "p", "p", 0, 10);
565 // is otherwise similar to property and is tested there further
566 }
567
568 { // wrap_to
569 int x = 5;
570 auto y = wrap_to<int>(x, transfer_none);
571 assert(x == y);
572 wrap_to<std::string>("x", transfer_none);
573 }
574
575 { // unwrap_maybe_float
576 CDerived *ob = gi_cpp_example_new();
577 assert(refcount(ob) == 1);
578 auto wo = wrap(ob, transfer_full);
579 assert(refcount(ob) == 1);
580 CDerived *uwo = detail::unwrap_maybe_float(std::move(wo), transfer_none);
581 assert(uwo == ob);
582 assert(!wo);
583 assert(refcount(ob) == 1);
584 assert(g_object_is_floating(ob));
585 wo = wrap(ob, transfer_none);
586 assert(refcount(ob) == 1);
587 assert(!g_object_is_floating(ob));
588 uwo = detail::unwrap_maybe_float(std::move(wo), transfer_full);
589 assert(!wo);
590 assert(uwo == ob);
591 assert(refcount(ob) == 1);
592 wo = wrap(ob, transfer_full);
593 assert(refcount(ob) == 1);
594 // wrapper will clean up
595
596 GBExample *exc = gi_cpp_gbexample_new();
597 GBoxedExample w1 = wrap(exc, transfer_full);
598 GBExample *uw1 = detail::unwrap_maybe_float(std::move(w1), transfer_full);
599 assert(!w1);
600 assert(uw1 == exc);
601 w1 = wrap(exc, transfer_full);
602 // wrapper will clean up
603 }
604
605 { // compile checks on cs_ptr helper
606 using Object = gi::repository::GObject::Object;
607 auto g = [](Object) {};
608 auto f = [&g](const gi::cs_ptr<Object> &ob) {
609 ob->handler_block(0);
610 Object obx = ob;
611 auto y = ob;
612 auto x = [y]() { y->handler_block(1); };
613 g(ob);
614 };
615 Object ob{};
616 if (false)
617 f(ob);
618 }
619 }
620
621 void
622 test_helpers()
623 {
624 constexpr int VALUE = 5;
625 int value = VALUE * 3;
626 using nmtype = std::unique_ptr<int>;
627 struct Args
628 {
629 gi::required<nmtype> ptr;
630 gi::required<int> v;
631 int ov{};
632 int &rv;
633 };
634
635 auto rf = [&value](nmtype ptr, int v, int ov, int &rv) {
636 assert(ptr && *ptr == VALUE);
637 assert(v == VALUE * 2);
638 assert(ov == 0);
639 assert(rv == value);
640 };
641
642 auto f = [&rf](Args args) {
643 rf(std::move(args.ptr), std::move(args.v), std::move(args.ov), args.rv);
644 };
645
646 f({.ptr = std::make_unique<int>(VALUE), .v = VALUE * 2, .rv = value});
647 }
648
649 using gi::detail::Collection;
650 using gi::detail::CollectionHolder;
651
652 namespace collection_helpers
653 {
654 struct LVHandler
655 {
656 template<typename T>
657 T &f(T &t)
658 {
659 return t;
660 }
661 };
662
663 struct RVHandler
664 {
665 template<typename T>
666 T &&f(T &t)
667 {
668 return std::move(t);
669 }
670 };
671
672 template<typename ListType, typename T, typename Transfer>
673 using CollectionProxy =
674 Collection<ListType, typename traits::ctype<T>::type, Transfer>;
675
676 template<typename ListType, typename T, typename Transfer>
677 using CollectionHolderProxy =
678 CollectionHolder<ListType, typename traits::ctype<T>::type, Transfer>;
679
680 std::string
681 get_value(const std::string &x)
682 {
683 return x;
684 }
685
686 std::string
687 get_value(const gi::cstring &x)
688 {
689 return x;
690 }
691
692 template<typename T>
693 auto
694 get_value(const T &x) -> decltype(unwrap(x, transfer_none))
695 {
696 return unwrap(const_cast<T &>(x), transfer_none);
697 }
698
699 template<template<typename, typename, typename> class CT = CollectionProxy,
700 typename CppType, typename Transfer, typename LVRHandler>
701 void
702 check_collection(std::vector<CppType> &&v, const Transfer &t, LVRHandler h)
703 {
704 // collect size
705 const auto VS = v.size();
706 // and (internal) value for later check
707 using VT = decltype(get_value(v.front()));
708 std::vector<VT> baseline;
709 auto get_values = [&v] {
710 decltype(baseline) out;
711 for (auto &val : v)
712 out.push_back(get_value(val));
713 return out;
714 };
715 baseline = get_values();
716 auto do_check = [&get_values, &baseline]() {
717 auto current = get_values();
718 assert(current == baseline);
719 };
720 auto check_list_it = [&baseline](auto &list) {
721 decltype(baseline) current;
722 int cnt = 0;
723 for (auto &e : list) {
724 // e should be const & iff Transfer none
725 using el_type = typename std::remove_reference<decltype(e)>::type;
726 static_assert(std::is_const<el_type>::value ==
727 std::is_same<Transfer, transfer_none_t>::value,
728 "");
729 assert(e);
730 ++cnt;
731 current.push_back(get_value(e));
732 }
733 assert(cnt == int(baseline.size()));
734 assert(current == baseline);
735 cnt = 0;
736 current.clear();
737 const auto &clist = list;
738 for (auto &e : clist) {
739 // e should always be const & here
740 using el_type = typename std::remove_reference<decltype(e)>::type;
741 static_assert(std::is_const<el_type>::value, "");
742 assert(e);
743 ++cnt;
744 current.push_back(get_value(e));
745 }
746 assert(cnt == int(baseline.size()));
747 assert(current == baseline);
748 // assign from non-const to const
749 auto cb = clist.begin();
750 cb = list.begin();
751 assert(!cnt || *cb);
752 };
753
754 // various trips through variations
755 CT<::GList, CppType, Transfer> l{h.f(v)};
756 static constexpr bool is_sub = std::is_same<decltype(l),
757 CollectionHolderProxy<::GList, CppType, Transfer>>::value;
758 static_assert(is_sub || (sizeof(l) == sizeof(::GList *)), "");
759 check_list_it(l);
760 v = std::move(l);
761 assert(!l);
762 assert(v.size() == VS);
763 do_check();
764 l = h.f(v);
765 v = std::move(l);
766 assert(!l);
767 assert(v.size() == VS);
768 do_check();
769 CT<::GSList, CppType, Transfer> sl{h.f(v)};
770 static_assert(is_sub || (sizeof(l) == sizeof(::GSList *)), "");
771 check_list_it(sl);
772 v = std::move(sl);
773 assert(!sl);
774 assert(v.size() == VS);
775 do_check();
776 sl = h.f(v);
777 v = std::move(sl);
778 assert(!sl);
779 assert(v.size() == VS);
780 do_check();
781 CT<::GPtrArray, CppType, Transfer> pa{h.f(v)};
782 static_assert(is_sub || (sizeof(l) == sizeof(::GPtrArray *)), "");
783 check_list_it(pa);
784 v = std::move(pa);
785 assert(!pa);
786 assert(v.size() == VS);
787 do_check();
788 pa = h.f(v);
789 v = std::move(pa);
790 assert(!pa);
791 assert(v.size() == VS);
792 do_check();
793 CT<DSpan, CppType, Transfer> da{h.f(v)};
794 check_list_it(da);
795 v = std::move(da);
796 assert(!da);
797 assert(v.size() == VS);
798 do_check();
799 da = h.f(v);
800 v = std::move(da);
801 assert(!da);
802 assert(v.size() == VS);
803 do_check();
804 CT<ZTSpan, CppType, Transfer> za{h.f(v)};
805 check_list_it(za);
806 v = std::move(za);
807 assert(!za);
808 za = h.f(v);
809 v = std::move(za);
810 assert(!za);
811 assert(v.size() == VS);
812 do_check();
813 l = h.f(v);
814 // temporarily use a separate sv
815 // (so v can keep its elements alive, e.g. std::string case)
816 std::vector<CppType> sv;
817 sv.resize(VS);
818 std::move(l).move_to(sv.data());
819 assert(!l);
820 v = std::move(sv);
821 sv.clear();
822 do_check();
823 // in case of holder; l has ownership here (so none wrap/unwrap below ok)
824 l = h.f(v);
825 auto lu = unwrap(std::move(l), t);
826 assert(!l || !t.value);
827 // last part; always need to wrap to a basic wrapper
828 using WrapTypeL = CollectionProxy<::GList, CppType, Transfer>;
829 auto ln = wrap_to<WrapTypeL>(lu, t);
830 assert(lu == ln.gobj_());
831 v = std::move(ln);
832 do_check();
833 pa = h.f(v);
834 auto pau = unwrap(std::move(pa), t);
835 assert(!pa || !t.value);
836 // ref'ed case, so it can always take ownership
837 using WrapTypeA = CollectionProxy<::GPtrArray, CppType, Transfer>;
838 auto pan = wrap_to<WrapTypeA>(pau, t);
839 assert(pau == pan.gobj_());
840 }
841
842 template<template<typename, typename, typename> class CT = CollectionProxy,
843 typename CppKey, typename CppType, typename Transfer, typename LVRHandler>
844 void
845 check_collection(std::map<CppKey, CppType> &&v, const Transfer &t, LVRHandler h)
846 {
847 const auto VS = v.size();
848 using VT = decltype(get_value(v.begin()->second));
849 std::map<CppKey, VT> baseline;
850 auto get_values = [](auto &c) {
851 decltype(baseline) out;
852 for (auto &val : c)
853 out[val.first] = get_value(val.second);
854 return out;
855 };
856 baseline = get_values(v);
857 auto do_check = [&get_values, &baseline](auto &c) {
858 auto current = get_values(c);
859 assert(current == baseline);
860 };
861 auto check_map_it = [&baseline](auto &list) {
862 decltype(baseline) current;
863 int cnt = 0;
864 for (auto &e : list) {
865 assert(e.first);
866 assert(e.second);
867 ++cnt;
868 current[get_value(e.first)] = get_value(e.second);
869 }
870 assert(cnt == int(baseline.size()));
871 assert(current == baseline);
872 cnt = 0;
873 current.clear();
874 const auto &clist = list;
875 for (auto &e : clist) {
876 assert(e.first);
877 assert(e.second);
878 ++cnt;
879 current[get_value(e.first)] = get_value(e.second);
880 }
881 assert(cnt == int(baseline.size()));
882 assert(current == baseline);
883 };
884
885 CT<::GHashTable, std::pair<CppKey, CppType>, Transfer> l{h.f(v)};
886 static constexpr bool is_sub = std::is_same<decltype(l),
887 CollectionHolderProxy<::GHashTable, std::pair<CppKey, CppType>,
888 Transfer>>::value;
889 static_assert(is_sub || (sizeof(l) == sizeof(::GHashTable *)), "");
890 check_map_it(l);
891 v = std::move(l);
892 assert(!l);
893 assert(v.size() == VS);
894 do_check(v);
895 l = h.f(v);
896 v = std::move(l);
897 assert(!l);
898 assert(v.size() == VS);
899 do_check(v);
900 l = h.f(v);
901 std::unordered_map<std::string, CppType> w;
902 w = std::move(l);
903 assert(!l);
904 assert(w.size() == VS);
905 do_check(w);
906 // in case of holder; l has ownership here (so none wrap/unwrap below ok)
907 l = h.f(w);
908 auto lu = unwrap(std::move(l), t);
909 assert(!l || !t.value);
910 // last part; always need to wrap to a basic wrapper
911 using WrapType =
912 CollectionProxy<::GHashTable, std::pair<CppKey, CppType>, Transfer>;
913 auto ln = wrap_to<WrapType>(lu, t);
914 assert(lu == ln.gobj_());
915 }
916
917 template<typename ListType, typename Transfer>
918 static void
919 check_collection_null()
920
921 { // check nullptr cases
922 // type not relevant in these cases
923 using BT = CDerived *;
924 std::vector<Derived> bv;
925 Transfer t;
926 {
927 auto ln =
928 wrap_to<Collection<ListType, BT, Transfer>>((ListType *)(nullptr), t);
929 assert(!ln);
930 ln = nullptr;
931 assert(!ln);
932 bv = std::move(ln);
933 assert(bv.empty());
934 }
935 }
936
937 } // namespace collection_helpers
938
939 void
940 test_collection()
941 {
942 using namespace collection_helpers;
943
944 auto make_object = [] { return wrap(gi_cpp_example_new(), transfer_full); };
945
946 auto make_box = []() -> GBoxedExample {
947 return wrap(gi_cpp_gbexample_new(), transfer_full);
948 };
949
950 { // init list construction
951 CollectionProxy<::GPtrArray, Derived, transfer_full_t> coll_pa{
952 make_object(), make_object()};
953 assert(coll_pa.size() == 2);
954 CollectionProxy<gi::DSpan, int, transfer_full_t> coll_da{2, 3, 4};
955 assert(coll_da.size() == 3);
956 #if 0
957 // will not work, since not copyable
958 CollectionProxy<gi::ZTSpan, GBoxedExample, transfer_full_t> coll_za{
959 make_box()};
960 assert(coll_za.size() == 1);
961 // but neither will the following
962 std::vector<std::unique_ptr<int>> x{std::make_unique<int>(5)};
963 #endif
964 }
965
966 // full
967 { // object
968 std::vector<Derived> v{make_object(), make_object()};
969 check_collection(std::move(v), transfer_full, LVHandler());
970 // map
971 std::map<std::string, Derived> m{
972 {"x", make_object()}, {"y", make_object()}};
973 check_collection(std::move(m), transfer_full, LVHandler());
974 }
975 { // non-copyable boxed
976 std::vector<GBoxedExample> v;
977 v.push_back(make_box());
978 v.push_back(make_box());
979 check_collection(std::move(v), transfer_full, RVHandler());
980 // map
981 std::map<std::string, GBoxedExample> m;
982 m["x"] = make_box();
983 m["y"] = make_box();
984 check_collection(std::move(m), transfer_full, RVHandler());
985 }
986 { // string
987 std::vector<std::string> v{"foo", "bar"};
988 check_collection(std::move(v), transfer_full, LVHandler());
989 // map
990 std::map<std::string, std::string> m{{"x", "foo"}, {"y", "bar"}};
991 check_collection(std::move(m), transfer_full, LVHandler());
992 }
993 { // cstring
994 std::vector<gi::cstring> v{"foo", "bar"};
995 check_collection(std::move(v), transfer_full, LVHandler());
996 // map
997 std::map<gi::cstring, gi::cstring> m{{"x", "foo"}, {"y", "bar"}};
998 check_collection(std::move(m), transfer_full, LVHandler());
999 }
1000 { // plain
1001 std::vector<int> v{2, 3};
1002 auto w = v;
1003 Collection<gi::DSpan, int, transfer_full_t> da{v.data(), v.size()};
1004 v = std::move(da);
1005 assert(!da);
1006 assert(v == w);
1007 da = v;
1008 std::move(da).move_to(v.data());
1009 assert(v == w);
1010 }
1011
1012 // container
1013 { // object
1014 std::vector<Derived> v{make_object(), make_object()};
1015 // hold item ownership as they are moved about
1016 auto t = v;
1017 check_collection(std::move(v), transfer_container, LVHandler());
1018 // map
1019 std::map<std::string, Derived> m{
1020 {"x", make_object()}, {"y", make_object()}};
1021 auto tm = m;
1022 check_collection(std::move(m), transfer_container, LVHandler());
1023 }
1024 { // copyable ref boxed
1025 std::vector<GBoxedExample> v;
1026 v.push_back(make_box());
1027 v.push_back(make_box());
1028 std::vector<GBoxedExampleRef> w{v.begin(), v.end()};
1029 check_collection(std::move(w), transfer_container, LVHandler());
1030 // map
1031 std::map<std::string, GBoxedExampleRef> m{
1032 {"x", v.front()}, {"y", v.back()}};
1033 check_collection(std::move(m), transfer_container, LVHandler());
1034 }
1035 { // string
1036 std::vector<std::string> v{"foo", "bar"};
1037 check_collection(std::move(v), transfer_container, LVHandler());
1038 // map
1039 std::map<std::string, std::string> m{{"x", "foo"}, {"y", "bar"}};
1040 check_collection(std::move(m), transfer_container, LVHandler());
1041 }
1042 { // cstring
1043 std::vector<gi::cstring> v{"foo", "bar"};
1044 std::vector<gi::cstring_v> w{v.begin(), v.end()};
1045 check_collection(std::move(w), transfer_container, LVHandler());
1046 // map
1047 std::map<gi::cstring, gi::cstring_v> m{{"x", v.front()}, {"y", v.back()}};
1048 check_collection(std::move(m), transfer_container, LVHandler());
1049 }
1050 { // plain
1051 std::vector<CppEnum> v{CppEnum::VALUE_0, CppEnum::VALUE_1};
1052 auto w = v;
1053 CollectionProxy<gi::DSpan, CppEnum, transfer_container_t> da{
1054 v.data(), v.size()};
1055 v = std::move(da);
1056 assert(!da);
1057 assert(v == w);
1058 da = v;
1059 std::move(da).move_to(v.data());
1060 assert(v == w);
1061 }
1062
1063 // none
1064 {
1065 static_assert(!std::is_copy_constructible<
1066 CollectionHolder<::GList, CDerived *>>::value,
1067 "");
1068 static_assert(std::is_move_constructible<
1069 CollectionHolder<::GList, CDerived *>>::value,
1070 "");
1071 }
1072 // parameter helper should behave much as a dynamic container transfer
1073 { // object
1074 std::vector<Derived> v{make_object(), make_object()};
1075 // hold item ownership as they are moved about
1076 auto t = v;
1077 check_collection<CollectionHolderProxy>(
1078 std::move(v), transfer_none, LVHandler());
1079 // map
1080 std::map<std::string, Derived> m{
1081 {"x", make_object()}, {"y", make_object()}};
1082 auto tm = m;
1083 check_collection<CollectionHolderProxy>(
1084 std::move(m), transfer_none, LVHandler());
1085 }
1086 { // copyable ref boxed
1087 std::vector<GBoxedExample> v;
1088 v.push_back(make_box());
1089 v.push_back(make_box());
1090 std::vector<GBoxedExampleRef> w{v.begin(), v.end()};
1091 check_collection<CollectionHolderProxy>(
1092 std::move(w), transfer_none, LVHandler());
1093 // map
1094 std::map<std::string, GBoxedExampleRef> m{
1095 {"x", v.front()}, {"y", v.back()}};
1096 check_collection<CollectionHolderProxy>(
1097 std::move(m), transfer_none, LVHandler());
1098 }
1099 { // string
1100 std::vector<std::string> v{"foo", "bar"};
1101 check_collection<CollectionHolderProxy>(
1102 std::move(v), transfer_none, LVHandler());
1103 // map
1104 std::map<std::string, std::string> m{{"x", "foo"}, {"y", "bar"}};
1105 check_collection<CollectionHolderProxy>(
1106 std::move(m), transfer_none, LVHandler());
1107 }
1108 { // cstring
1109 std::vector<gi::cstring> v{"foo", "bar"};
1110 std::vector<gi::cstring_v> w{v.begin(), v.end()};
1111 check_collection<CollectionHolderProxy>(
1112 std::move(w), transfer_none, LVHandler());
1113 // map
1114 std::map<gi::cstring, gi::cstring_v> m{{"x", v.front()}, {"y", v.back()}};
1115 check_collection<CollectionHolderProxy>(
1116 std::move(m), transfer_none, LVHandler());
1117 }
1118
1119 // some other array cases
1120 {
1121 std::array<const char *, 2> sa{{"x", "y"}};
1122 Collection<gi::DSpan, char *, transfer_none_t> sc;
1123 sc = wrap_to<decltype(sc)>(sa.data(), sa.size(), transfer_none);
1124 assert(sc.gobj_() == sa.data());
1125 auto usa = unwrap(sc, transfer_none);
1126 assert(usa == sa.data());
1127 }
1128 { // case that might arise trying to pass main's(argv, argc) to a parameter
1129 std::array<const char *, 2> sa{{"x", "y"}};
1130 CollectionParameter<gi::DSpan, char *, transfer_none_t> sc{
1131 (char **)sa.data(), sa.size()};
1132 assert(sc.gobj_() == sa.data());
1133 auto usa = unwrap(sc, transfer_none);
1134 assert(usa == sa.data());
1135 }
1136 {
1137 std::array<int, 2> a{3, 4};
1138 CollectionHolderProxy<gi::DSpan, int, transfer_none_t> ac{
1139 a.data(), a.size()};
1140 // optimization applies
1141 assert(ac.gobj_() == a.data());
1142 assert(ac._size() == a.size());
1143 auto rc = ac.gobj_();
1144 auto uac = unwrap(std::move(ac), transfer_none);
1145 assert(rc == uac);
1146 for (int i : {0, 1})
1147 assert(uac[i] == a[i]);
1148 ac = wrap_to<decltype(ac)>(uac, a.size(), transfer_none);
1149 assert(ac.gobj_() == uac);
1150 }
1151 {
1152 std::array<int, 2> a{5, 6};
1153 // allocate new array
1154 Collection<gi::DSpan, int, transfer_full_t> ac{a.data(), a.size()};
1155 assert(ac.gobj_() != a.data());
1156 assert(ac._size() == a.size());
1157 auto rc = ac.gobj_();
1158 auto uac = unwrap(std::move(ac), transfer_full);
1159 // now owned by uac above
1160 assert(!ac);
1161 assert(rc == uac);
1162 for (int i : {0, 1})
1163 assert(uac[i] == a[i]);
1164 // transfer full to ac again
1165 ac = wrap_to<decltype(ac)>(uac, a.size(), transfer_full);
1166 assert(ac.gobj_() == uac);
1167 }
1168
1169 { // check GHashTable transfer_none hack
1170 using VT = std::pair<char *, char *>;
1171 using TT = gi::CollectionParameter<::GHashTable, VT, transfer_none_t>;
1172 static_assert(
1173 std::is_same<TT,
1174 gi::detail::Collection<::GHashTable, VT, transfer_full_t>>::value,
1175 "");
1176 // should work for refcnt case
1177 TT t;
1178 unwrap(t, transfer_none);
1179 // also check null handling
1180 assert(!t);
1181 t = nullptr;
1182 assert(!t);
1183 std::map<std::string, std::string> m = std::move(t);
1184 assert(m.empty());
1185 }
1186
1187 { // nullptr handling
1188 check_collection_null<::GList, transfer_full_t>();
1189 check_collection_null<::GList, transfer_none_t>();
1190 check_collection_null<::GSList, transfer_full_t>();
1191 check_collection_null<::GSList, transfer_none_t>();
1192 check_collection_null<::GPtrArray, transfer_full_t>();
1193 check_collection_null<::GPtrArray, transfer_none_t>();
1194 // array cases
1195 using BT = int;
1196 std::vector<int> bv;
1197 {
1198 auto ln = wrap_to<Collection<ZTSpan, BT, transfer_full_t>>(
1199 (BT *)(nullptr), transfer_full);
1200 assert(!ln);
1201 ln = nullptr;
1202 assert(!ln);
1203 bv = std::move(ln);
1204 assert(bv.empty());
1205 }
1206 {
1207 auto ln = wrap_to<Collection<DSpan, BT, transfer_none_t>>(
1208 (BT *)(nullptr), transfer_none);
1209 assert(!ln);
1210 ln = nullptr;
1211 assert(!ln);
1212 bv = std::move(ln);
1213 assert(bv.empty());
1214 }
1215 }
1216
1217 { // transition to other transfer types
1218 auto check_equal = [](auto &l1, auto &l2) {
1219 assert(!l1.empty());
1220 assert(!l2.empty());
1221 assert(l1.front());
1222 assert(l2.front());
1223 auto it1 = l1.begin();
1224 auto it2 = l2.begin();
1225 for (; it1 != l1.end() && it2 != l2.end(); ++it1, ++it2) {
1226 assert(*it1 == *it2);
1227 }
1228 };
1229 CollectionProxy<::GList, GBoxedExample, transfer_full_t> lf;
1230 lf.push_back(make_box());
1231 lf.push_back(make_box());
1232 assert(!lf.empty());
1233 assert(++lf.begin() != lf.end());
1234 assert(++(++lf.begin()) == lf.end());
1235 // generic container creation
1236 CollectionProxy<::GList, GBoxedExample, transfer_container_t> lc{lf};
1237 check_equal(lf, lc);
1238 // special case to none
1239 CollectionProxy<::GList, GBoxedExample, transfer_none_t> ln{lf};
1240 check_equal(lf, ln);
1241 // also this way
1242 lc = lf;
1243 check_equal(lf, lc);
1244 ln = lc;
1245 check_equal(ln, lc);
1246 }
1247
1248 auto uw = [](const auto &obj) { return unwrap(obj, transfer_none); };
1249 auto check_list = [&uw](auto &lf, auto factory) {
1250 lf.push_back(factory());
1251 assert(!lf.empty());
1252 auto p = uw(lf.front());
1253 lf.push_back(factory());
1254 assert(uw(lf.front()) == p);
1255 assert(std::next(lf.begin()) != lf.end());
1256 assert(std::next(lf.begin(), 2) == lf.end());
1257 lf.push_front(factory());
1258 assert(uw(lf.front()) != p);
1259 lf.pop_front();
1260 assert(uw(lf.front()) == p);
1261 lf.pop_back();
1262 assert(uw(lf.front()) == p);
1263 lf.pop_back();
1264 assert(lf.empty());
1265 lf.push_front(factory());
1266 assert(!lf.empty());
1267 lf.pop_front();
1268 assert(lf.empty());
1269 lf.push_front(factory());
1270 assert(!lf.empty());
1271 lf.clear();
1272 assert(lf.empty());
1273 auto it = lf.insert(lf.begin(), factory());
1274 p = uw(lf.front());
1275 assert(it == lf.begin());
1276 it = lf.insert(lf.begin(), factory());
1277 assert(it == lf.begin());
1278 assert(uw(lf.front()) != p);
1279 lf.insert(lf.end(), factory());
1280 assert(uw(*std::next(lf.begin())) == p);
1281 it = std::next(lf.begin());
1282 while (it != lf.end())
1283 it = lf.erase(it);
1284 assert(std::next(lf.begin()) == lf.end());
1285 it = lf.erase(lf.begin());
1286 assert(it == lf.end());
1287 assert(lf.empty());
1288 lf.push_front(factory());
1289 typename std::remove_reference<decltype(lf)>::type lfo;
1290 lf.swap(lfo);
1291 assert(lf.empty());
1292 assert(!lfo.empty());
1293 lfo.pop_front();
1294 assert(lfo.empty());
1295 };
1296 auto check_array = [&uw](auto &l, auto factory) {
1297 l.clear();
1298 l.push_back(factory());
1299 auto p = uw(l.front());
1300 assert(p);
1301 assert(l.data()[0] == l[0]);
1302 assert(l.data()[0] == l.at(0));
1303 assert(l.data()[0] == l.front());
1304 assert(l.at(0));
1305 l.resize(10);
1306 assert(l.size() == 10);
1307 assert(!l.back());
1308 l.resize(1);
1309 assert(l.size() == 1);
1310 assert(l.back() == l.front());
1311 assert(uw(l.back()) == p);
1312 };
1313 { // check common operations on lists
1314 CollectionProxy<::GList, GBoxedExample, transfer_full_t> lf;
1315 check_list(lf, make_box);
1316 // special list part
1317 lf.clear();
1318 lf.push_back(make_box());
1319 lf.push_back(make_box());
1320 auto p = lf.front().gobj_();
1321 lf.reverse();
1322 assert(lf.front().gobj_() != p);
1323 lf.reverse();
1324 assert(lf.front().gobj_() == p);
1325 // ptrarray
1326 CollectionProxy<::GPtrArray, GBoxedExample, transfer_full_t> pa;
1327 check_list(pa, make_box);
1328 check_array(pa, make_box);
1329 // dyn array
1330 CollectionProxy<gi::DSpan, GBoxedExample, transfer_full_t> da;
1331 check_list(da, make_box);
1332 check_array(da, make_box);
1333 // zt array
1334 CollectionProxy<gi::ZTSpan, GBoxedExample, transfer_full_t> za;
1335 check_list(za, make_box);
1336 check_array(za, make_box);
1337 // check ZT
1338 za.clear();
1339 za.push_back(make_box());
1340 za.push_back(make_box());
1341 assert(*za.end() == nullptr);
1342 p = za.front().gobj_();
1343 // auto d = za.data();
1344 auto cap = za.capacity();
1345 za.reserve(5 * cap);
1346 assert(za.capacity() >= 5 * cap);
1347 // NOTE new alloc might match old alloc
1348 // assert(d != za.data());
1349 assert(za.front().gobj_() == p);
1350 // plain array
1351 CollectionProxy<gi::DSpan, int, transfer_full_t> pda;
1352 int gen = 1;
1353 auto make_int = [&gen]() { return ++gen; };
1354 check_list(pda, make_int);
1355 check_array(pda, make_int);
1356 }
1357 { // map operations
1358 Collection<::GHashTable, std::pair<char *, GBExample *>, transfer_full_t>
1359 mf;
1360 assert(mf.empty());
1361 const char *KEY1 = "key1";
1362 const char *KEY2 = "key2";
1363 auto v1 = make_box();
1364 auto v2 = make_box();
1365 auto p1 = uw(v1);
1366 auto p2 = uw(v2);
1367 auto ret = mf.replace(KEY1, std::move(v1));
1368 assert(ret);
1369 assert(mf.size() == 1);
1370 ret = mf.replace(KEY2, std::move(v2));
1371 assert(ret);
1372 assert(mf.size() == 2);
1373 assert(std::next(std::next(mf.begin())) == mf.end());
1374 assert(mf.find("blah") == mf.end());
1375 assert(!mf.lookup("blah"));
1376 auto it = mf.find(KEY1);
1377 assert(it != mf.end());
1378 assert(uw(it->second) == p1);
1379 it = mf.find(KEY2);
1380 assert(it != mf.end());
1381 assert(uw(it->second) == p2);
1382 // transfer to variant container
1383 // generic to container
1384 Collection<::GHashTable, std::pair<char *, GBExample *>,
1385 transfer_container_t>
1386 mc{mf};
1387 assert(mc.size() == mf.size());
1388 mc.clear();
1389 assert(mc.empty());
1390 mc = mf;
1391 assert(mc.size() == mf.size());
1392 assert(uw(mc.lookup(KEY2)) == p2);
1393 // always down to none
1394 Collection<::GHashTable, std::pair<char *, GBExample *>, transfer_none_t>
1395 mn{mf};
1396 assert(mn.size() == mf.size());
1397 assert(uw(mn.lookup(KEY2)) == p2);
1398 mn = mc;
1399 assert(mn.size() == mf.size());
1400 assert(uw(mn.lookup(KEY1)) == p1);
1401 }
1402 }
1403
1404 namespace string_helpers
1405 {
1406 struct MyView
1407 {
1408 const char *c_str() const { return nullptr; }
1409 size_t size() const { return 0; }
1410 };
1411
1412 struct MyViewCustom
1413 {};
1414
1415 using cstr = detail::cstring;
1416 using cstrv = detail::cstring_v;
1417
1418 template<typename StringType>
1419 void
1420 check_string()
1421 {
1422 constexpr bool is_view = std::is_same<cstrv, StringType>::value;
1423 // construct
1424 const char *STR = "ab";
1425 StringType cs(STR);
1426 assert((cs.data() == STR) == is_view);
1427 std::string s("cd");
1428 StringType tcs(s);
1429 cs = s;
1430 assert((cs.data() == s.data()) == is_view);
1431 cstrv csv(cs);
1432 assert(csv.data() == cs.data());
1433 tcs = std::move(cs);
1434 assert(tcs.data() == csv.data());
1435 cs = std::move(tcs);
1436 assert(cs.data() == csv.data());
1437 // ops
1438 for (auto &c : cs)
1439 assert(c);
1440 assert(cs.size() == s.size());
1441 assert(cs.length() == cs.size());
1442 assert(!cs.empty());
1443 assert(cs.at(0) == s.at(0));
1444 assert(cs[0] == s[0]);
1445 assert(cs.front() == s.front());
1446 assert(cs.compare(cs) == 0);
1447 assert(cs.find(s) == 0);
1448 assert(cs.find(s, 1) == cs.npos);
1449 assert(cs.rfind(s) == 0);
1450 assert(cs.rfind(s, 1) == cs.npos);
1451
1452 // assign/construct to string
1453 std::string ns{cs};
1454 assert(ns == s);
1455 ns = cs;
1456 assert(ns == s);
1457 assert(ns == cs);
1458 assert(cs == cs);
1459 ns = "xy";
1460 assert(cs <= ns);
1461 assert(cs < ns);
1462 assert(ns > cs);
1463 assert(ns >= cs);
1464 assert(ns != cs);
1465
1466 // construct using convert
1467 {
1468 MyViewCustom cv;
1469 StringType cs(cv);
1470 assert(!cs);
1471 }
1472 cs.swap(tcs);
1473 assert(tcs);
1474 cs = nullptr;
1475 assert(!cs);
1476
1477 assert(!s.empty());
1478 {
1479 std::map<StringType, int> m;
1480 cs = s;
1481 m[cs] = 5;
1482 assert(m[cs] == 5);
1483 }
1484 {
1485 std::unordered_map<StringType, int> m;
1486 cs = s;
1487 m[cs] = 5;
1488 assert(m[cs] == 5);
1489 }
1490
1491 #if __cplusplus >= 201703L
1492 // C++17 specifics
1493 cs = std::nullopt;
1494 std::optional<std::string> os;
1495 cs = os;
1496 assert(!cs);
1497 os = s;
1498 cs = os;
1499 assert(cs == s);
1500 os = std::nullopt;
1501 os = cs;
1502 assert(os && os.value() == s);
1503 cs = nullptr;
1504 assert(!cs);
1505 os = cs.opt_();
1506 assert(!os);
1507
1508 cs = STR;
1509 assert(cs.find_first_of("b") == 1);
1510 assert(cs.find_first_of('b') == 1);
1511 #endif
1512 }
1513
1514 } // namespace string_helpers
1515
1516 namespace gi
1517 {
1518 namespace convert
1519 {
1520 template<typename Transfer>
1521 struct converter<string_helpers::MyViewCustom, detail::cstr<Transfer>>
1522 : public converter_base<string_helpers::MyViewCustom,
1523 detail::cstr<Transfer>>
1524 {
1525 static detail::cstr<Transfer> convert(const string_helpers::MyViewCustom &v)
1526 {
1527 (void)v;
1528 return nullptr;
1529 }
1530 };
1531 } // namespace convert
1532 } // namespace gi
1533
1534 void
1535 test_string()
1536 {
1537 using namespace string_helpers;
1538 { // generic
1539 check_string<cstr>();
1540 check_string<cstrv>();
1541 }
1542
1543 std::string s("xy");
1544 { // string only
1545 cstr cs(s, 1, 1);
1546 {
1547 MyView cv;
1548 cstr cs(cv);
1549 assert(!cs);
1550 }
1551 {
1552 auto *s = (char *)g_malloc0(5);
1553 cstr cs(s, transfer_full);
1554 assert(cs.data() == s);
1555 }
1556 // ops
1557 cs = s;
1558 cs = cs + s;
1559 assert(cs == s + s);
1560 assert(cs.size() == 4);
1561 cs = s;
1562 cs = s + cs;
1563 assert(cs == s + s);
1564 cs.pop_back();
1565 assert(cs.size() == 3);
1566 cs.push_back(s.back());
1567 assert(cs.size() == 4);
1568 assert(cs == s + s);
1569 cs = cs.substr(0, 2);
1570 assert(cs == s);
1571 cs += s;
1572 assert(cs == s + s);
1573 cs.assign(2, 'x');
1574 assert(cs == "xx");
1575 cs = 'y' + cs;
1576 assert(cs == "yxx");
1577 cs = cs + 'y';
1578 assert(cs == "yxxy");
1579 cs.clear();
1580 assert(!cs);
1581 cs = s;
1582 auto sp = unwrap(std::move(cs), transfer_full);
1583 assert(s == sp);
1584 assert(sp);
1585 assert(!cs);
1586 cs = cstr{sp, transfer_full};
1587 assert(cs.data() == sp);
1588 sp = unwrap(std::move(cs), transfer_full);
1589 assert(!cs);
1590 cs = wrap(sp, transfer_full);
1591 assert(cs.data() == sp);
1592
1593 // C++17 string view
1594 #if __cplusplus >= 201703L
1595 std::string_view sv(s);
1596 cs = sv;
1597 assert(cs.data() != sv.data());
1598 assert(sv == cs);
1599 assert(cs == sv);
1600 sv = "zz";
1601 assert(cs <= sv);
1602 assert(cs < sv);
1603 assert(sv > cs);
1604 assert(sv >= cs);
1605 assert(sv != cs);
1606 #endif
1607 }
1608
1609 { // view only
1610 cstrv cv(s);
1611 assert(cv.data() == s.data());
1612 cv.remove_prefix(1);
1613 assert(cv.size() == s.size() - 1);
1614 assert(cv == s.substr(1));
1615 const cstrv ccv(s);
1616 std::string ns(ccv);
1617 }
1618
1619 { // combination
1620 cstr cs("blah");
1621 cstrv cv(cs);
1622 assert(cv.data() == cs.data());
1623 cv = cs;
1624 assert(cv.data() == cs.data());
1625 cs = cv;
1626 assert(cs.data() != cv.data());
1627 }
1628 }
1629
1630 void
1631 test_exception()
1632 {
1633 static_assert(traits::is_gboxed<GLib::Error>::value, "");
1634
1635 GQuark domain = g_quark_from_string("test-domain");
1636 const char *msg = "exception_test";
1637 const int code = 42;
1638 GError *err = g_error_new_literal(domain, code, msg);
1639 auto w = wrap(err, transfer_full);
1640 assert(w.matches(domain, code));
1641 auto what = w.what();
1642 assert(strstr(what, msg) != NULL);
1643
1644 auto wr = wrap(err, transfer_none);
1645 assert(wr.matches(domain, code));
1646
1647 GLib::Error e{std::move(w)};
1648 assert(!w);
1649 assert(e.matches(domain, code));
1650
1651 check_error(nullptr);
1652 err = g_error_new_literal(domain, code, msg);
1653 detail::make_unexpected(err);
1654 bool value = true;
1655 auto r = make_result<bool>(value, nullptr);
1656 static_assert(detail::is_result<decltype(r)>::value, "");
1657 static_assert(!detail::is_result<bool>::value, "");
1658 // make sure we have bool result
1659 auto s = expect(std::move(r));
1660 static_assert(std::is_same<bool, decltype(s)>::value, "");
1661 auto &e2 = expect(e);
1662 assert(&e2 == &e);
1663 }
1664
1665 void
1666 test_enumflag()
1667 {
1668 const char *name = "EnumValue1";
1669 const char *nick = "v1";
1670 auto v = CppEnum::VALUE_1;
1671
1672 auto w = value_info(v);
1673 auto w1 = EnumValue<CppEnum>::get_by_name(name);
1674 auto w2 = EnumValue<CppEnum>::get_by_nick(nick);
1675 assert(w == w1);
1676 assert(w1 == w2);
1677 assert(w1.value_name() == name);
1678 assert(w1.value_nick() == nick);
1679
1680 // convenient operations
1681 auto f = CppFlags::VALUE_0 | CppFlags::VALUE_1;
1682 f = f & CppFlags::VALUE_1;
1683 f = ~f;
1684 f ^= CppFlags::VALUE_0;
1685 }
1686
1687 typedef int(CCallback)(int, float);
1688 typedef detail::callback<int(int, float), transfer_none_t,
1689 std::tuple<transfer_none_t, transfer_none_t>>
1690 CppCallback;
1691
1692 void
1693 test_callback()
1694 {
1695 { // argument selection helper
1696 auto f = [](int a, int b) { return a - b; };
1697 using Y = detail::args_index<1, 0>;
1698 auto rr = detail::apply_with_args<Y>(f, 7, 3, 5);
1699 assert(rr == -4);
1700 }
1701 {
1702 const char *t = "";
1703 auto f = [](const char *s) -> decltype(auto) { return *s; };
1704 static_assert(std::is_reference<decltype(f(t))>::value, "");
1705 using Y = detail::args_index<0>;
1706 auto &rr = detail::apply_with_args<Y>(f, t);
1707 assert(&rr == t);
1708 }
1709 {
1710 using T = typename detail::arg_traits<transfer_none_t>::transfer_type;
1711 static_assert(std::is_same<transfer_none_t, T>::value, "");
1712 static_assert(detail::is_simple_cb<std::tuple<transfer_none_t>>::value, "");
1713 using TC = std::tuple<
1714 detail::arg_info<transfer_full_t, false, void, detail::args_index<0>>,
1715 detail::arg_info<transfer_full_t, false, void, detail::args_index<1>>>;
1716 static_assert(!detail::is_simple_cb<TC>::value, "");
1717 }
1718
1719 { // exception helpers
1720 detail::report_exception<true>(std::runtime_error("fail"), 5, nullptr);
1721 detail::report_exception(
1722 std::runtime_error("fail"), 5, (::GError **)(nullptr));
1723 GError *error = nullptr;
1724 detail::report_exception(std::runtime_error("fail"), 5, &error);
1725 assert(error);
1726 g_error_free(error);
1727 error = nullptr;
1728 }
1729
1730 int calls = 0;
1731
1732 auto l = [&](int a, float b) {
1733 ++calls;
1734 return a * b;
1735 };
1736 detail::transform_callback_wrapper<int(int, float)>::with_transfer<false,
1737 transfer_full_t, transfer_none_t, transfer_none_t>
1738 x{l};
1739 x.wrapper(1, 2, &x);
1740 assert(calls == 1);
1741 x.take_data(std::make_shared<int>(0));
1742
1743 auto m = [&](int a, CBoxedExample /*b*/, int &x, int *y) {
1744 x = 1;
1745 *y = 2;
1746 ++calls;
1747 return a;
1748 };
1749 detail::transform_callback_wrapper<void(
1750 int, CBoxedExample, int &, int *)>::with_transfer<false, transfer_full_t,
1751 transfer_full_t, transfer_full_t, transfer_none_t, transfer_none_t>
1752 y{m};
1753 int p = 0, q = 0;
1754 y.wrapper(1, gi_cpp_cbexample_new(), &p, &q, &y);
1755 assert(calls == 2);
1756 assert(p == 1);
1757 assert(q == 2);
1758
1759 auto w = wrap(gi_cpp_example_new(), transfer_full);
1760 auto w2 = wrap(gi_cpp_example_new(), transfer_full);
1761 char str[] = "blah";
1762 auto n = [&](GBoxedExample /*b*/, Derived &ob, CppEnum *e, gi::cstring_v &s,
1763 gpointer &vb, GLib::Error * /*error*/) {
1764 ++calls;
1765 if (e)
1766 *e = CppEnum::VALUE_1;
1767 ob = w2;
1768 s = str;
1769 vb = str;
1770 return w;
1771 };
1772 detail::transform_callback_wrapper<Derived(GBoxedExample, Derived &,
1773 CppEnum *, gi::cstring_v &, gpointer &, GLib::Error *)>::
1774 with_transfer<false, transfer_full_t, transfer_full_t, transfer_full_t,
1775 transfer_none_t, transfer_none_t, transfer_none_t, transfer_full_t>
1776 z{n};
1777 CDerived *ob{};
1778 CEnum e{};
1779 char *s{};
1780 gpointer vb{};
1781 auto r = z.wrapper(gi_cpp_gbexample_new(), &ob, &e, &s, &vb, nullptr, &z);
1782 assert(calls == 3);
1783 assert(r == w.gobj_());
1784 assert(refcount(r) == 2);
1785 g_object_unref(r);
1786 assert(e == CEnum::ENUM_VALUE_1);
1787 assert(s == str);
1788 assert(vb == s);
1789 assert(ob == w2.gobj_());
1790 assert(refcount(ob) == 2);
1791 g_object_unref(ob);
1792
1793 // extended case
1794 {
1795 using CLocalCallback_CF_CType = int (*)(int, gpointer);
1796 struct CLocalCallback_CF_Trait
1797 {
1798 using handler_cb_type = CLocalCallback_CF_CType;
1799 static auto handler(int x, handler_cb_type cb, gpointer ud)
1800 {
1801 return cb(x, ud);
1802 };
1803 };
1804 using CppLocalCallback = detail::callback<int(int), transfer_none_t,
1805 std::tuple<transfer_none_t>>;
1806 int xx = 0;
1807 auto nx = [&](GBoxedExample /*b*/, Derived &ob, CppEnum *e, int x,
1808 CppLocalCallback cb, GLib::Error * /*error*/) {
1809 ++calls;
1810 xx = x;
1811 ob = w2;
1812 if (e)
1813 *e = CppEnum::VALUE_1;
1814 auto r = cb(x);
1815 // supplied cb below is identity function
1816 assert(r == x);
1817 return w;
1818 };
1819 detail::transform_callback_wrapper<Derived(GBoxedExample, Derived &,
1820 CppEnum *, int, CppLocalCallback,
1821 GLib::Error *),
1822 CDerived *(GBExample *, CDerived **, CEnum *, int,
1823 CLocalCallback_CF_CType, gpointer,
1824 ::GError **)>::with_transfer<false, transfer_none_t,
1825 detail::arg_info<transfer_full_t, false, void, detail::args_index<0>>,
1826 detail::arg_info<transfer_none_t, false, void, detail::args_index<1>>,
1827 detail::arg_info<transfer_full_t, false, void, detail::args_index<2>>,
1828 detail::arg_info<transfer_full_t, false, void, detail::args_index<3>>,
1829 detail::arg_info<transfer_full_t, false, CLocalCallback_CF_Trait,
1830 detail::args_index<4, 5>>,
1831 detail::arg_info<transfer_full_t, false, void, detail::args_index<6>>>
1832 zx{nx};
1833 CDerived *obx{};
1834 CEnum ex{};
1835 auto cb = [](int v, gpointer ud) {
1836 if (ud)
1837 *(int *)(ud) = v;
1838 return v;
1839 };
1840 int oi = 0;
1841 auto r = zx.wrapper(
1842 gi_cpp_gbexample_new(), &obx, &ex, 5, cb, (gpointer)&oi, nullptr, &zx);
1843 // cpp callback should have been called
1844 assert(calls == 4);
1845 assert(xx == 5);
1846 assert(r == w.gobj_());
1847 assert(obx != nullptr);
1848 assert(obx == w2.gobj_());
1849 assert(e == CEnum::ENUM_VALUE_1);
1850 // which in turn should have called the supplied callback
1851 assert(oi == 5);
1852 //
1853 // destroy_notify parameter
1854 bool called = false;
1855 int INTVAL = 7;
1856 auto ndx = [&](CppLocalCallback cb) {
1857 called = true;
1858 int x = INTVAL;
1859 auto r = cb(x);
1860 // supplied cb below is identity function
1861 assert(r == x);
1862 return w;
1863 };
1864 detail::transform_callback_wrapper<void(CppLocalCallback),
1865 void(CLocalCallback_CF_CType, gpointer,
1866 GDestroyNotify)>::with_transfer<false, transfer_none_t,
1867 detail::arg_info<transfer_full_t, false, CLocalCallback_CF_Trait,
1868 detail::args_index<0, 1, 2>>>
1869 zdx{ndx};
1870 auto dn = [](gpointer ud) { *(int *)ud = 0; };
1871 zdx.wrapper(cb, (gpointer)&oi, dn, &zdx);
1872 assert(called);
1873 // destroy notify called last
1874 assert(oi == 0);
1875 //
1876 // void return case, and sized array out
1877 char *sx{};
1878 gpointer vbx{};
1879 const int V = 56;
1880 int *a_data{};
1881 int a_size = 0;
1882 using TestColType = gi::Collection<gi::DSpan, int, transfer_full_t>;
1883 auto nnx = [&](gi::cstring_v &s, gpointer &vb, CppLocalCallback cb,
1884 TestColType &d) {
1885 ++calls;
1886 s = str;
1887 vb = str;
1888 auto r = cb(V);
1889 // supplied cb below is identity function
1890 assert(r == V);
1891 d.push_back(4);
1892 };
1893 detail::transform_callback_wrapper<void(gi::cstring_v & s, gpointer & vb,
1894 CppLocalCallback, TestColType &),
1895 void(char **, gpointer *, CLocalCallback_CF_CType, gpointer, int **,
1896 int *)>::with_transfer<false, transfer_full_t,
1897 detail::arg_info<transfer_none_t, false, void, detail::args_index<0>>,
1898 detail::arg_info<transfer_none_t, false, void, detail::args_index<1>>,
1899 detail::arg_info<transfer_full_t, false, CLocalCallback_CF_Trait,
1900 detail::args_index<2, 3>>,
1901 detail::arg_info<transfer_full_t, false, void,
1902 detail::args_index<4, 5>>>
1903 zzx{nnx};
1904 zzx.wrapper(&sx, &vbx, cb, (gpointer)&oi, &a_data, &a_size, &zzx);
1905 assert(calls == 5);
1906 assert(sx == str);
1907 assert(vbx == str);
1908 // cpp callback should have been called
1909 assert(oi == V);
1910 // handle returned array
1911 assert(a_size == 1);
1912 assert(*a_data == 4);
1913 g_free(a_data);
1914 }
1915
1916 { // compilation checks
1917 const CppCallback cppcb(l);
1918 auto uw{unwrap(cppcb, gi::scope_async)};
1919 delete uw;
1920 auto uw2{unwrap(cppcb, gi::scope_notified)};
1921 delete uw2;
1922 }
1923 }
1924
1925 void
1926 test_value()
1927 {
1928 using GObject_::Value;
1929 static_assert(traits::gtype<Value>::value, "");
1930 assert(g_type_is_a(GI_CPP_TYPE_ENUM, G_TYPE_ENUM));
1931 assert(g_type_is_a(GI_CPP_TYPE_FLAGS, G_TYPE_FLAGS));
1932
1933 // detail helper
1934 {
1935 detail::Value v(5);
1936 auto vs = detail::transform_value<std::string>(&v);
1937 assert(vs == "5");
1938 detail::Value v2("ab");
1939 auto w = detail::get_value<std::string>(&v2);
1940 assert(w == "ab");
1941 }
1942
1943 // main Value
1944 {
1945 Value v;
1946 v.init(G_TYPE_STRING);
1947 v.set_value("ab");
1948 auto w = v.get_value<std::string>();
1949 assert(w == "ab");
1950 }
1951 {
1952 Value v(0);
1953 auto w = v.get_value<int>();
1954 assert(w == 0);
1955 v.set_value(5);
1956 w = v.get_value<int>();
1957 assert(w == 5);
1958 }
1959 {
1960 Value v{std::string()};
1961 auto w = v.get_value<std::string>();
1962 assert(w.empty());
1963 }
1964 {
1965 Value v((double)1.0);
1966 auto w = v.get_value<double>();
1967 assert(w == 1.0);
1968 }
1969 {
1970 Value v('a');
1971 auto w = v.get_value<char>();
1972 assert(w == 'a');
1973 }
1974 {
1975 CDerived *ob = gi_cpp_example_new();
1976 auto wob = wrap(ob, transfer_none);
1977 Value v(wob);
1978 assert(refcount(ob) == 3);
1979 auto w2 = v.get_value<Derived>();
1980 assert(w2 == wob);
1981 assert(refcount(ob) == 4);
1982 auto wr = wrap(v.gobj_(), transfer_none);
1983 assert(wr == v);
1984 assert(refcount(ob) == 4);
1985 g_object_unref(ob);
1986 // others clean up by magic
1987 }
1988 {
1989 Value v(GBoxedExample{});
1990 auto w = v.get_value<GBoxedExample>();
1991 assert(w.gobj_() == nullptr);
1992 auto wr = v.get_value<GBoxedExampleRef>();
1993 assert(wr.gobj_() == nullptr);
1994 }
1995 {
1996 Value v(CppEnum::VALUE_0);
1997 auto w = v.get_value<CppEnum>();
1998 assert(w == CppEnum::VALUE_0);
1999 Value v1(v.copy_());
2000 assert(v != v1);
2001 }
2002 {
2003 Value v(CppFlags::VALUE_1);
2004 auto w = v.get_value<CppFlags>();
2005 assert(w == CppFlags::VALUE_1);
2006 Value v1(v.copy_());
2007 assert(v != v1);
2008 }
2009 { // test function; auto conversion
2010 auto tf = [](Value v, GType t) { assert(G_VALUE_TYPE(v.gobj_()) == t); };
2011 tf(5, G_TYPE_INT);
2012 tf("ab", G_TYPE_STRING);
2013 tf(GBoxedExample(), GI_CPP_TYPE_BOXED_EXAMPLE);
2014 }
2015 { // wrapping
2016 GValue *v = (GValue *)1;
2017 auto w = wrap(v, transfer_none);
2018 auto v1 = unwrap(w, transfer_none);
2019 assert(v1 = v);
2020 }
2021 }
2022
2023 void
2024 test_property()
2025 {
2026 CDerived *ob = gi_cpp_example_new();
2027 // take ownership
2028 Derived w = wrap(ob, transfer_full);
2029
2030 // manual
2031 w.set_property(NAME_NUMBER, 5);
2032 assert(w.get_property<int>(NAME_NUMBER) == 5);
2033 w.set_property(NAME_PRESENT, true);
2034 assert(w.get_property<bool>(NAME_PRESENT) == true);
2035 const char *str = "value";
2036 w.set_property(NAME_DATA, str);
2037 assert(w.get_property<std::string>(NAME_DATA) == str);
2038 w.set_property(NAME_OBJECT, w);
2039 auto w2 = w.get_property<Derived>(NAME_OBJECT);
2040 assert(w2 == w);
2041 assert(refcount(ob) == 3);
2042 // remove cycle ref held within ob
2043 w.set_property(NAME_OBJECT, Derived());
2044 assert(refcount(ob) == 2);
2045 w.set_property(NAME_ENUM, CppEnum::VALUE_1);
2046 assert(w.get_property<CppEnum>(NAME_ENUM) == CppEnum::VALUE_1);
2047
2048 // multiple props
2049 double fnum = 5.2;
2050 w.set_properties(NAME_NUMBER, 10, NAME_FNUMBER, fnum, NAME_PRESENT, FALSE);
2051 assert(w.get_property<int>(NAME_NUMBER) == 10);
2052 assert(w.get_property<double>(NAME_FNUMBER) == fnum);
2053 assert(w.get_property<bool>(NAME_PRESENT) == false);
2054
2055 // generic value
2056 #ifdef GI_GOBJECT_PROPERTY_VALUE
2057 w.get_property(NAME_NUMBER);
2058 #endif
2059
2060 // via proxy
2061 fnum = 6.2;
2062 Derived w3 = wrap(gi_cpp_example_new(), transfer_full);
2063 w.property_number().set(7);
2064 w.property_fnumber().set(fnum);
2065 w.property_data().set(str);
2066 w.property_object().set(w3);
2067 w.property_present().set(true);
2068 w.property_choice().set(CppEnum::VALUE_0);
2069 w.property_flags().set(CppFlags::VALUE_0);
2070 // boxed property
2071 GQuark domain = g_quark_from_string("test-domain");
2072 auto error = GLib::Error::new_literal(domain, 1, "msg");
2073 w.property_error().set(error.copy());
2074 w.property_error().set(std::move(error));
2075
2076 const Derived cw = w;
2077 assert(cw.property_number().get() == 7);
2078 assert(cw.property_fnumber().get() == fnum);
2079 assert(cw.property_data().get() == str);
2080 assert(cw.property_object().get() == w3);
2081 assert(cw.property_data().get() == str);
2082 assert(cw.property_choice().get() == CppEnum::VALUE_0);
2083 assert(cw.property_flags().get() == CppFlags::VALUE_0);
2084
2085 // property queries
2086 auto pspec = cw.find_property(NAME_NUMBER);
2087 assert(pspec);
2088 assert(pspec.get_name() == NAME_NUMBER);
2089
2090 auto pspecs = cw.list_properties();
2091 assert(pspecs.size() == PROP_LAST);
2092 }
2093
2094 void
2095 test_signal()
2096 {
2097 // example values
2098 double v_d = 2.7;
2099 int v_i = 4;
2100 std::string v_s = "values";
2101 bool v_b = true;
2102 CppEnum v_e = CppEnum::VALUE_1;
2103 CppFlags v_f = CppFlags::VALUE_1;
2104 Derived v_o = wrap(gi_cpp_example_new(), transfer_full);
2105
2106 // object to signal on
2107 CDerived *ob = gi_cpp_example_new();
2108 // take ownership
2109 Derived w = wrap(ob, transfer_full);
2110
2111 // lambda callbacks
2112 int recv = 0;
2113 int ret = 7;
2114 auto l1 = [&](Derived src, GObject_::Object o, bool b, bool c,
2115 const std::string &s) -> int {
2116 assert(src == w);
2117 assert(o == v_o);
2118 assert(s == v_s);
2119 assert(b == v_b);
2120 assert(c == !b);
2121 ++recv;
2122 return ret;
2123 };
2124 w.signal_to_int().connect(l1);
2125 auto r = w.signal_to_int().emit(v_o, v_b, !v_b, v_s);
2126 assert(recv == 1);
2127 assert(r == ret);
2128
2129 // another signal
2130 auto l2 = [&](Derived src, int i, gint64 ll) -> std::string {
2131 assert(src == w);
2132 ++recv;
2133 return std::to_string(i + ll);
2134 };
2135 w.signal_to_string().connect(l2);
2136 gint64 ll = 4;
2137 auto sr = w.signal_to_string().emit(v_i, ll);
2138 assert(recv == 2);
2139 assert(std::stoi(sr) == v_i + ll);
2140
2141 // and another
2142 auto l3 = w.signal_to_void().slot(
2143 [&](Derived src, double d, CppEnum e, CppFlags f) {
2144 assert(src == w);
2145 assert(d == v_d);
2146 assert(e == v_e);
2147 assert(f == v_f);
2148 ++recv;
2149 });
2150 auto id = w.signal_to_void().connect(l3);
2151 w.signal_to_void().emit(v_d, v_e, v_f);
2152 assert(recv == 3);
2153
2154 auto conn = make_connection(id, l3, w);
2155 assert(conn.connected());
2156 {
2157 // safe to disconnect twice (or attempt so)
2158 GObject_::SignalScopedConnection sconn(conn), sconn2(conn);
2159 assert(sconn.connected());
2160 assert(sconn2.connected());
2161 }
2162
2163 assert(!conn.connected());
2164 w.signal_to_void().emit(v_d, v_e, v_f);
2165 assert(recv == 3);
2166
2167 // signal collection and output argument
2168 auto l4 = [&](Derived src, unsigned &o, auto pa, auto la) {
2169 assert(src == w);
2170 o = pa.size() + std::distance(la.begin(), la.end());
2171 };
2172 gi::Collection<::GPtrArray, char *, gi::transfer_full_t> pa;
2173 gi::Collection<::GSList, char *, gi::transfer_full_t> la;
2174 pa.push_back("blah");
2175 la = pa;
2176 assert(!la.empty());
2177 la.push_back("foo");
2178 w.signal_to_output_int().connect(l4);
2179 unsigned result = 0;
2180 w.signal_to_output_int().emit(result, pa, nullptr);
2181 assert(result == pa.size());
2182 w.signal_to_output_int().emit(result, nullptr, la);
2183 assert(result == 2);
2184
2185 // assert exception helper
2186 auto assert_exc = [](const std::function<void()> &func) {
2187 bool exc = false;
2188 try {
2189 func();
2190 } catch (std::exception &) {
2191 exc = true;
2192 }
2193 assert(exc);
2194 };
2195
2196 // check connect check
2197 assert_exc(
2198 [&]() { w.connect<std::string(Derived, int, gint64)>("to_void", l2); });
2199 // check property value conversion
2200 assert_exc([&]() { w.set_property<std::string>(NAME_OBJECT, "blah"); });
2201 }
2202
2203 // ExampleInterface
2204
2205 class ExampleInterface : public gi::InterfaceBase
2206 {
2207 public:
2208 typedef GICppExampleItf BaseObjectType;
2209 static GType get_type_() G_GNUC_CONST
2210 {
2211 return gi_cpp_example_interface_get_type();
2212 }
2213 };
2214
2215 class ExampleInterfaceDef
2216 {
2217 typedef ExampleInterfaceDef self;
2218
2219 public:
2220 typedef ExampleInterface instance_type;
2221 typedef GICppExampleInterface interface_type;
2222
2223 using GI_MEMBER_CHECK_CONFLICT(vmethod) = self;
2224 using GI_MEMBER_CHECK_CONFLICT(imethod) = self;
2225
2226 struct TypeInitData;
2227
2228 protected:
2229 ~ExampleInterfaceDef() = default;
2230
2231 static void interface_init(gpointer iface, gpointer /*data*/);
2232
2233 virtual int vmethod_(int a) = 0;
2234 virtual int imethod_(int a) = 0;
2235 };
2236
2237 using ExampleInterfaceImpl = gi::detail::InterfaceImpl<ExampleInterfaceDef>;
2238
2239 class ExampleInterfaceClassImpl
2240 : public gi::detail::InterfaceClassImpl<ExampleInterfaceImpl>
2241 {
2242 friend class ExampleInterfaceDef;
2243 typedef ExampleInterfaceImpl self;
2244 typedef gi::detail::InterfaceClassImpl<ExampleInterfaceImpl> super;
2245
2246 protected:
2247 using super::super;
2248
2249 int vmethod_(int a) override
2250 {
2251 auto _struct = get_struct_();
2252 return _struct->vmethod(this->gobj_(), a);
2253 }
2254
2255 int imethod_(int a) override
2256 {
2257 auto _struct = get_struct_();
2258 return _struct->imethod(this->gobj_(), a);
2259 }
2260 };
2261
2262 struct ExampleInterfaceDef::TypeInitData
2263 {
2264 GI_MEMBER_DEFINE(ExampleInterfaceClassImpl, vmethod)
2265 GI_MEMBER_DEFINE(ExampleInterfaceClassImpl, imethod)
2266
2267 template<typename SubClass>
2268 constexpr static TypeInitData factory()
2269 {
2270 using DefData = detail::DefinitionData<SubClass, TypeInitData>;
2271 return {GI_MEMBER_HAS_DEFINITION(SubClass, DefData, vmethod),
2272 GI_MEMBER_HAS_DEFINITION(SubClass, DefData, imethod)};
2273 }
2274 };
2275
2276 void
2277 ExampleInterfaceDef::interface_init(gpointer iface, gpointer data)
2278 {
2279 auto init_data = GI_MEMBER_INIT_DATA(TypeInitData, data);
2280 auto itf = (interface_type *)(iface);
2281
2282 if (init_data.vmethod)
2283 itf->vmethod = gi::detail::method_wrapper<self, int (*)(int),
2284 transfer_full_t, std::tuple<transfer_none_t>>::wrapper<&self::vmethod_>;
2285 if (init_data.imethod)
2286 itf->imethod = gi::detail::method_wrapper<self, int (*)(int),
2287 transfer_full_t, std::tuple<transfer_none_t>>::wrapper<&self::imethod_>;
2288 }
2289
2290 // PropertyInterface
2291 class PropertyInterface : public gi::InterfaceBase
2292 {
2293 public:
2294 typedef GICppPropertyItf BaseObjectType;
2295 static GType get_type_() G_GNUC_CONST
2296 {
2297 return gi_cpp_property_interface_get_type();
2298 }
2299 };
2300
2301 class PropertyInterfaceDef
2302 {
2303 typedef PropertyInterfaceDef self;
2304
2305 public:
2306 typedef PropertyInterface instance_type;
2307 typedef GICppPropertyInterface interface_type;
2308
2309 protected:
2310 static void interface_init(gpointer iface, gpointer /*data*/)
2311 {
2312 auto itf = (interface_type *)(iface);
2313 (void)itf;
2314 }
2315 };
2316
2317 using PropertyInterfaceImpl = gi::detail::InterfaceImpl<PropertyInterfaceDef>;
2318
2319 class PropertyInterfaceClassImpl
2320 : public gi::detail::InterfaceClassImpl<PropertyInterfaceImpl>
2321 {
2322 typedef PropertyInterfaceImpl self;
2323 typedef gi::detail::InterfaceClassImpl<PropertyInterfaceImpl> super;
2324
2325 protected:
2326 using super::super;
2327 };
2328
2329 class DerivedClassDef
2330 {
2331 typedef DerivedClassDef self;
2332
2333 public:
2334 typedef Derived instance_type;
2335 typedef GICppExampleClass class_type;
2336
2337 struct TypeInitData;
2338
2339 using GI_MEMBER_CHECK_CONFLICT(vmethod) = self;
2340 using GI_MEMBER_CHECK_CONFLICT(cmethod) = self;
2341
2342 protected:
2343 ~DerivedClassDef() = default;
2344
2345 static void class_init(gpointer g_class, gpointer class_data_factory);
2346
2347 virtual int vmethod_(int a, int b) = 0;
2348 virtual int cmethod_(int a, int b) = 0;
2349 };
2350
2351 GI_CLASS_IMPL_BEGIN
2352
2353 class DerivedClass
2354 : public gi::detail::ClassTemplate<DerivedClassDef,
2355 GObject_::impl::internal::ObjectClass, ExampleInterfaceClassImpl>
2356 {
2357 friend class DerivedClassDef;
2358 typedef DerivedClass self;
2359 typedef gi::detail::ClassTemplate<DerivedClassDef,
2360 GObject_::impl::internal::ObjectClass, ExampleInterfaceClassImpl>
2361 super;
2362
2363 public:
2364 typedef ExampleInterfaceClassImpl ExampleInterface_type;
2365
2366 private:
2367 // make local helpers private
2368 using super::get_struct_;
2369 using super::gobj_;
2370
2371 protected:
2372 GI_DISABLE_DEPRECATED_WARN_BEGIN
2373 using super::super;
2374 GI_DISABLE_DEPRECATED_WARN_END
2375
2376 virtual int vmethod_(int a, int b) override
2377 {
2378 auto _struct = get_struct_();
2379 return _struct->vmethod(gobj_(), a, b);
2380 }
2381
2382 virtual int cmethod_(int a, int b) override
2383 {
2384 auto _struct = get_struct_();
2385 return _struct->cmethod(gobj_(), a, b);
2386 }
2387 };
2388
2389 struct DerivedClassDef::TypeInitData
2390 {
2391 GI_MEMBER_DEFINE(DerivedClass, vmethod)
2392 GI_MEMBER_DEFINE(DerivedClass, cmethod)
2393
2394 template<typename SubClass>
2395 constexpr static TypeInitData factory()
2396 {
2397 using DefData = detail::DefinitionData<SubClass, TypeInitData>;
2398 return {GI_MEMBER_HAS_DEFINITION(SubClass, DefData, vmethod),
2399 GI_MEMBER_HAS_DEFINITION(SubClass, DefData, cmethod)};
2400 }
2401 };
2402
2403 void
2404 DerivedClassDef::class_init(gpointer g_class, gpointer class_data_factory)
2405 {
2406 auto class_data = GI_MEMBER_INIT_DATA(TypeInitData, class_data_factory);
2407 GICppExampleClass *klass = (GICppExampleClass *)g_class;
2408
2409 if (class_data.vmethod)
2410 klass->vmethod = gi::detail::method_wrapper<self, int (*)(int, int),
2411 transfer_full_t,
2412 std::tuple<transfer_none_t, transfer_none_t>>::wrapper<&self::vmethod_>;
2413 if (class_data.cmethod)
2414 klass->cmethod = gi::detail::method_wrapper<self, int (*)(int, int),
2415 transfer_full_t,
2416 std::tuple<transfer_none_t, transfer_none_t>>::wrapper<&self::cmethod_>;
2417 // local compile check
2418 (void)gi::detail::method_wrapper<self, int (*)(int, int),
2419 std::nullptr_t>::wrapper<&self::cmethod_>;
2420 }
2421
2422 GI_CLASS_IMPL_END
2423
2424 using DerivedImpl = gi::detail::ObjectImpl<Derived, DerivedClass>;
2425
2426 template<typename T>
2427 class custom_property : public gi::property<T>
2428 {
2429 public:
2430 using super = gi::property<T>;
2431 using super::super;
2432
2433 using handler = std::function<void(const T &)>;
2434 handler handler_;
2435
2436 void set_property(const GValue *value) override
2437 {
2438 super::set_property(value);
2439 if (handler_)
2440 handler_(this->get_value());
2441 }
2442 };
2443
2444 class UserDerived : public DerivedImpl
2445 {
2446 public:
2447 // possible conflict for vmethod
2448 // so we specify the override situation explicity
2449 struct DefinitionData
2450 {
2451 GI_DEFINES_MEMBER(DerivedClassDef, vmethod, true)
2452 GI_DEFINES_MEMBER(ExampleInterfaceDef, vmethod, true)
2453 };
2454
2455 UserDerived()
2456 : DerivedImpl(this), prop_int_set(this, "prop_int_set", "prop_int_set",
2457 "prop_int_set", 0, 10, 0),
2458 prop_bool_override(this, NAME_PRESENT)
2459 {
2460 // check detection of method definitions
2461 constexpr auto class_def =
2462 DerivedClassDef::TypeInitData::factory<UserDerived>();
2463 static_assert(class_def.vmethod.value, "");
2464 static_assert(!class_def.cmethod.value, "");
2465 constexpr auto itf_def =
2466 ExampleInterfaceDef::TypeInitData::factory<UserDerived>();
2467 static_assert(itf_def.vmethod.value, "");
2468 static_assert(!itf_def.imethod.value, "");
2469 }
2470
2471 int vmethod_(int a, int b) override { return a * b; }
2472
2473 int pvmethod(int a, int b) { return DerivedImpl::vmethod_(a, b); }
2474
2475 int vmethod_(int a) override { return 2 * a; }
2476
2477 int pivmethod(int a) { return ExampleInterface_type::vmethod_(a); }
2478
2479 custom_property<int> prop_int_set;
2480 custom_property<bool> prop_bool_override;
2481 };
2482
2483 class UserDerived2 : public DerivedImpl
2484 {
2485 public:
2486 // possible conflict for vmethod
2487 // so we specify the override situation explicity
2488 struct DefinitionData
2489 {
2490 GI_DEFINES_MEMBER(DerivedClassDef, vmethod, true)
2491 GI_DEFINES_MEMBER(ExampleInterfaceDef, vmethod, false)
2492 };
2493
2494 UserDerived2() : DerivedImpl(this)
2495 {
2496 // check detection of method definitions
2497 constexpr auto x = DerivedClassDef::TypeInitData::factory<UserDerived2>();
2498 static_assert(x.vmethod.value, "");
2499 static_assert(x.cmethod.value, "");
2500 constexpr auto itf_def =
2501 ExampleInterfaceDef::TypeInitData::factory<UserDerived2>();
2502 static_assert(!itf_def.vmethod.value, "");
2503 static_assert(itf_def.imethod.value, "");
2504 }
2505
2506 int vmethod_(int a, int b) override { return a * b; }
2507
2508 int cmethod_(int a, int b) override { return a * b; }
2509
2510 int imethod_(int a) override { return 5 * a; }
2511 };
2512
2513 GI_DISABLE_DEPRECATED_WARN_BEGIN
2514 class OldUserDerived : public DerivedImpl
2515 {
2516 public:
2517 OldUserDerived() : DerivedImpl(typeid(*this)) {}
2518 };
2519 GI_DISABLE_DEPRECATED_WARN_END
2520
2521 static const int DEFAULT_PROP_INT = 7;
2522
2523 class UserObject : public ExampleInterfaceImpl,
2524 public PropertyInterfaceImpl,
2525 public GObject_::impl::ObjectImpl
2526 {
2527 public:
2528 UserObject()
2529 : ObjectImpl(this, {}, {{NAME_INUMBER, {&prop_itf_int, nullptr}}}),
2530 signal_demo_(this, "demo"), prop_itf_int(this, NAME_INUMBER),
2531 prop_int(
2532 this, "prop_int", "prop_int", "prop_int", 0, 10, DEFAULT_PROP_INT),
2533 prop_bool(this, "prop_bool", "prop_bool", "prop_bool", false),
2534 prop_str(this, "prop_str", "prop_str", "prop_str", ""),
2535 prop_object(this, "prop_object", "prop_object", "prop_object"),
2536 prop_enum(this, "prop_enum", "prop_enum", "prop_enum")
2537 {}
2538
2539 int vmethod_(int a) override { return 5 * a; }
2540 int imethod_(int a) override { return 7 * a; }
2541
2542 gi::signal<void(Object, int)> signal_demo_;
2543 gi::property<int> prop_itf_int;
2544 gi::property<int> prop_int;
2545 gi::property<bool> prop_bool;
2546 gi::property<std::string> prop_str;
2547 gi::property<Object> prop_object;
2548 gi::property<CppEnum> prop_enum;
2549 };
2550
2551 class UserCObject : public ExampleInterfaceImpl,
2552 public PropertyInterfaceImpl,
2553 public GObject_::impl::ObjectImpl
2554 {
2555 using self_type = UserCObject;
2556
2557 public:
2558 UserCObject(const InitData &id) : ObjectImpl(this, id) { assert(id); }
2559
2560 static GType get_type_()
2561 {
2562 return register_type_<UserCObject>("UserCObject", 0,
2563 {ExampleInterfaceImpl::interface_init_data<UserCObject>(),
2564 {PropertyInterfaceImpl::register_interface, nullptr}},
2565 {{&self_type::prop_itf_int, NAME_INUMBER},
2566 {&self_type::prop_int, "prop_int", "prop_int", "prop_int", 0, 10,
2567 DEFAULT_PROP_INT}},
2568 {{&self_type::signal_demo_, "demo"}});
2569 }
2570
2571 int vmethod_(int a) override { return 5 * a; }
2572 int imethod_(int a) override { return 7 * a; }
2573
2574 // details provided in get_type_
2575 gi::signal<void(Object, int)> signal_demo_{this, "demo"};
2576 gi::property<int> prop_itf_int{this, NAME_INUMBER};
2577 gi::property<int> prop_int{this, "prop_int"};
2578 };
2579
2580 void
2581 test_impl()
2582 {
2583 {
2584 // base object implements interface
2585 UserDerived u, v;
2586 assert(u.gobj_type_() == v.gobj_type_());
2587 assert(u.pvmethod(2, 3) == 5);
2588 auto klass =
2589 G_TYPE_INSTANCE_GET_CLASS(u.gobj_(), u.gobj_type(), GICppExampleClass);
2590 assert(klass->vmethod(u.gobj_(), 2, 3) == 6);
2591 // no cmethod
2592 assert(!klass->cmethod);
2593 // interface
2594 assert(u.pivmethod(4) == 6);
2595 auto iface = G_TYPE_INSTANCE_GET_INTERFACE(
2596 u.gobj_(), ExampleInterface::get_type_(), GICppExampleInterface);
2597 assert(iface->vmethod((GICppExampleItf *)u.gobj_(), 4) == 8);
2598 assert(!iface->imethod);
2599 // implemented here
2600 UserDerived2 w;
2601 assert(w.imethod_(2) == 10);
2602 // compilation check
2603 assert(u.gobj_klass()->vmethod);
2604 { // check custom property (on a non-Object derived class)
2605 int value = 0;
2606 auto &tp = u.prop_int_set;
2607 tp.handler_ = [&value](int setv) { value = setv; };
2608 auto proxy = tp.get_proxy();
2609 //
2610 const int NEW_VALUE = 7;
2611 proxy.set(NEW_VALUE);
2612 assert(value == NEW_VALUE);
2613 assert(tp.get_value() == NEW_VALUE);
2614 tp.handler_ = nullptr;
2615 }
2616 { // likewise on overriden property
2617 auto &tp = u.prop_bool_override;
2618 auto proxy = tp.get_proxy();
2619 proxy.set(false);
2620 assert(!proxy.get());
2621 bool value = false;
2622 tp.handler_ = [&value](bool setv) { value = setv; };
2623 const bool NEW_VALUE = true;
2624 //
2625 proxy.set(NEW_VALUE);
2626 assert(value == NEW_VALUE);
2627 assert(tp.get_value() == NEW_VALUE);
2628 tp.handler_ = nullptr;
2629 }
2630 }
2631
2632 {
2633 // vanilla object
2634 UserObject u, v;
2635 assert(u.gobj_type_() == v.gobj_type_());
2636 auto iface = G_TYPE_INSTANCE_GET_INTERFACE(
2637 u.gobj_(), ExampleInterface::get_type_(), GICppExampleInterface);
2638 assert(iface);
2639 assert(iface->vmethod((GICppExampleItf *)u.gobj_(), 4) == 20);
2640 // signal
2641 int i = 0, j = 5;
2642 u.signal_demo_.connect([&i](GObject_::Object, int in) -> void { i = in; });
2643 u.signal_demo_.emit(j);
2644 assert(i == j);
2645 // properties
2646 {
2647 // also check notification
2648 bool notified = false;
2649 auto proxy = u.prop_int.get_proxy();
2650 auto l = proxy.signal_notify().slot(
2651 [&notified](
2652 GObject_::Object, GObject_::ParamSpec) { notified = true; });
2653 GObject_::SignalScopedConnection conn =
2654 make_connection(proxy.signal_notify().connect(l), l, u);
2655 assert(u.prop_int == DEFAULT_PROP_INT);
2656 u.prop_int = j;
2657 assert(notified);
2658 notified = false;
2659 assert(proxy.get() == j);
2660 proxy.set(2 * j);
2661 assert(u.prop_int == 2 * j);
2662 assert(notified);
2663 }
2664 {
2665 auto proxy = u.prop_bool.get_proxy();
2666 u.prop_bool = true;
2667 assert(proxy.get() == true);
2668 proxy.set(false);
2669 assert(u.prop_bool == false);
2670 }
2671 {
2672 auto proxy = u.prop_str.get_proxy();
2673 const std::string strv = "value";
2674 u.prop_str = strv;
2675 assert(proxy.get() == strv);
2676 proxy.set(strv + strv);
2677 assert(u.prop_str.get_value() == (strv + strv));
2678 }
2679 {
2680 auto proxy = u.prop_object.get_proxy();
2681 u.prop_object = v;
2682 assert(proxy.get() == v);
2683 proxy.set(nullptr);
2684 assert(u.prop_object.get_value() == nullptr);
2685 }
2686 {
2687 CppEnum v1 = CppEnum::VALUE_1, v0 = CppEnum::VALUE_0;
2688 auto proxy = u.prop_enum.get_proxy();
2689 u.prop_enum = v1;
2690 assert(proxy.get() == v1);
2691 proxy.set(v0);
2692 assert(u.prop_enum == v0);
2693 }
2694 {
2695 const int val = 8;
2696 u.prop_itf_int = val;
2697 auto proxy = u.prop_itf_int.get_proxy();
2698 assert(proxy.get() == val);
2699 proxy.set(val);
2700 assert(u.prop_itf_int == val);
2701 }
2702
2703 {
2704 // local properties
2705 property_read<bool> p{&u, "p", "p", "p", false};
2706 p.get_proxy();
2707
2708 property_write<bool> q{&u, "p", "p", "p", false};
2709 q.get_proxy();
2710 }
2711 }
2712
2713 {
2714 // create non-stack
2715 auto u = gi::make_ref<UserObject>();
2716 assert(u->list_properties().size() > 0);
2717 // cast works ok
2718 GObject_::Object v = u;
2719 assert(refcount(v.gobj_()) == 2);
2720 auto l = [](GObject_::Object) {};
2721 l(u);
2722 auto u2 = ref_ptr_cast<UserObject>(v);
2723 assert(u2 == u);
2724 assert(refcount(v.gobj_()) == 3);
2725 auto u3 = u2;
2726 assert(refcount(v.gobj_()) == 4);
2727 auto u4 = std::move(u3);
2728 assert(refcount(v.gobj_()) == 4);
2729 assert(!u3);
2730
2731 auto um = gi::make_ref<UserObject, gi::construct_cpp_t>();
2732 assert(um->gobj_type_() == u->gobj_type_());
2733 }
2734
2735 {
2736 // similar for C-style
2737 auto u = gi::make_ref<UserCObject>("prop_int", DEFAULT_PROP_INT / 2);
2738 assert(u->list_properties().size() > 0);
2739 assert(u->prop_int.get_value() == DEFAULT_PROP_INT / 2);
2740
2741 auto um = gi::make_ref<UserCObject>();
2742 assert(um->prop_int.get_value() == DEFAULT_PROP_INT);
2743 assert(um->gobj_type_() == u->gobj_type_());
2744 assert(um->gobj_type_() == gi::register_type<UserCObject>());
2745 }
2746 }
2747
2748 int
2749 main(int argc, char *argv[])
2750 {
2751 (void)argc;
2752 (void)argv;
2753
2754 test_trait();
2755 test_wrap();
2756 test_helpers();
2757 test_collection();
2758 test_string();
2759 test_exception();
2760 test_enumflag();
2761 test_value();
2762 test_property();
2763 test_signal();
2764 test_callback();
2765 test_impl();
2766
2767 return 0;
2768 }