Glib-Ex-ObjectBits
view release on metacpan or search on metacpan
lib/Glib/Ex/FreezeNotify.pm view on Meta::CPAN
# Copyright 2008, 2009, 2010, 2011, 2012, 2014, 2024 Kevin Ryde
# This file is part of Glib-Ex-ObjectBits.
#
# Glib-Ex-ObjectBits is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Glib-Ex-ObjectBits is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Glib-Ex-ObjectBits. If not, see <http://www.gnu.org/licenses/>.
package Glib::Ex::FreezeNotify;
use 5.008;
use strict;
use warnings;
use Scalar::Util;
use Devel::GlobalDestruction 'in_global_destruction';
our $VERSION = 17;
sub new {
my $class = shift;
my $self = bless [], $class;
$self->add (@_);
return $self;
}
sub add {
my $self = shift;
### FreezeNotify add(): "@_"
foreach my $object (@_) {
$object->freeze_notify;
push @$self, $object;
Scalar::Util::weaken ($self->[-1]);
}
}
sub DESTROY {
my ($self) = @_;
### FreezeNotify DESTROY()
while (@$self) {
my $object = pop @$self;
if (defined $object # possible undef by weakening
&& ! in_global_destruction()) {
### FreezeNotify thaw: "$object"
$object->thaw_notify;
}
}
}
1;
__END__
=for stopwords Glib-Ex-ObjectBits FreezeNotify AtExit destructor Ryde ReleaseAction
=head1 NAME
Glib::Ex::FreezeNotify -- freeze Glib object property notifies by scope guard style
=for test_synopsis my ($obj, $obj1, $obj2)
=head1 SYNOPSIS
use Glib::Ex::FreezeNotify;
{ my $freezer = Glib::Ex::FreezeNotify->new ($obj);
$obj->set (foo => 123);
$obj->set (bar => 456);
# notify signals emitted when $freezer goes out of scope
}
# or multiple objects in one FreezeNotify
{
my $freezer = Glib::Ex::FreezeNotify->new ($obj1, $obj2);
$obj1->set (foo => 999);
$obj2->set (bar => 666);
}
=head1 DESCRIPTION
C<Glib::Ex::FreezeNotify> applies a C<freeze_notify()> to given objects,
with automatic corresponding C<thaw_notify()> at the end of a block, no
matter how it's exited, whether a C<goto>, early C<return>, C<die>, etc.
This protects against an error throw leaving the object permanently frozen.
Even in simple code an error can be thrown for a bad property name in a
C<set()>, or while calculating a value. (Though as of Glib-Perl 1.222 an
invalid argument type to C<set()> generally only provokes warnings.)
=head2 Operation
FreezeNotify works by having C<thaw_notify()> in the destroy code of the
FreezeNotify object.
FreezeNotify only holds weak references to its objects, so the mere fact
they're due for later thawing doesn't keep them alive if nothing else cares
whether they live or die. The effect is that frozen objects can be garbage
collected within a freeze block at the same point they would be without any
freezing, instead of extending their life to the end of the block.
It works to have multiple freeze/thaws, done either with FreezeNotify or
with other C<freeze_notify()> calls. C<Glib::Object> simply counts
outstanding freezes, which means they don't have to nest, so multiple
( run in 1.460 second using v1.01-cache-2.11-cpan-39bf76dae61 )