GtkGC shared auto-release



For interest, this is the operative part of the Gtk2::GC shared gc pool
auto-release I mentioned I tried.  The concept is that the GC returned
by the "get" is released automatically when no longer used (from perl),
instead of having to do an explicit Gtk2::GC->release.

The pool get/release maintains a kind of extra reference count on the GC
(on top of its ordinary GObject ref count), so the theory is to hide
that, and from perl just forget a shared GC when you don't want it any
more.

These couple of lines seem to work, and yes it has to be DESTROY, not
FINALIZE_INSTANCE (or so I believe and found).  But maybe there's an
easier way I haven't realized.  If the concept is sound enough what I
don't have is a particularly good name, something suggestive of release
or shared or pool, but still close to the GtkGC or Gtk2::GC which is its
basis.


package Gtk2::Ex::GtkGC;
use strict;
use warnings;
use Carp;

our @ISA = ('Gtk2::Gdk::GC');

use constant DEBUG => 1;

sub get {
  my $class = shift;
  my $self = Gtk2::GC->get (@_);
  $self->{'release_count'}++;
  return bless $self, $class;  # rebless
}

sub DESTROY {
  my ($self) = @_;
  if (DEBUG) { print "$self destroy, release count ",
                 ($self->{'release_count'}||0), "\n"; }
  while ($self->{'release_count'} > 0) {
    $self->{'release_count'}--;
    Gtk2::GC->release ($self);
  }
  $self->SUPER::DESTROY;
}



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