[perl-glib] Add wrappers for GVariantDict



commit 5dc4a9cdc37ac4987df43c9757c611107b6da1e9
Author: Torsten Schönfeld <kaffeetisch gmx de>
Date:   Sat Sep 1 18:46:19 2018 +0200

    Add wrappers for GVariantDict

 GVariant.xs  | 75 ++++++++++++++++++++++++++++++++++++++++++++++--------------
 Glib.exports |  6 +++++
 gperl.h      |  9 ++++++++
 t/variant.t  | 32 +++++++++++++++++++++++++-
 typemap      |  4 ++++
 5 files changed, 108 insertions(+), 18 deletions(-)
---
diff --git a/GVariant.xs b/GVariant.xs
index 8e5d5ce..814c6c5 100644
--- a/GVariant.xs
+++ b/GVariant.xs
@@ -149,6 +149,34 @@ SvGVariantType (SV * sv)
        return gperl_get_boxed_check (sv, G_TYPE_VARIANT_TYPE);
 }
 
+/* --- GVariantDict ----------------------------------------------------------*/
+
+#if GLIB_CHECK_VERSION (2, 40, 0)
+
+/* --- typemap glue --- */
+
+SV *
+newSVGVariantDict (GVariantDict * dict)
+{
+       return gperl_new_boxed (dict, G_TYPE_VARIANT_DICT, FALSE);
+}
+
+SV *
+newSVGVariantDict_own (GVariantDict * dict)
+{
+       return gperl_new_boxed (dict, G_TYPE_VARIANT_DICT, TRUE);
+}
+
+GVariantDict *
+SvGVariantDict (SV * sv)
+{
+       if (!gperl_sv_is_defined (sv))
+               return NULL;
+       return gperl_get_boxed_check (sv, G_TYPE_VARIANT_DICT);
+}
+
+#endif
+
 /* -------------------------------------------------------------------------- */
 
 /* --- helpers ---*/
@@ -265,6 +293,9 @@ For a complete specification, see the documentation at
 =for see_also Glib::VariantType
 =cut
 
+=for see_also Glib::VariantDict
+=cut
+
 BOOT:
        gperl_register_fundamental_full (G_TYPE_VARIANT, "Glib::Variant",
                                         &variant_wrapper_class);
@@ -273,6 +304,9 @@ BOOT:
        variant_type_wrapper_class.unwrap = unwrap_variant_type;
        gperl_register_boxed (G_TYPE_VARIANT_TYPE, "Glib::VariantType",
                              &variant_type_wrapper_class);
+#if GLIB_CHECK_VERSION (2, 40, 0)
+       gperl_register_boxed (G_TYPE_VARIANT_DICT, "Glib::VariantDict", NULL);
+#endif
 
 const GVariantType * g_variant_get_type (GVariant *value);
 
@@ -521,23 +555,6 @@ DESTROY (GVariant * variant)
 
 # --------------------------------------------------------------------------- #
 
-# GLIB_AVAILABLE_IN_2_40 {
-#   GVariantDict * g_variant_dict_new (GVariant *from_asv);
-#   void g_variant_dict_init (GVariantDict *dict, GVariant *from_asv);
-#   gboolean g_variant_dict_lookup (GVariantDict *dict, const gchar *key, const gchar *format_string, ...);
-#   GVariant * g_variant_dict_lookup_value (GVariantDict *dict, const gchar *key, const GVariantType 
*expected_type);
-#   gboolean g_variant_dict_contains (GVariantDict *dict, const gchar *key);
-#   void g_variant_dict_insert (GVariantDict *dict, const gchar *key, const gchar *format_string, ...);
-#   void g_variant_dict_insert_value (GVariantDict *dict, const gchar *key, GVariant *value);
-#   gboolean g_variant_dict_remove (GVariantDict *dict, const gchar *key);
-#   void g_variant_dict_clear (GVariantDict *dict);
-#   GVariant * g_variant_dict_end (GVariantDict *dict);
-#   GVariantDict * g_variant_dict_ref (GVariantDict *dict);
-#   void g_variant_dict_unref (GVariantDict *dict);
-# }
-
-# --------------------------------------------------------------------------- #
-
 # GVariantBuilder * g_variant_builder_new (const GVariantType *type);
 # void g_variant_builder_unref (GVariantBuilder *builder);
 # GVariantBuilder * g_variant_builder_ref (GVariantBuilder *builder);
@@ -684,3 +701,27 @@ GVariantType_own * g_variant_type_new_tuple (class, SV *items);
 GVariantType_own * g_variant_type_new_dict_entry (class, const GVariantType *key, const GVariantType *value);
     C_ARGS:
        key, value
