| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||||||||||
| 3 | # File: GX/Callback/Hook/Queue.pm | ||||||||||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package GX::Callback::Hook::Queue; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 41 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 41 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 41 | ▶ | use GX::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 41 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 17 | # Constructor | ||||||||||||||||||||||||||||
| 18 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 19 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 19 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 19 | my $self = bless [ [ undef ] ], $class; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 19 | return $self; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 32 | # Public methods | ||||||||||||||||||||||||||||
| 33 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 32 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 32 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 32 | for ( @_ ) { | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 55 | ▶ | if ( ! blessed $_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 55 | ▶ | if ( $_->isa( 'GX::Callback' ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 46 | 7 | my $callback = $_; | |||||||||||||||||||||||||||
| 47 | 7 | push @$self, [ $callback ]; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif ( $_->isa( 'GX::Callback::Hook' ) ) { | ||||||||||||||||||||||||||||
| 50 | 48 | my $hook = $_; | |||||||||||||||||||||||||||
| 51 | 48 | my $hook_name = $hook->name; | |||||||||||||||||||||||||||
| 52 | 48 | my @callbacks = $hook->all; | |||||||||||||||||||||||||||
| 53 | 48 | ▶ | push @$self, map { [ $_, $hook, $hook_name ] } ( @callbacks ? @callbacks : ( undef ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||
| 56 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 32 | return; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | @$self = ( [ undef ] ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | return; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return bless [ @$self ], ref $self; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 48 | ▶ | sub current { | ||||||||||||||||||||||||||
|
Subroutine
current: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 48 | ▶ | return ( $_[0]->[0] || return )->[0]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[0][0] || (return)
MC/DC0%0/1
$_[0][0] || (return)
$_[0][0]
Truth table50%1/2
A
A= $_[0][0]
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 36 | ▶ | sub current_hook { | ||||||||||||||||||||||||||
|
Subroutine
current_hook: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 36 | ▶ | return ( $_[0]->[0] || return )->[1]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[0][0] || (return)
MC/DC0%0/1
$_[0][0] || (return)
$_[0][0]
Truth table50%1/2
A
A= $_[0][0]
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 38 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 38 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 38 | my $current = shift @$self; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 38 | ▶ | while ( @$self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 37 | ▶ | return $self->[0][0] || shift( @$self ) && next; | ||||||||||||||||||||||||||
|
Condition80%4/5
$self->[0][0] || shift @$self && (next)
shift @$self and next
MC/DC0%0/2
$self->[0][0] || shift @$self && (next)
$self->[0][0]
shift @$self
Truth table66%2/3
A || B
A= $self->[0][0]B= shift @$self
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 9 | @$self = ( $current ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 9 | return; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 35 | ▶ | sub all { | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 35 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 35 | ▶ | if ( @$self > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 29 | return grep { defined } map { $_->[0] } @$self[ 1 .. $#$self ]; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 6 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 20 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 20 | my $count = @$self; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 20 | for ( @_ ) { | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 20 | ▶ | if ( ! defined $_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 20 | ▶ | if ( blessed $_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 14 | ▶ | if ( $_->isa( 'GX::Callback' ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 139 | 7 | my $callback = $_; | |||||||||||||||||||||||||||
| 140 | 7 | @$self = ( shift( @$self ), grep( { $_->[0] != $callback } @$self ) ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | elsif ( $_->isa( 'GX::Callback::Hook' ) ) { | ||||||||||||||||||||||||||||
| 143 | 7 | my $hook = $_; | |||||||||||||||||||||||||||
| 144 | 7 | @$self = ( shift( @$self ), grep( { $_->[1] != $hook } @$self ) ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||
| 147 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||||||||||
| 152 | 6 | my $hook_name = $_; | |||||||||||||||||||||||||||
| 153 | 41 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 154 | 6 | @$self = ( shift( @$self ), grep( { $_->[2] ne $hook_name } @$self ) ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 20 | return $count - @$self; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 4 | ▶ | sub remove_all { | ||||||||||||||||||||||||||
|
Subroutine
remove_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | @$self = ( $self->[0] ); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 4 | return; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 2 | ▶ | sub replace_all { | ||||||||||||||||||||||||||
|
Subroutine
replace_all: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | $self->remove_all; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 2 | eval { | |||||||||||||||||||||||||||
| 180 | 2 | $self->add( @_ ); | |||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 2 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | complain $@; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | return; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 32 | ▶ | sub skip_to { | ||||||||||||||||||||||||||
|
Subroutine
skip_to: called CC=16 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 32 | my $self = shift; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 32 | ▶ | if ( @_ != 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | complain "Invalid number of arguments"; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 32 | my $position; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 32 | ▶ | if ( blessed $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 25 | ▶ | if ( $_[0]->isa( 'GX::Callback' ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 12 | my $callback = $_[0]; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 12 | my $i = 0; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 12 | for ( @$self ) { | |||||||||||||||||||||||||||
| 210 | 29 | ▶ | $position = $i, last if defined( $_->[0] ) && $_->[0] == $callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_->[0] and $_->[0] == $callback
MC/DC100%2/2
defined $_->[0] and $_->[0] == $callback
defined $_->[0]
$_->[0] == $callback
Truth table100%3/3
A and B
A= defined $_->[0]B= $_->[0] == $callback
| |||||||||||||||||||||||||||||
| 211 | 17 | $i++; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | elsif ( $_[0]->isa( 'GX::Callback::Hook' ) ) { | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 13 | my $hook = $_[0]; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 13 | my $i = 0; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 13 | for ( @$self ) { | |||||||||||||||||||||||||||
| 222 | 37 | ▶ | $position = $i, last if defined( $_->[1] ) && $_->[1] == $hook; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_->[1] and $_->[1] == $hook
MC/DC100%2/2
defined $_->[1] and $_->[1] == $hook
defined $_->[1]
$_->[1] == $hook
Truth table100%3/3
A and B
A= defined $_->[1]B= $_->[1] == $hook
| |||||||||||||||||||||||||||||
| 223 | 26 | $i++; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | else { | ||||||||||||||||||||||||||||
| 228 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | elsif ( defined $_[0] ) { | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 7 | my $hook_name = $_[0]; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 7 | my $i = 0; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 7 | for ( @$self ) { | |||||||||||||||||||||||||||
| 239 | 41 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | 25 | ▶ | $position = $i, last if $_->[2] eq $hook_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 22 | $i++; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | else { | ||||||||||||||||||||||||||||
| 246 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 32 | ▶ | if ( $position ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 25 | ▶ | if ( $position > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 12 | splice( @$self, 1, $position - 1 ); | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 25 | return 1; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 7 | return; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 41 | 1; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | __END__ | ||||||||||||||||||||||||||||