On Apr 30, 2008, at 9:44 PM, Kevin Ryde wrote:
> Is there a way to make a Glib::Flags object, or rather a subclass like
> Gtk2::Gdk::EventMask, from an array of elements like ['a','b','c']?
> Which is to say, perl level access to gperl_convert_flags() basically.
>
> I know an array is converted automatically on input to various funcs,
> but I'd like to do some arithmetic setting and clearing elements, and
> thought to use the flags object overloads, as long as one of my
> operands
> is an object.
Seems like a reasonable thing to want to do. The big question is,
what kind of syntax would you expect for this? Here's one possible way:
#!/usr/bin/perl -w
use Glib qw(1.173); # the value field is relatively new
use Gtk2;
#
# pretend you didn't see this. it takes advantage of knowledge of the
internal
# representation of GFlags values in the bindings.
#
sub Glib::Flags::bless {
my ($val, $type) = @_;
my %valmap = (map { ($_->{name} => $_->{value}),
($_->{nick} => $_->{value})
} Glib::Type->list_values ($type));
my $num = 0;
$num += $valmap{$_} foreach ('ARRAY' eq ref $val) ? @$val : $val;
return bless \$num, $type;
}
# a plain-jane array of modifier enum value names
my $modifiers = [ qw(GDK_CONTROL_MASK shift-mask) ];
# give if the Glib::Flags magic
$modifiers = Glib::Flags::bless $modifiers, 'Gtk2::Gdk::ModifierType';
# Now the Glib::Flags operator overloads work.
print "$modifiers\n";
if ($modifiers >= 'control-mask') {
print "yay, we're evil!\n";
}
__END__
Of course, it would be much more efficient if implemented as a line or
two of XS code...
--
Diane, ten-oh-three, Great Northern Hotel.
Sheriff Truman and I have just been with the one-armed man, or what's
left of him.
In another time, another culture, he may have been a seer, or a shaman
priest, but in our world, he's a shoe salesman, and lives among the
shadows.
-- Special Agent Dale Cooper
_______________________________________________
gtk-perl-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gtk-perl-list