[RFC][PATCH] Small enhancement for Glib::Object::Subclass



Hello All,

When subclassing a widget, it's probably not uncommon that setting or
getting at least one property needs to be treated specialy, but for some
other properties the default behaviour (just storing it in the hash) is
ok. Or perhaps even for the special property get can do with the default
implementation. Therefore I suggest following enhancement for the
default SET_PROPERTY/GET_PROPERTY methods:

If SET_<property-name>/GET_<property-name> method exists, it is called.
Otherwise, the property is set to/retrieved from the hash.

I would actualy like set_/get_ prefixes better (such methods are more
suited to be called directly), but the following patch uses SET_/GET_
because that is less likely to break existing stuff.

PS: Should I add the patch to the BTS on sourceforge too?

Note: The code for doc is syntacticaly incorrect, but one line shorter. Feel
free to use the actual code. Or perhaps a real pseudocode.

-------------------------------------------------------------------------------
                                                 Jan 'Bulb' Hudec <bulb ucw cz>

--- orig/Subclass.pm    2004-04-12 01:03:07.000000000 +0200
+++ mod/Subclass.pm     2004-05-11 17:52:12.000000000 +0200
@@ -146,7 +146,12 @@
 The default implementation looks like this:
 
    my ($self, $pspec) = @_;
-   return $self->{$pspec->get_name};
+   my $name = $pspec->get_name;
+   if($self->can("GET_$name")) {
+       $self->GET_$name();
+   } else {
+       $self->{$name};
+   }
 
 =item SET_PROPERTY $self, $pspec, $newval                 [not a method]
 
@@ -165,7 +170,12 @@
 The default C<SET_PROPERTY> looks like this:
 
    my ($self, $pspec, $newval) = @_;
-   $self->{$pspec->get_name} = $newval;
+   my $name = $pspec->get_name;
+   if($self->can("SET_$name")) {
+       $self->SET_$name($newval);
+   } else {
+       $self->{$pspec->get_name} = $newval;
+   }
 
 =item FINALIZE_INSTANCE $self                             [not a method]
 
@@ -200,12 +210,24 @@
 
 sub GET_PROPERTY {
    my ($self, $pspec) = @_;
-   $self->{$pspec->get_name};
+   my $name = $pspec->get_name;
+   if($self->can("GET_$name")) {
+       $name = "GET_$name";
+       $self->$name();
+   } else {
+       $self->{$name};
+   }
 }
 
 sub SET_PROPERTY {
    my ($self, $pspec, $newval) = @_;
-   $self->{$pspec->get_name} = $newval;
+   my $name = $pspec->get_name;
+   if($self->can("SET_$name")) {
+       $name = "SET_$name";
+       $self->$name($newval);
+   } else {
+       $self->{$pspec->get_name} = $newval;
+   }
 }
 
 sub import {

Attachment: signature.asc
Description: Digital signature



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