| 1 | # Copyright 2008, 2009, 2010, 2011, 2012, 2014, 2016, 2024 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Glib-Ex-ObjectBits. | ||
| 4 | # | ||
| 5 | # Glib-Ex-ObjectBits is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by the | ||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Glib-Ex-ObjectBits is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Glib-Ex-ObjectBits. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package Glib::Ex::SignalIds; | ||
| 19 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | 0 | ▶ | use Glib; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 24 | use Scalar::Util; | ||
| 25 | use Devel::GlobalDestruction 'in_global_destruction'; | ||
| 26 | |||
| 27 | our $VERSION = 17; | ||
| 28 | |||
| 29 | sub new { | ||
| 30 | my ($class, $object, @ids) = @_; | ||
| 31 | |||
| 32 | # it's easy to forget the object in the call (and pass only the IDs), so | ||
| 33 | # validate the first arg now | ||
| 34 | (Scalar::Util::blessed($object) && $object->isa('Glib::Object')) | ||
| 35 | or croak 'Glib::Ex::SignalIds->new(): first param must be the target object'; | ||
| 36 | |||
| 37 | my $self = bless [ $object ], $class; | ||
| 38 | Scalar::Util::weaken ($self->[0]); | ||
| 39 | $self->add (@ids); | ||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | sub add { | ||
| 43 | my ($self, @ids) = @_; | ||
| 44 | push @$self, @ids; # grep {$_} @ids; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub DESTROY { | ||
| 48 | my ($self) = @_; | ||
| 49 | unless (in_global_destruction()) { | ||
| 50 | $self->disconnect; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub object { | ||
| 55 | my ($self) = @_; | ||
| 56 | return $self->[0]; | ||
| 57 | } | ||
| 58 | sub ids { | ||
| 59 | my ($self) = @_; | ||
| 60 | return @{$self}[1..$#$self]; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub disconnect { | ||
| 64 | my ($self) = @_; | ||
| 65 | my $object = $self->[0] || return; # target object already destroyed | ||
| 66 | |||
| 67 | while (@$self > 1) { | ||
| 68 | my $id = pop @$self; | ||
| 69 | |||
| 70 | # might have been disconnected by $object in the course of its destruction | ||
| 71 | if ($object->signal_handler_is_connected ($id)) { | ||
| 72 | $object->signal_handler_disconnect ($id); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | __END__ | ||