Re: GParamSpec further funcs



Kevin Ryde wrote:

I also wondered if there'd be some value in g_param_value_validate
and/or g_param_values_cmp.  I was tinkering with some comparing for my
"ConnectProperties".  g_param_values_cmp looks like it respects the
"epsilon" in float and double, though it also looks pretty useless on
boxed types (just a pointer compare).

Here's an updated patch for g_param_value_validate and g_param_values_cmp.  I
had to change the doc for g_param_value_validate since its boolean return
value surprisingly means something else.

Commit?

-- 
Bye,
-Torsten
Index: GParamSpec.xs
===================================================================
RCS file: /cvsroot/gtk2-perl/gtk2-perl-xs/Glib/GParamSpec.xs,v
retrieving revision 1.23
diff -u -d -p -r1.23 GParamSpec.xs
--- GParamSpec.xs       17 Oct 2005 19:26:09 -0000      1.23
+++ GParamSpec.xs       3 Aug 2008 15:49:05 -0000
@@ -289,8 +289,60 @@ const gchar* g_param_spec_get_nick (GPar
 const gchar* g_param_spec_get_blurb (GParamSpec * pspec)
 
 
+MODULE = Glib::ParamSpec       PACKAGE = Glib::ParamSpec       PREFIX = g_param_
+
+=for apidoc
+
+=signature bool = $paramspec->value_validate (value)
+
+=signature (bool, newval) = $paramspec->value_validate (value)
+
+In scalar context return a boolean indicating whether $value is valid for
+$paramspec: FALSE if it is valid, and TRUE otherwise.  (Note that this is
+likely the opposite of what you expect.)  In array context return also a new
+value which is $value modified to be valid, which means for instance clamped to
+the minimum/maximum, etc.
+
+=cut
+void
+g_param_value_validate (GParamSpec * pspec, SV *value)
+    PREINIT:
+       GValue v = { 0, };
+       GType type;
+       int ret;
+    PPCODE:
+       type = G_PARAM_SPEC_VALUE_TYPE (pspec);
+       g_value_init (&v, type);
+       gperl_value_from_sv (&v, value);
+       ret = g_param_value_validate (pspec, &v);
+       PUSHs (sv_2mortal (boolSV (ret)));
+       if (GIMME_V == G_ARRAY)
+               XPUSHs (sv_2mortal (gperl_sv_from_value (&v)));
+       g_value_unset (&v);
+
+int
+g_param_values_cmp (GParamSpec * pspec, SV *value1, SV *value2)
+    PREINIT:
+       GValue v1 = { 0, };
+       GValue v2 = { 0, };
+       GType type;
+    CODE:
+       type = G_PARAM_SPEC_VALUE_TYPE (pspec);
+       g_value_init (&v1, type);
+       g_value_init (&v2, type);
+       gperl_value_from_sv (&v1, value1);
+       gperl_value_from_sv (&v2, value2);
+       RETVAL = g_param_values_cmp (pspec, &v1, &v2);
+       g_value_unset (&v1);
+       g_value_unset (&v2);
+    OUTPUT:
+       RETVAL
+
+
 ## stuff from gparamspecs.h
 
+MODULE = Glib::ParamSpec       PACKAGE = Glib::ParamSpec       PREFIX = g_param_spec_
+
 ###
 ### glib's param specs offer lots of different sizes of integers and floating
 ### point values, but perl only supports UV (uint), IV (int), and NV (double).
Index: t/e.t
===================================================================
RCS file: /cvsroot/gtk2-perl/gtk2-perl-xs/Glib/t/e.t,v
retrieving revision 1.2
diff -u -d -p -r1.2 e.t
--- t/e.t       20 Oct 2004 17:33:56 -0000      1.2
+++ t/e.t       3 Aug 2008 15:49:05 -0000
@@ -1,9 +1,10 @@
+#!/usr/bin/perl
 #
 # ParamSpec stuff.
 #
 use strict;
 use Glib ':constants';
-use Test::More tests => 231;
+use Test::More tests => 234;
 
 # first register some types with which to play below.
 
@@ -204,3 +205,18 @@ Glib::Type->register (
 foreach (@params) {
        is ($_->get_owner_type, 'Bar', ref($_)." owner type after adding");
 }
+
+
+
+#
+# test value_validate and values_cmp
+#
+{
+       my $pspec = Glib::ParamSpec->int ('int', 'Int', 'Int',
+                                         1, 5, 2,
+                                         G_PARAM_READWRITE);
+       is ($pspec->value_validate (3), FALSE);
+       is_deeply ([$pspec->value_validate (6)], [TRUE, 5]);
+
+       is ($pspec->values_cmp (4, 3), 1);
+}


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