[perl-Glib] Make Glib::Flags::bool() and as_arrayref() callable as methods



commit f61bde517b7604f2099a5007d4d86dece0176881
Author: Kevin Ryde <user42 zip com au>
Date:   Sun Jul 19 16:35:59 2009 +0200

    Make Glib::Flags::bool() and as_arrayref() callable as methods
    
    Signed-off-by: Torsten Schönfeld <kaffeetisch gmx de>

 GType.xs |   32 +++++++++++++++++++++-----------
 t/c.t    |   17 ++++++++++++++++-
 2 files changed, 37 insertions(+), 12 deletions(-)
---
diff --git a/GType.xs b/GType.xs
index cda2d27..e293483 100644
--- a/GType.xs
+++ b/GType.xs
@@ -2785,26 +2785,36 @@ new (const char *class, SV *a)
 	RETVAL
 
 =for apidoc
-=for arg b (SV*)
-=for arg swap (integer)
+=for signature bool = $f->bool
+=for arg ... (__hide__)
+Return 1 if any bits are set in $f, or 0 if none are set.  This is the
+overload for $f in boolean context (like C<if>, etc).  You can call it
+as a method to get a true/false directly too.
 =cut
 int
-bool (SV *a, b, swap)
+bool (SV *f, ...)
     PROTOTYPE: $;@
     CODE:
 	RETVAL = !!gperl_convert_flags (
-		     gperl_fundamental_type_from_obj (a),
-		     a
+		     gperl_fundamental_type_from_obj (f),
+		     f
 		   );
     OUTPUT:
 	RETVAL
 
 =for apidoc
-=for signature ref = $a->as_arrayref
+=for signature aref = $f->as_arrayref
 =for arg ... (__hide__)
+Return the bits of $f as a reference to an array of strings, like
+['flagbit1','flagbit2'].  This is the overload function for C<@{}>,
+ie. arrayizing $f.  You can call it directly as a method too.
+
+Note that @$f gives the bits as a list, but as_arrayref gives an arrayref.
+If an arrayref is what you want then the method style
+somefunc()->as_arrayref can be more readable than [ {somefunc()}].
 =cut
 SV *
-as_arrayref (SV *a, ...)
+as_arrayref (SV *f, ...)
     PROTOTYPE: $;@
     CODE:
 {
@@ -2813,12 +2823,12 @@ as_arrayref (SV *a, ...)
 	 * users call method-style with no args "$f->as_arrayref" too.
 	 */
 	GType gtype;
-	gint a_;
+	gint f_;
 
-	gtype = gperl_fundamental_type_from_obj (a);
-	a_ = gperl_convert_flags (gtype, a);
+	gtype = gperl_fundamental_type_from_obj (f);
+	f_ = gperl_convert_flags (gtype, f);
 
-	RETVAL = flags_as_arrayref (gtype, a_);
+	RETVAL = flags_as_arrayref (gtype, f_);
 }
     OUTPUT:
 	RETVAL
diff --git a/t/c.t b/t/c.t
index 880fbfd..875e5b0 100644
--- a/t/c.t
+++ b/t/c.t
@@ -13,7 +13,7 @@ use warnings;
 
 #########################
 
-use Test::More tests => 51;
+use Test::More tests => 57;
 BEGIN { use_ok('Glib') };
 
 #########################
@@ -225,6 +225,12 @@ is ($obj->get ('some_enum'), 'value-two', 'enum property, after set');
 
 is_deeply (\ { $obj->get ('some_flags') }, ['value-one'], 'flags property');
 is_deeply ($obj->get('some_flags')->as_arrayref, ['value-one'], 'flags property');
+
+is (($obj->get('some_flags') ? "true" : "false"), "true",
+    'flags property, boolean context');
+is ($obj->get('some_flags')->bool, 1,
+    'flags property, bool()');
+
 $obj->set (some_flags => ['value-one', 'value-two']);
 is_deeply (\ { $obj->get ('some_flags') }, ['value-one', 'value-two'],
 	   'flags property, after set');
@@ -239,6 +245,15 @@ eval {
 ok ($@ eq '', 'empty flags values do not croak');
 ok ($obj->get ('some_flags') == [], 'empty flags values work');
 
+is_deeply (\ { $obj->get ('some_flags') }, [], 'empty flags @{}');
+is_deeply ($obj->get('some_flags')->as_arrayref, [],
+           'empty flags, as_arrayref()');
+
+is (($obj->get('some_flags') ? "true" : "false"), "false",
+    'empty flags, boolean context');
+is ($obj->get('some_flags')->bool, 0,
+    'empty flags, bool()');
+
 $obj->set (some_flags => [qw/value-one value-two/]);
 
 ok ($obj->get ('some_flags') == [qw/value-one value-two/], '== is overloaded');



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