1 // Generated by gmmproc 2.64.2 -- DO NOT MODIFY!
2 
3 
4 #include <glibmm.h>
5 
6 #include <glibmm/variant.h>
7 #include <glibmm/private/variant_p.h>
8 
9 
10 /* Copyright 2010 The glibmm Development Team
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include <glibmm/variant.h>
27 #include <glibmm/utility.h>
28 #include <glib.h>
29 #include <iostream>
30 
31 namespace Glib
32 {
33 
VariantBase(GVariant * castitem,bool make_a_copy)34 VariantBase::VariantBase(GVariant* castitem, bool make_a_copy /* = false */)
35 {
36   if (castitem)
37   {
38     if (g_variant_is_floating(castitem))
39       g_variant_ref_sink(castitem);
40 
41     if (make_a_copy)
42       g_variant_ref(castitem);
43   }
44 
45   gobject_ = castitem;
46 }
47 
48 void
get_normal_form(VariantBase & result) const49 VariantBase::get_normal_form(VariantBase& result) const
50 {
51   GVariant* const g_value = g_variant_get_normal_form(const_cast<GVariant*>(gobj()));
52 
53   // The C function never returns NULL, according to its documenation,
54   // so we don't need a bool return value.
55   result.init(g_value); // g_value is already referenced.
56 }
57 
58 void
byteswap(VariantBase & result) const59 VariantBase::byteswap(VariantBase& result) const
60 {
61   GVariant* const g_value = g_variant_byteswap(const_cast<GVariant*>(gobj()));
62   result.init(g_value); // g_value is already referenced.
63 }
64 
65 bool
is_castable_to(const VariantType & supertype) const66 VariantBase::is_castable_to(const VariantType& supertype) const
67 {
68   const std::string subtype_string = get_type_string();
69   const std::string supertype_string = supertype.get_string();
70 
71   // The following code is similar to g_variant_type_is_subtype_of(), with
72   // these differences:
73   // - Both types are assumed to be definite types (no indefinite types,
74   //   no 'r', '?' or '*').
75   // - VARIANT_TYPE_OBJECT_PATH (o) and VARIANT_TYPE_SIGNATURE (g) can be cast to
76   //   VARIANT_TYPE_STRING (s), (Variant<Glib::ustring>::variant_type()).
77   // - VARIANT_TYPE_STRING (s), VARIANT_TYPE_OBJECT_PATH (o) and
78   //   VARIANT_TYPE_SIGNATURE (g) can be cast to VARIANT_TYPE_BYTESTRING (ay),
79   //   (Variant<std::string>::variant_type()).
80   // - VARIANT_TYPE_HANDLE (h) can be cast to VARIANT_TYPE_INT32 (i),
81   //   (Variant<gint32>::variant_type()).
82 
83   std::size_t subtype_index = 0;
84   std::size_t supertype_index = 0;
85   const std::size_t supertype_size = supertype_string.size();
86   while (supertype_index < supertype_size)
87   {
88     const char supertype_char = supertype_string[supertype_index++];
89     const char subtype_char = subtype_string[subtype_index++];
90 
91     if (supertype_char == subtype_char)
92       continue;
93 
94     switch (supertype_char)
95     {
96     case 's':
97       if (!(subtype_char == 'o' || subtype_char == 'g'))
98         return false;
99       break;
100 
101     case 'a':
102       if (!(supertype_string[supertype_index] == 'y' &&
103             (subtype_char == 's' || subtype_char == 'o' || subtype_char == 'g')))
104         return false;
105       supertype_index++;
106       break;
107 
108     case 'i':
109       if (!(subtype_char == 'h'))
110         return false;
111       break;
112 
113     default:
114       return false;
115     }
116   }
117   return true;
118 }
119 
VariantStringBase()120 VariantStringBase::VariantStringBase() : VariantBase()
121 {
122 }
123 
VariantStringBase(GVariant * castitem,bool take_a_reference)124 VariantStringBase::VariantStringBase(GVariant* castitem, bool take_a_reference)
125 : VariantBase(castitem, take_a_reference)
126 {
127 }
128 
129 // static
130 void
create_object_path(VariantStringBase & output,const std::string & object_path)131 VariantStringBase::create_object_path(VariantStringBase& output, const std::string& object_path)
132 {
133   GVariant* result = nullptr;
134   result = g_variant_new_object_path(object_path.c_str());
135   g_variant_ref_sink(result);
136   output.init(result);
137 }
138 
139 // static
140 void
create_signature(VariantStringBase & output,const std::string & signature)141 VariantStringBase::create_signature(VariantStringBase& output, const std::string& signature)
142 {
143   GVariant* result = nullptr;
144   result = g_variant_new_signature(signature.c_str());
145   g_variant_ref_sink(result);
146   output.init(result);
147 }
148 
VariantContainerBase()149 VariantContainerBase::VariantContainerBase() : VariantBase()
150 {
151 }
152 
VariantContainerBase(GVariant * castitem,bool take_a_reference)153 VariantContainerBase::VariantContainerBase(GVariant* castitem, bool take_a_reference)
154 : VariantBase(castitem, take_a_reference)
155 {
156 }
157 
158 // static
159 VariantContainerBase
create_tuple(const std::vector<VariantBase> & children)160 VariantContainerBase::create_tuple(const std::vector<VariantBase>& children)
161 {
162   using var_ptr = GVariant*;
163   var_ptr* const var_array = new var_ptr[children.size()];
164 
165   for (std::vector<VariantBase>::size_type i = 0; i < children.size(); i++)
166     var_array[i] = const_cast<GVariant*>(children[i].gobj());
167 
168   VariantContainerBase result =
169     VariantContainerBase(g_variant_new_tuple(var_array, children.size()));
170   delete[] var_array;
171   return result;
172 }
173 
174 // static
175 VariantContainerBase
create_tuple(const VariantBase & child)176 VariantContainerBase::create_tuple(const VariantBase& child)
177 {
178   std::vector<VariantBase> vec;
179   vec.emplace_back(child);
180   return create_tuple(vec);
181 }
182 
183 // static
184 VariantContainerBase
create_maybe(const VariantType & child_type,const VariantBase & child)185 VariantContainerBase::create_maybe(const VariantType& child_type, const VariantBase& child)
186 {
187   GVariant* g_variant = g_variant_new_maybe(child_type.gobj(), const_cast<GVariant*>(child.gobj()));
188   VariantContainerBase result = VariantContainerBase(g_variant);
189   return result;
190 }
191 
192 void
get_child(VariantBase & child,gsize index) const193 VariantContainerBase::get_child(VariantBase& child, gsize index) const
194 {
195   if (index >= get_n_children())
196     throw std::out_of_range("VariantContainerBase::get_child(): Index out of bounds.");
197 
198   GVariant* const gvariant = g_variant_get_child_value(gobject_, index);
199   child.init(gvariant);
200 }
201 
202 // VariantContainerBase has no method variant_type()
203 template <>
204 VariantContainerBase
cast_dynamic(const VariantBase & v)205 VariantBase::cast_dynamic<VariantContainerBase>(const VariantBase& v)
206 {
207   if (!v.gobj())
208     return VariantContainerBase();
209 
210   if (v.get_type().is_container())
211   {
212     return VariantContainerBase(const_cast<GVariant*>(v.gobj()), true);
213   }
214   else
215   {
216     // std::cerr << "vtype=" << v.get_type_string() << std::endl;
217     throw std::bad_cast();
218   }
219 }
220 
221 bool
get_maybe(VariantBase & maybe) const222 VariantContainerBase::get_maybe(VariantBase& maybe) const
223 {
224   GVariant* const g_value = g_variant_get_maybe(const_cast<GVariant*>(gobj()));
225 
226   if (g_value)
227   {
228     maybe.init(g_value); // g_value is already referenced.
229     return true;
230   }
231   else
232   {
233     return false;
234   }
235 }
236 
237 VariantIter
get_iter(const VariantType & container_variant_type) const238 VariantContainerBase::get_iter(const VariantType& container_variant_type) const
239 {
240   // Get the GVariantIter.
241   // If the underlying GVariant can be cast to the type of the container,
242   // use the actual type (which may differ from container_variant_type, if
243   // the GVariant contains strings, object paths or DBus type signatures),
244   // otherwise let g_variant_get() report what's wrong with the type.
245   GVariantIter* g_iter = nullptr;
246   g_variant_get(const_cast<GVariant*>(gobj()),
247     is_castable_to(container_variant_type) ? get_type_string().c_str()
248                                            : container_variant_type.get_string().c_str(),
249     &g_iter);
250 
251   return VariantIter(g_iter);
252 }
253 
254 /****************** Specializations ***********************************/
255 
256 #ifndef GLIBMM_DISABLE_DEPRECATED
257 
operator const void*() const258 VariantBase::operator const void*() const
259 {
260   return gobj() ? GINT_TO_POINTER(1) : nullptr;
261 }
262 #endif // GLIBMM_DISABLE_DEPRECATED
263 
264 
operator bool() const265 VariantBase::operator bool() const
266 {
267   return gobj() != nullptr;
268 }
269 
270 void
init(const GVariant * cobject,bool take_a_reference)271 VariantBase::init(const GVariant* cobject, bool take_a_reference)
272 {
273   if (gobject_)
274     g_variant_unref(gobject_);
275 
276   gobject_ = const_cast<GVariant*>(cobject);
277   if (take_a_reference)
278     g_variant_ref(gobject_);
279 }
280 
281 /*--------------------Variant<VariantBase>---------------------*/
282 
Variant()283 Variant<VariantBase>::Variant() : VariantContainerBase()
284 {
285 }
286 
Variant(GVariant * castitem,bool take_a_reference)287 Variant<VariantBase>::Variant(GVariant* castitem, bool take_a_reference)
288 : VariantContainerBase(castitem, take_a_reference)
289 {
290 }
291 
292 // static
293 const VariantType&
variant_type()294 Variant<VariantBase>::variant_type()
295 {
296   return VARIANT_TYPE_VARIANT;
297 }
298 
299 Variant<VariantBase>
create(const VariantBase & data)300 Variant<VariantBase>::create(const VariantBase& data)
301 {
302   auto result = Variant<VariantBase>(g_variant_new_variant(const_cast<GVariant*>(data.gobj())));
303   return result;
304 }
305 
306 void
get(VariantBase & variant) const307 Variant<VariantBase>::get(VariantBase& variant) const
308 {
309   GVariant* const gvariant = g_variant_get_variant(gobject_);
310   variant.init(gvariant);
311 }
312 
313 /*--------------------Variant<Glib::ustring>---------------------*/
314 
Variant()315 Variant<Glib::ustring>::Variant() : VariantStringBase()
316 {
317 }
318 
Variant(GVariant * castitem,bool take_a_reference)319 Variant<Glib::ustring>::Variant(GVariant* castitem, bool take_a_reference)
320 : VariantStringBase(castitem, take_a_reference)
321 {
322 }
323 
324 // static
325 const VariantType&
variant_type()326 Variant<Glib::ustring>::variant_type()
327 {
328   return VARIANT_TYPE_STRING;
329 }
330 
331 Variant<Glib::ustring>
create(const Glib::ustring & data)332 Variant<Glib::ustring>::create(const Glib::ustring& data)
333 {
334   auto result = Variant<Glib::ustring>(g_variant_new_string(data.c_str()));
335   return result;
336 }
337 
338 Glib::ustring
get() const339 Variant<Glib::ustring>::get() const
340 {
341   return convert_const_gchar_ptr_to_ustring(g_variant_get_string(gobject_, nullptr));
342 }
343 
344 // Variant<Glib::ustring> makes sense for multiple types.
345 // See http://library.gnome.org/devel/glib/unstable/glib-GVariant.html#g-variant-get-string
346 template <>
347 Variant<Glib::ustring>
cast_dynamic(const VariantBase & v)348 VariantBase::cast_dynamic<Variant<Glib::ustring>>(const VariantBase& v)
349 {
350   if (!v.gobj())
351   {
352     return Variant<Glib::ustring>();
353   }
354 
355   const VariantType vtype = v.get_type();
356   if (vtype.equal(VARIANT_TYPE_STRING) || vtype.equal(VARIANT_TYPE_OBJECT_PATH) ||
357       vtype.equal(VARIANT_TYPE_SIGNATURE))
358   {
359     return Variant<Glib::ustring>(const_cast<GVariant*>(v.gobj()), true);
360   }
361   else
362   {
363     // std::cerr << "vtype=" << v.get_type_string() << std::endl;
364     throw std::bad_cast();
365   }
366 }
367 
368 /*--------------------Variant<Glib::DBusObjectPathString>---------------------*/
369 
Variant()370 Variant<Glib::DBusObjectPathString>::Variant() : VariantStringBase()
371 {
372 }
373 
Variant(GVariant * castitem,bool take_a_reference)374 Variant<Glib::DBusObjectPathString>::Variant(GVariant* castitem, bool take_a_reference)
375 : VariantStringBase(castitem, take_a_reference)
376 {
377 }
378 
379 // static
380 const VariantType&
variant_type()381 Variant<Glib::DBusObjectPathString>::variant_type()
382 {
383   return VARIANT_TYPE_OBJECT_PATH;
384 }
385 
386 Variant<Glib::DBusObjectPathString>
create(const Glib::DBusObjectPathString & data)387 Variant<Glib::DBusObjectPathString>::create(const Glib::DBusObjectPathString& data)
388 {
389   auto result = Variant<CppType>(g_variant_new_object_path(data.c_str()));
390   return result;
391 }
392 
393 Glib::DBusObjectPathString
get() const394 Variant<Glib::DBusObjectPathString>::get() const
395 {
396   const char* s = g_variant_get_string(gobject_, nullptr);
397   return s ? CppType(s) : CppType();
398 }
399 
400 /*--------------------Variant<Glib::DBusSignatureString>---------------------*/
401 
Variant()402 Variant<Glib::DBusSignatureString>::Variant() : VariantStringBase()
403 {
404 }
405 
Variant(GVariant * castitem,bool take_a_reference)406 Variant<Glib::DBusSignatureString>::Variant(GVariant* castitem, bool take_a_reference)
407 : VariantStringBase(castitem, take_a_reference)
408 {
409 }
410 
411 // static
412 const VariantType&
variant_type()413 Variant<Glib::DBusSignatureString>::variant_type()
414 {
415   return VARIANT_TYPE_SIGNATURE;
416 }
417 
418 Variant<Glib::DBusSignatureString>
create(const Glib::DBusSignatureString & data)419 Variant<Glib::DBusSignatureString>::create(const Glib::DBusSignatureString& data)
420 {
421   auto result = Variant<CppType>(g_variant_new_signature(data.c_str()));
422   return result;
423 }
424 
425 Glib::DBusSignatureString
get() const426 Variant<Glib::DBusSignatureString>::get() const
427 {
428   const char* s = g_variant_get_string(gobject_, nullptr);
429   return s ? CppType(s) : CppType();
430 }
431 
432 /*--------------------Variant<std::string>---------------------*/
433 
Variant()434 Variant<std::string>::Variant() : VariantStringBase()
435 {
436 }
437 
Variant(GVariant * castitem,bool take_a_reference)438 Variant<std::string>::Variant(GVariant* castitem, bool take_a_reference)
439 : VariantStringBase(castitem, take_a_reference)
440 {
441 }
442 
443 // static
444 const VariantType&
variant_type()445 Variant<std::string>::variant_type()
446 {
447   return VARIANT_TYPE_BYTESTRING;
448 }
449 
450 Variant<std::string>
create(const std::string & data)451 Variant<std::string>::create(const std::string& data)
452 {
453   auto result = Variant<std::string>(g_variant_new_bytestring(data.c_str()));
454   return result;
455 }
456 
457 // Variant<std::string> makes sense for multiple types.
458 // See http://library.gnome.org/devel/glib/unstable/glib-GVariant.html#g-variant-get-string
459 template <>
460 Variant<std::string>
cast_dynamic(const VariantBase & v)461 VariantBase::cast_dynamic<Variant<std::string>>(const VariantBase& v)
462 {
463   if (!v.gobj())
464   {
465     return Variant<std::string>();
466   }
467 
468   const VariantType vtype = v.get_type();
469   if (vtype.equal(VARIANT_TYPE_STRING) || vtype.equal(VARIANT_TYPE_BYTESTRING) ||
470       vtype.equal(VARIANT_TYPE_OBJECT_PATH) || vtype.equal(VARIANT_TYPE_SIGNATURE))
471   {
472     return Variant<std::string>(const_cast<GVariant*>(v.gobj()), true);
473   }
474   else
475   {
476     // std::cerr << "vtype=" << v.get_type_string() << std::endl;
477     throw std::bad_cast();
478   }
479 }
480 
481 std::string
get() const482 Variant<std::string>::get() const
483 {
484   const VariantType vtype = get_type();
485 
486   const char* pch = nullptr;
487   if (vtype.equal(VARIANT_TYPE_BYTESTRING))
488     pch = g_variant_get_bytestring(gobject_);
489   else // g_variant_get_string() can handle strings, object paths, and signatures.
490     pch = g_variant_get_string(gobject_, nullptr);
491 
492   return convert_const_gchar_ptr_to_stdstring(pch);
493 }
494 
495 /*--------------------Variant< std::vector<Glib::ustring> >---------------------*/
496 
497 using type_vec_ustring = std::vector<Glib::ustring>;
498 
Variant()499 Variant<type_vec_ustring>::Variant() : VariantContainerBase()
500 {
501 }
502 
Variant(GVariant * castitem,bool take_a_reference)503 Variant<type_vec_ustring>::Variant(GVariant* castitem, bool take_a_reference)
504 : VariantContainerBase(castitem, take_a_reference)
505 {
506 }
507 
508 // static
509 const VariantType&
variant_type()510 Variant<type_vec_ustring>::variant_type()
511 {
512   return VARIANT_TYPE_STRING_ARRAY;
513 }
514 
515 Variant<type_vec_ustring>
create(const type_vec_ustring & data)516 Variant<type_vec_ustring>::create(const type_vec_ustring& data)
517 {
518   // Get the variant type of the elements.
519   VariantType element_variant_type = Variant<Glib::ustring>::variant_type();
520 
521   // Get the variant type of the array.
522   VariantType array_variant_type = Variant<type_vec_ustring>::variant_type();
523 
524   // Create a GVariantBuilder to build the array.
525   GVariantBuilder* builder = g_variant_builder_new(array_variant_type.gobj());
526 
527   // Add the elements of the vector into the builder.
528   for (const auto& str : data)
529     g_variant_builder_add(builder, element_variant_type.get_string().c_str(), str.c_str());
530 
531   // Create the variant using the builder.
532   auto result =
533     Variant<type_vec_ustring>(g_variant_new(array_variant_type.get_string().c_str(), builder));
534 
535   g_variant_builder_unref(builder);
536 
537   return result;
538 }
539 
540 Glib::ustring
get_child(gsize index) const541 Variant<type_vec_ustring>::get_child(gsize index) const
542 {
543   if (index >= get_n_children())
544     throw std::out_of_range(
545       "Variant< std::vector<Glib::ustring> >::get_child(): Index out of bounds.");
546 
547   GVariant* gvariant = g_variant_get_child_value(const_cast<GVariant*>(gobj()), index);
548 
549   return Glib::Variant<Glib::ustring>(gvariant).get();
550 }
551 
552 type_vec_ustring
get() const553 Variant<type_vec_ustring>::get() const
554 {
555   // g_variant_get_strv() can be used only if the type is VARIANT_TYPE_STRING_ARRAY,
556   // but the Variant can alternatively hold an array of object paths or DBus type signatures.
557   type_vec_ustring result;
558 
559   for (gsize i = 0, n_children = get_n_children(); i < n_children; ++i)
560   {
561     GVariant* gvariant = g_variant_get_child_value(const_cast<GVariant*>(gobj()), i);
562     result.emplace_back(Glib::Variant<Glib::ustring>(gvariant).get());
563   }
564 
565   return result;
566 }
567 
568 VariantIter
get_iter() const569 Variant<type_vec_ustring>::get_iter() const
570 {
571   return VariantContainerBase::get_iter(variant_type());
572 }
573 
574 /*--------------------Variant<std::vector<Glib::DBusObjectPathString>>---------------------*/
575 
576 using type_vec_opstring = std::vector<Glib::DBusObjectPathString>;
577 
Variant()578 Variant<type_vec_opstring>::Variant() : VariantContainerBase()
579 {
580 }
581 
Variant(GVariant * castitem,bool take_a_reference)582 Variant<type_vec_opstring>::Variant(GVariant* castitem, bool take_a_reference)
583 : VariantContainerBase(castitem, take_a_reference)
584 {
585 }
586 
587 // static
588 const VariantType&
variant_type()589 Variant<type_vec_opstring>::variant_type()
590 {
591   return VARIANT_TYPE_OBJECT_PATH_ARRAY;
592 }
593 
594 // static
595 Variant<type_vec_opstring>
create(const type_vec_opstring & data)596 Variant<type_vec_opstring>::create(const type_vec_opstring& data)
597 {
598   // Get the variant type of the elements.
599   VariantType element_variant_type = Variant<CppType>::variant_type();
600 
601   // Get the variant type of the array.
602   VariantType array_variant_type = Variant<type_vec_opstring>::variant_type();
603 
604   // Create a GVariantBuilder to build the array.
605   GVariantBuilder* builder = g_variant_builder_new(array_variant_type.gobj());
606 
607   // Add the elements of the vector into the builder.
608   for (const auto& str : data)
609     g_variant_builder_add(builder, element_variant_type.get_string().c_str(), str.c_str());
610 
611   // Create the variant using the builder.
612   auto result =
613     Variant<type_vec_opstring>(g_variant_new(array_variant_type.get_string().c_str(), builder));
614 
615   g_variant_builder_unref(builder);
616 
617   return result;
618 }
619 
620 Glib::DBusObjectPathString
get_child(gsize index) const621 Variant<type_vec_opstring>::get_child(gsize index) const
622 {
623   if (index >= get_n_children())
624     throw std::out_of_range(
625       "Variant< std::vector<Glib::DBusObjectPathString> >::get_child(): Index out of bounds.");
626 
627   GVariant* gvariant = g_variant_get_child_value(const_cast<GVariant*>(gobj()), index);
628 
629   return Glib::Variant<CppType>(gvariant).get();
630 }
631 
632 type_vec_opstring
get() const633 Variant<type_vec_opstring>::get() const
634 {
635   gsize n_children = 0;
636   const gchar** children = g_variant_get_objv(const_cast<GVariant*>(gobj()), &n_children);
637   type_vec_opstring result = type_vec_opstring(children, children+n_children);
638   g_free(children);
639   return result;
640 }
641 
642 VariantIter
get_iter() const643 Variant<type_vec_opstring>::get_iter() const
644 {
645   return VariantContainerBase::get_iter(variant_type());
646 }
647 
648 /*--------------------Variant< std::vector<std::string> >---------------------*/
649 
650 using type_vec_string = std::vector<std::string>;
651 
Variant()652 Variant<type_vec_string>::Variant() : VariantContainerBase()
653 {
654 }
655 
Variant(GVariant * castitem,bool take_a_reference)656 Variant<type_vec_string>::Variant(GVariant* castitem, bool take_a_reference)
657 : VariantContainerBase(castitem, take_a_reference)
658 {
659 }
660 
661 // static
662 const VariantType&
variant_type()663 Variant<type_vec_string>::variant_type()
664 {
665   return VARIANT_TYPE_BYTESTRING_ARRAY;
666 }
667 
668 Variant<type_vec_string>
create(const type_vec_string & data)669 Variant<type_vec_string>::create(const type_vec_string& data)
670 {
671   // Create a string array to add the strings of the vector to.
672   char** str_array = g_new(char*, data.size() + 1);
673 
674   // Add the elements of the vector into the string array.
675   for (type_vec_string::size_type i = 0; i < data.size(); i++)
676     str_array[i] = g_strdup(data[i].c_str());
677 
678   // Terminate the string array.
679   str_array[data.size()] = nullptr;
680 
681   // Create the variant using g_variant_new_bytestring_array() (passing the
682   // newly constructed array.
683   auto result = Variant<type_vec_string>(g_variant_new_bytestring_array(str_array, data.size()));
684 
685   g_strfreev(str_array);
686   return result;
687 }
688 
689 Variant<type_vec_string>
create_from_object_paths(const type_vec_string & data)690 Variant<type_vec_string>::create_from_object_paths(const type_vec_string& data)
691 {
692   // Create a string array to add the strings of the vector to.
693   char** str_array = g_new(char*, data.size() + 1);
694 
695   // Add the elements of the vector into the string array.
696   for (type_vec_string::size_type i = 0; i < data.size(); i++)
697     str_array[i] = g_strdup(data[i].c_str());
698 
699   // Terminate the string array.
700   str_array[data.size()] = nullptr;
701 
702   // Create the variant using g_variant_new_objv() (passing the
703   // newly constructed array.
704   auto result = Variant<type_vec_string>(g_variant_new_objv(str_array, data.size()));
705 
706   g_strfreev(str_array);
707   return result;
708 }
709 
710 std::string
get_child(gsize index) const711 Variant<type_vec_string>::get_child(gsize index) const
712 {
713   if (index >= get_n_children())
714     throw std::out_of_range(
715       "Variant< std::vector<std::string> >::get_child(): Index out of bounds.");
716 
717   GVariant* gvariant = g_variant_get_child_value(const_cast<GVariant*>(gobj()), index);
718 
719   return Glib::Variant<std::string>(gvariant).get();
720 }
721 
722 type_vec_string
get() const723 Variant<type_vec_string>::get() const
724 {
725   // g_variant_get_bytestring_array() can be used only if the type is VARIANT_TYPE_BYTESTRING_ARRAY,
726   // but the Variant can alternatively hold an array of strings, object paths or DBus type
727   // signatures.
728   type_vec_string result;
729 
730   for (gsize i = 0, n_children = get_n_children(); i < n_children; ++i)
731   {
732     GVariant* gvariant = g_variant_get_child_value(const_cast<GVariant*>(gobj()), i);
733     result.emplace_back(Glib::Variant<std::string>(gvariant).get());
734   }
735 
736   return result;
737 }
738 
739 VariantIter
get_iter() const740 Variant<type_vec_string>::get_iter() const
741 {
742   return VariantContainerBase::get_iter(variant_type());
743 }
744 
745 /*---------------------Value<Glib::VariantBase>---------------------*/
746 
set(CppType data)747 void Value<VariantBase>::set(CppType data)
748 {
749   set_variant(data.gobj());
750 }
751 
get() const752 Value<VariantBase>::CppType Value<VariantBase>::get() const
753 {
754   return CppType(get_variant(), true);
755 }
756 
757 } // namespace Glib
758 
759 namespace
760 {
761 } // anonymous namespace
762 
763 
VariantParseError(Glib::VariantParseError::Code error_code,const Glib::ustring & error_message)764 Glib::VariantParseError::VariantParseError(Glib::VariantParseError::Code error_code, const Glib::ustring& error_message)
765 :
766   Glib::Error (G_VARIANT_PARSE_ERROR, error_code, error_message)
767 {}
768 
VariantParseError(GError * gobject)769 Glib::VariantParseError::VariantParseError(GError* gobject)
770 :
771   Glib::Error (gobject)
772 {}
773 
code() const774 Glib::VariantParseError::Code Glib::VariantParseError::code() const
775 {
776   return static_cast<Code>(Glib::Error::code());
777 }
778 
throw_func(GError * gobject)779 void Glib::VariantParseError::throw_func(GError* gobject)
780 {
781   throw Glib::VariantParseError(gobject);
782 }
783 
784 
785 namespace Glib
786 {
787 
wrap(GVariant * object,bool take_copy)788 Glib::VariantBase wrap(GVariant* object, bool take_copy /* = false */)
789 {
790   return Glib::VariantBase(object, take_copy);
791 }
792 
793 } // namespace Glib
794 
795 
796 namespace Glib
797 {
798 
799 
VariantBase()800 VariantBase::VariantBase()
801 :
802   gobject_ (nullptr) // Allows creation of invalid wrapper, e.g. for output arguments to methods.
803 {}
804 
VariantBase(const VariantBase & src)805 VariantBase::VariantBase(const VariantBase& src)
806 :
807   gobject_ ((src.gobject_) ? g_variant_ref_sink(src.gobject_) : nullptr)
808 {}
809 
810 
operator =(const VariantBase & src)811 VariantBase& VariantBase::operator=(const VariantBase& src)
812 {
813   const auto new_gobject = (src.gobject_) ? g_variant_ref_sink(src.gobject_) : nullptr;
814 
815   if(gobject_)
816     g_variant_unref(gobject_);
817 
818   gobject_ = new_gobject;
819 
820   return *this;
821 }
822 
VariantBase(VariantBase && other)823 VariantBase::VariantBase(VariantBase&& other) noexcept
824 :
825   gobject_(other.gobject_)
826 {
827   other.gobject_ = nullptr;
828 }
829 
operator =(VariantBase && other)830 VariantBase& VariantBase::operator=(VariantBase&& other) noexcept
831 {
832   VariantBase temp (other);
833   swap(temp);
834   return *this;
835 }
836 
~VariantBase()837 VariantBase::~VariantBase() noexcept
838 {
839   if(gobject_)
840     g_variant_unref(gobject_);
841 }
842 
swap(VariantBase & other)843 void VariantBase::swap(VariantBase& other) noexcept
844 {
845   std::swap(gobject_, other.gobject_);
846 }
847 
gobj_copy() const848 GVariant* VariantBase::gobj_copy() const
849 {
850   return g_variant_ref_sink(gobject_);
851 }
852 
853 
get_type() const854 VariantType VariantBase::get_type() const
855 {
856   return Glib::wrap(const_cast<GVariantType*>(g_variant_get_type(const_cast<GVariant*>(gobj()))), true);
857 }
858 
get_type_string() const859 std::string VariantBase::get_type_string() const
860 {
861   return Glib::convert_const_gchar_ptr_to_stdstring(g_variant_get_type_string(const_cast<GVariant*>(gobj())));
862 }
863 
is_floating() const864 bool VariantBase::is_floating() const
865 {
866   return g_variant_is_floating(const_cast<GVariant*>(gobj()));
867 }
868 
is_of_type(const VariantType & type) const869 bool VariantBase::is_of_type(const VariantType& type) const
870 {
871   return g_variant_is_of_type(const_cast<GVariant*>(gobj()), (type).gobj());
872 }
873 
is_container() const874 bool VariantBase::is_container() const
875 {
876   return g_variant_is_container(const_cast<GVariant*>(gobj()));
877 }
878 
classify() const879 GVariantClass VariantBase::classify() const
880 {
881   return g_variant_classify(const_cast<GVariant*>(gobj()));
882 }
883 
get_size() const884 gsize VariantBase::get_size() const
885 {
886   return g_variant_get_size(const_cast<GVariant*>(gobj()));
887 }
888 
889 #ifndef GLIBMM_DISABLE_DEPRECATED
890 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
get_data()891 gconstpointer VariantBase::get_data()
892 {
893   return g_variant_get_data(gobj());
894 }
895 G_GNUC_END_IGNORE_DEPRECATIONS
896 #endif // GLIBMM_DISABLE_DEPRECATED
897 
get_data() const898 gconstpointer VariantBase::get_data() const
899 {
900   return g_variant_get_data(const_cast<GVariant*>(gobj()));
901 }
902 
get_data_as_bytes() const903 Glib::RefPtr<const Glib::Bytes> VariantBase::get_data_as_bytes() const
904 {
905   return Glib::wrap(g_variant_get_data_as_bytes(const_cast<GVariant*>(gobj())));
906 }
907 
store(gpointer data) const908 void VariantBase::store(gpointer data) const
909 {
910   g_variant_store(const_cast<GVariant*>(gobj()), data);
911 }
912 
print(bool type_annotate) const913 Glib::ustring VariantBase::print(bool type_annotate) const
914 {
915   return Glib::convert_return_gchar_ptr_to_ustring(g_variant_print(const_cast<GVariant*>(gobj()), static_cast<int>(type_annotate)));
916 }
917 
hash() const918 guint VariantBase::hash() const
919 {
920   return g_variant_hash(const_cast<GVariant*>(gobj()));
921 }
922 
equal(const VariantBase & other) const923 bool VariantBase::equal(const VariantBase& other) const
924 {
925   return g_variant_equal(const_cast<GVariant*>(gobj()), const_cast<GVariant*>((other).gobj()));
926 }
927 
is_normal_form() const928 bool VariantBase::is_normal_form() const
929 {
930   return g_variant_is_normal_form(const_cast<GVariant*>(gobj()));
931 }
932 
check_format_string(const std::string & format_string,bool copy_only) const933 bool VariantBase::check_format_string(const std::string& format_string, bool copy_only) const
934 {
935   return g_variant_check_format_string(const_cast<GVariant*>(gobj()), format_string.c_str(), static_cast<int>(copy_only));
936 }
937 
938 
939 } // namespace Glib
940 
941 
942 namespace Glib
943 {
944 
945 
is_object_path(const std::string & string)946 bool VariantStringBase::is_object_path(const std::string& string)
947 {
948   return g_variant_is_object_path(string.c_str());
949 }
950 
is_signature(const std::string & string)951 bool VariantStringBase::is_signature(const std::string& string)
952 {
953   return g_variant_is_signature(string.c_str());
954 }
955 
956 
957 } // namespace Glib
958 
959 
960 namespace Glib
961 {
962 
963 
get_n_children() const964 gsize VariantContainerBase::get_n_children() const
965 {
966   return g_variant_n_children(const_cast<GVariant*>(gobj()));
967 }
968 
get_child(gsize index)969 VariantBase VariantContainerBase::get_child(gsize index)
970 {
971   return Glib::wrap(g_variant_get_child_value(gobj(), index));
972 }
973 
974 
975 } // namespace Glib
976 
977 
978 namespace Glib
979 {
980 
981 
get() const982 VariantBase Variant<VariantBase>::get() const
983 {
984   return Glib::wrap(g_variant_get_variant(const_cast<GVariant*>(gobj())));
985 }
986 
987 
988 } // namespace Glib
989 
990 
991 namespace Glib
992 {
993 
994 
995 } // namespace Glib
996 
997 
998 namespace Glib
999 {
1000 
1001 
1002 } // namespace Glib
1003 
1004 
1005 namespace Glib
1006 {
1007 
1008 
1009 } // namespace Glib
1010 
1011 
1012 namespace Glib
1013 {
1014 
1015 
1016 } // namespace Glib
1017 
1018 
1019