Glib::Flags overloaded mutators

View: New views
4 Messages — Rating Filter:   Alert me  

Glib::Flags overloaded mutators

by Kevin Ryde :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It'd be good if Glib::Flags had overloaded += as well as + so you could
go

        $f += 'val';

I think the basic overload can be used without too much trouble, the new
object returned being assigned to the destination automatically by
"overload".

I remember a long time ago making mutators which peeked into the target
object and on refcount==1 changed it in-place instead of creating a new
one.  I guess that's possible with Glib::Flags too, though it's small
enough it might not be worth bothering.




--- GType.xs 02 Jun 2008 09:38:05 +1000 1.92
+++ GType.xs 20 Jun 2008 18:10:11 +1000
@@ -2773,7 +2773,7 @@
         RETVAL
 
 SV *
-union (SV *a, SV *b, int swap)
+union (SV *a, SV *b, SV *swap)
     ALIAS:
         sub = 1
         intersect = 2
@@ -2783,12 +2783,13 @@
 {
  GType gtype;
  const char *package;
-        gint a_, b_;
+        gint a_, b_, swap_;
 
  package = sv_reftype (SvRV (a), TRUE);
  gtype = gperl_fundamental_type_from_package (package);
-        a_ = gperl_convert_flags (gtype, swap ? b : a);
-        b_ = gperl_convert_flags (gtype, swap ? a : b);
+        swap_ = SvTRUE (swap);
+        a_ = gperl_convert_flags (gtype, swap_ ? b : a);
+        b_ = gperl_convert_flags (gtype, swap_ ? a : b);
 
         switch (ix) {
           case 0: a_ |= b_; break;



--- Glib.pm 23 May 2008 09:49:02 +1000 1.121
+++ Glib.pm 20 Jun 2008 17:54:15 +1000
@@ -73,13 +73,16 @@
 
 use overload
    'bool' => \&bool,
-   '+'    => \&union,     '|'    => \&union,
-   '-'    => \&sub,
+   '+'    => \&union,     '+='   => \&union,
+   '|'    => \&union,     '|='   => \&union,
+   '-'    => \&sub,       '-='   => \&sub,
+   '*'    => \&intersect, '*='   => \&intersect,
+   '&'    => \&intersect, '&='   => \&intersect,
+   '/'    => \&xor,       '/='   => \&xor,
+   '^'    => \&xor,       '^='   => \&xor,
    '>='   => \&ge,
    '=='   => \&eq,        'eq'   => \&eq,
    '!='   => \&ne,        'ne'   => \&ne,
-   '*'    => \&intersect, '&'    => \&intersect,
-   '/'    => \&xor,       '^'    => \&xor,
    '@{}'  => \&as_arrayref,
    '""'   => sub { "[ @{$_[0]} ]" },
    fallback => 1;



--- c.t 23 May 2008 09:49:02 +1000 1.12
+++ c.t 20 Jun 2008 18:12:47 +1000
@@ -13,7 +13,7 @@
 
 #########################
 
-use Test::More tests => 32;
+use Test::More tests => 34;
 BEGIN { use_ok('Glib') };
 
 #########################
@@ -40,6 +40,16 @@
 eval { my $h = Glib::Flags->new (['readable']); };
 ok ($@, "Will croak on trying to create plain old Glib::Flags");
 
+{ $f = Glib::ParamFlags->new (['readable']); # with array
+  my $g = $f;
+  $g += 'writable';
+  ok ($g == ['readable', 'writable'],
+      "overloaded +=");
+  ok ($f == ['readable'],
+      "overloaded += leaves original unchanged");
+}
+
+
 #########################
 
 $@ = undef;


_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Glib::Flags overloaded mutators

by Torsten Schoenfeld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Kevin Ryde wrote:
> It'd be good if Glib::Flags had overloaded += as well as + so you could
> go
>
> $f += 'val';
>
> I think the basic overload can be used without too much trouble, the new
> object returned being assigned to the destination automatically by
> "overload".

Actually, I think this is supposed to work correctly right now.  The
"fallback => 1" argument to overload tells it to use "magic
autogeneration" of operators that aren't overloaded.  For example, "+="
is supposed to be automatically implemented with just "+".

Consequently, the new test cases you sent pass for me without the code
changes you also provided.

Is there something else I missed?

--
Bye,
-Torsten
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Glib::Flags overloaded mutators

by Kevin Ryde :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Torsten Schoenfeld <kaffeetisch@...> writes:
>
> Is there something else I missed?

I got a warning from the program below

    Use of uninitialized value in null operation at foo.pl line 13.

Maybe it only needs to watch for undef in the swap arg.


use strict;
use warnings;
use Data::Dumper;
use Gtk2 '-init';

my $w = Gtk2::Window->new ('toplevel');
my $f = $w->flags;
print Dumper($f);
print "\n";

print $f,"\n";

$f += 'can-focus';

print $f,"\n";

exit 0;

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Glib::Flags overloaded mutators

by Torsten Schoenfeld :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Kevin Ryde wrote:
> Torsten Schoenfeld <kaffeetisch@...> writes:
>> Is there something else I missed?
>
> I got a warning from the program below
>
>     Use of uninitialized value in null operation at foo.pl line 13.
>
> Maybe it only needs to watch for undef in the swap arg.

Ah, I see.  Yes, it needs to handle undef for swap.  This happens when
overload.pm uses a normal 'x' operator to automatically create a fallback
implementation for 'x=' operators.  Committed your original patch without the
Glib.pm changes.  Thanks!

--
Bye,
-Torsten
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list
LightInTheBox - Buy quality products at wholesale price