=?utf-8?q?=5Bperl-Glib-Object-Introspection=5D_Add_API_to_manually_conver?= =?utf-8?q?t_SV_=E2=86=94_enum?=



commit 277a23086a28a1420a90a7100df357d8f008fc35
Author: Torsten SchÃnfeld <kaffeetisch gmx de>
Date:   Mon Jan 28 00:08:44 2013 +0100

    Add API to manually convert SV â enum
    
    Glib::Object::Introspection->convert_sv_to_enum and convert_enum_to_sv are thin
    wrappers around gperl_convert_enum and gperl_convert_back_enum, respectively,
    for use in pure-Perl bindings that need to handle extendable enums.

 GObjectIntrospection.xs          |   20 ++++++++++++++++++++
 NEWS                             |    1 +
 lib/Glib/Object/Introspection.pm |   12 ++++++++++++
 3 files changed, 33 insertions(+), 0 deletions(-)
---
diff --git a/GObjectIntrospection.xs b/GObjectIntrospection.xs
index 23cc7c8..df7aa1a 100644
--- a/GObjectIntrospection.xs
+++ b/GObjectIntrospection.xs
@@ -934,6 +934,26 @@ invoke (class, basename, namespace, function, ...)
 	SPAGAIN;
 	g_base_info_unref ((GIBaseInfo *) info);
 
+gint
+convert_sv_to_enum (class, const gchar *package, SV *sv)
+    PREINIT:
+	GType gtype;
+    CODE:
+	gtype = gperl_type_from_package (package);
+	RETVAL = gperl_convert_enum (gtype, sv);
+    OUTPUT:
+	RETVAL
+
+SV *
+convert_enum_to_sv (class, const gchar *package, gint n)
+    PREINIT:
+	GType gtype;
+    CODE:
+	gtype = gperl_type_from_package (package);
+	RETVAL = gperl_convert_back_enum (gtype, n);
+    OUTPUT:
+	RETVAL
+
 # --------------------------------------------------------------------------- #
 
 MODULE = Glib::Object::Introspection	PACKAGE = Glib::Object::Introspection::GValueWrapper
diff --git a/NEWS b/NEWS
index 3704af2..30d7f0d 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,7 @@ Overview of changes in Glib::Object::Introspection <next>
   related bugs.  This makes it possible to implement complicated interfaces
   such as Gtk3::TreeModel.
 * Allow enums and flags to have class functions.
+* Add API to manually convert between SVs and enums.
 * Plug a few memory leaks.
 
 Overview of changes in Glib::Object::Introspection 0.013
diff --git a/lib/Glib/Object/Introspection.pm b/lib/Glib/Object/Introspection.pm
index ce7c1b5..345b483 100644
--- a/lib/Glib/Object/Introspection.pm
+++ b/lib/Glib/Object/Introspection.pm
@@ -470,6 +470,18 @@ wrapper for a function that expects a GValue, do this:
   # substitute $real_value where you'd use $value
   ...
 
+=head2 Handling extendable enumerations
+
+If you need to handle extendable enumerations for which more than the
+pre-defined values might be valid, then use C<<
+Glib::Object::Introspection->convert_enum_to_sv >> and C<<
+Glib::Object::Introspection->convert_sv_to_enum >>.  They will raise an
+exception on unknown values; catching it then allows you to implement fallback
+behavior.
+
+  Glib::Object::Introspection->convert_enum_to_sv (package, enum_value)
+  Glib::Object::Introspection->convert_sv_to_enum (package, sv)
+
 =head1 SEE ALSO
 
 =over



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