+
+# --------------------------------------------------------------------------- #
+
+#if GLIB_CHECK_VERSION (2, 40, 0)
+
+MODULE = Glib::Variant PACKAGE = Glib::VariantDict     PREFIX = g_variant_dict_
+
+GVariantDict_own * g_variant_dict_new (class, GVariant *from_asv);
+    C_ARGS:
+       from_asv
+
+# gboolean g_variant_dict_lookup (GVariantDict *dict, const gchar *key, const gchar *format_string, ...);
+GVariant_noinc * g_variant_dict_lookup_value (GVariantDict *dict, const gchar *key, const GVariantType 
*expected_type);
+
+gboolean g_variant_dict_contains (GVariantDict *dict, const gchar *key);
+
+# void g_variant_dict_insert (GVariantDict *dict, const gchar *key, const gchar *format_string, ...);
+void g_variant_dict_insert_value (GVariantDict *dict, const gchar *key, GVariant *value);
+
+gboolean g_variant_dict_remove (GVariantDict *dict, const gchar *key);
+
+GVariant_noinc * g_variant_dict_end (GVariantDict *dict);
+
+#endif
diff --git a/Glib.exports b/Glib.exports
index 63e7a62..089ad4e 100644
--- a/Glib.exports
+++ b/Glib.exports
@@ -119,4 +119,10 @@ if (ExtUtils::PkgConfig->atleast_version ('glib-2.0', '2.24.0')) {
     newSVGVariantType
     newSVGVariantType_own);
 }
+if (ExtUtils::PkgConfig->atleast_version ('glib-2.0', '2.40.0')) {
+  push @exports, qw(
+    SvGVariantDict
+    newSVGVariantDict
+    newSVGVariantDict_own);
+}
 1;
diff --git a/gperl.h b/gperl.h
index 1b08d94..c32be5b 100644
--- a/gperl.h
+++ b/gperl.h
@@ -378,6 +378,15 @@ const GVariantType * SvGVariantType (SV * sv);
 
 #endif /* 2.24.0 */
 
+#if GLIB_CHECK_VERSION (2, 40, 0)
+
+typedef GVariantDict GVariantDict_own;
+SV * newSVGVariantDict (GVariantDict * dict);
+SV * newSVGVariantDict_own (GVariantDict * dict);
+GVariantDict * SvGVariantDict (SV * sv);
+
+#endif /* 2.40.0 */
+
 /*
  * --- GBytes -----------------------------------------------------------------
  */
diff --git a/t/variant.t b/t/variant.t
index d954788..717ba2d 100644
--- a/t/variant.t
+++ b/t/variant.t
@@ -14,7 +14,7 @@ use constant {
 };
 
 if (Glib->CHECK_VERSION (2, 24, 0)) {
-  plan tests => 211;
+  plan tests => 223;
 } else {
   plan skip_all => 'Need libglib >= 2.24';
 }
@@ -338,3 +338,33 @@ note ('convenience constructor and accessor');
     is_deeply ($v2->get ('a{si}'), [['äöü', 23], ['Perl', 42], ['💑', 2342]]);
   }
 }
+
+note ('variant dict');
+SKIP: {
+  skip 'dict', 12
+    unless Glib->CHECK_VERSION (2, 40, 0);
+
+  my $v = Glib::Variant->new ('a{sv}', {
+    'äöü' => Glib::Variant->new_uint16 (23),
+    'Perl' => Glib::Variant->new_uint32 (42),
+    '💑' => Glib::Variant->new_uint64 (2342)});
+  my $d = Glib::VariantDict->new ($v);
+
+  ok ($d->contains ('äöü'));
+  ok ($d->contains ('Perl'));
+  ok ($d->contains ('💑'));
+
+  is ($d->lookup_value ('äöü', 'q')->get_uint16 (), $v->lookup_value ('äöü', 'q')->get_uint16 ());
+  is ($d->lookup_value ('Perl', 'u')->get_uint32 (), $v->lookup_value ('Perl', 'u')->get_uint32 ());
+  is ($d->lookup_value ('💑', 't')->get_uint64 (), $v->lookup_value ('💑', 't')->get_uint64 ());
+
+  $d->insert_value ('GNU', Glib::Variant->new_string ('RMS'));
+  ok ($d->contains ('GNU'));
+  ok ($d->remove ('GNU'));
+  ok (not $d->contains ('GNU'));
+
+  my $d_v = $d->end ();
+  is ($d_v->lookup_value ('äöü', 'q')->get_uint16 (), $v->lookup_value ('äöü', 'q')->get_uint16 ());
+  is ($d_v->lookup_value ('Perl', 'u')->get_uint32 (), $v->lookup_value ('Perl', 'u')->get_uint32 ());
+  is ($d_v->lookup_value ('💑', 't')->get_uint64 (), $v->lookup_value ('💑', 't')->get_uint64 ());
+}
diff --git a/typemap b/typemap
index 1e86762..1698c57 100644
--- a/typemap
+++ b/typemap
@@ -108,6 +108,10 @@ GVariantType *             T_GPERL_GENERIC_WRAPPER
 const GVariantType *   T_GPERL_GENERIC_WRAPPER
 GVariantType_own *     T_GPERL_GENERIC_WRAPPER
 
+GVariantDict *         T_GPERL_GENERIC_WRAPPER
+const GVariantDict *   T_GPERL_GENERIC_WRAPPER
+GVariantDict_own *     T_GPERL_GENERIC_WRAPPER
+
 GBytes *               T_GPERL_GENERIC_WRAPPER
 GBytes_own *           T_GPERL_GENERIC_WRAPPER
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]