[perl-Glib-Object-Introspection] Handle constants



commit 59b855721dc14e2b2b1faf126161ae9037f0ce8e
Author: Torsten Schönfeld <kaffeetisch gmx de>
Date:   Thu Nov 11 23:27:54 2010 +0100

    Handle constants

 GObjectIntrospection.xs          |   39 +++++++++++++++++++++++++++++++++----
 lib/Glib/Object/Introspection.pm |   24 ++++++++++++++++------
 t/constants.t                    |   13 ++++++++++++
 3 files changed, 64 insertions(+), 12 deletions(-)
---
diff --git a/GObjectIntrospection.xs b/GObjectIntrospection.xs
index d8d4d2f..47b4892 100644
--- a/GObjectIntrospection.xs
+++ b/GObjectIntrospection.xs
@@ -1656,7 +1656,7 @@ store_methods (HV *namespaced_functions, GIBaseInfo *info, GIInfoType info_type)
 MODULE = Glib::Object::Introspection	PACKAGE = Glib::Object::Introspection
 
 void
-load_library (class, namespace, version, search_path=NULL)
+_load_library (class, namespace, version, search_path=NULL)
 	const gchar *namespace
 	const gchar *version
 	const gchar *search_path
@@ -1673,17 +1673,19 @@ load_library (class, namespace, version, search_path=NULL)
 	}
 
 void
-register_types (class, namespace, package)
+_register_types (class, namespace, package)
 	const gchar *namespace
 	const gchar *package
     PREINIT:
 	GIRepository *repository;
 	gint number, i;
+	AV *constants;
 	AV *global_functions;
 	HV *namespaced_functions;
     PPCODE:
 	repository = g_irepository_get_default ();
 
+	constants = newAV ();
 	global_functions = newAV ();
 	namespaced_functions = newHV ();
 
@@ -1699,6 +1701,10 @@ register_types (class, namespace, package)
 		info_type = g_base_info_get_type (info);
 		name = g_base_info_get_name (info);
 
+		if (info_type == GI_INFO_TYPE_CONSTANT) {
+			av_push (constants, newSVpv (name, PL_na));
+		}
+
 		if (info_type == GI_INFO_TYPE_FUNCTION) {
 			av_push (global_functions, newSVpv (name, PL_na));
 		}
@@ -1771,13 +1777,36 @@ register_types (class, namespace, package)
 
 	EXTEND (SP, 1);
 	PUSHs (sv_2mortal (newRV_noinc ((SV *) namespaced_functions)));
+	PUSHs (sv_2mortal (newRV_noinc ((SV *) constants)));
+
+void
+_fetch_constant (class, basename, constant)
+	const gchar *basename
+	const gchar *constant
+    PREINIT:
+	GIRepository *repository;
+	GIConstantInfo *info;
+	GITypeInfo *type_info;
+	GArgument value = {0,};
+    PPCODE:
+	repository = g_irepository_get_default ();
+	info = g_irepository_find_by_name (repository, basename, constant);
+	if (!GI_IS_CONSTANT_INFO (info))
+		ccroak ("not a constant");
+	type_info = g_constant_info_get_type (info);
+	/* FIXME: What am I suppossed to do with the return value? */
+	g_constant_info_get_value (info, &value);
+	EXTEND (sp, 1);
+	PUSHs (sv_2mortal (arg_to_sv (&value, type_info, GI_TRANSFER_NOTHING)));
+	g_base_info_unref ((GIBaseInfo *) type_info);
+	g_base_info_unref ((GIBaseInfo *) info);
 
 void
-invoke (class, basename, namespace, method, ...)
+_invoke (class, basename, namespace, method, ...)
 	const gchar *basename
 	const gchar_ornull *namespace
 	const gchar *method
-PREINIT:
+    PREINIT:
 	guint stack_offset = 4;
 	GIRepository *repository;
 	GIFunctionInfo *info;
@@ -1802,7 +1831,7 @@ PREINIT:
 	GArgument * out_args = NULL;
 	GError * local_error = NULL;
 	gpointer local_error_address = &local_error;
-PPCODE:
+    PPCODE:
 	repository = g_irepository_get_default ();
 	info = get_function_info (repository, basename, namespace, method);
 	symbol = g_function_info_get_symbol (info);
diff --git a/lib/Glib/Object/Introspection.pm b/lib/Glib/Object/Introspection.pm
index 4dc1e51..5bb2a2c 100644
--- a/lib/Glib/Object/Introspection.pm
+++ b/lib/Glib/Object/Introspection.pm
@@ -40,10 +40,10 @@ sub setup {
 
   my %shift_package_name_for = map { $_ => 1 } @$class_static_methods;
 
-  __PACKAGE__->load_library($basename, $version, $search_path);
+  __PACKAGE__->_load_library($basename, $version, $search_path);
 
-  my $functions =
-    __PACKAGE__->register_types($basename, $package);
+  my ($functions, $constants) =
+    __PACKAGE__->_register_types($basename, $package);
 
   no strict 'refs';
 
@@ -58,13 +58,23 @@ sub setup {
         : $auto_name;
       *{$corrected_name} = sub {
         shift if $shift_package_name_for{$corrected_name};
-        __PACKAGE__->invoke($basename,
-                            $is_namespaced ? $namespace : undef,
-                            $name,
-                            @_);
+        __PACKAGE__->_invoke($basename,
+                             $is_namespaced ? $namespace : undef,
+                             $name,
+                             @_);
       };
     }
   }
+
+  foreach my $name (@{$constants}) {
+    my $auto_name = $package . '::' . $name;
+    my $corrected_name = exists $name_corrections->{$auto_name}
+      ? $name_corrections->{$auto_name}
+      : $auto_name;
+    *{$corrected_name} = sub {
+      __PACKAGE__->_fetch_constant($basename, $name);
+    };
+  }
 }
 
 1;
diff --git a/t/constants.t b/t/constants.t
new file mode 100644
index 0000000..44f91ee
--- /dev/null
+++ b/t/constants.t
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+
+BEGIN { require './t/inc/setup.pl' };
+
+use strict;
+use warnings;
+
+plan tests => 4;
+
+is (INT_CONSTANT, 4422);
+delta_ok (DOUBLE_CONSTANT, 44.22);
+is (STRING_CONSTANT, "Some String");
+is (Mixed_Case_Constant, 4423);



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