lib/TUI/Objects/NSCollection.pm

filter: Statementstmt 98.1% 208 / 212 Branchbran 68.0% 34 / 50 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 40 / 40 Podpod 15.3% 4 / 26 totaltotal 85.1% 298 / 350 CC 39 SCAR 10.7
CC
39
Cov
91%
setLimit
6.0
__ANON__
5.0
from
4.1
CRAP 2.9
« lib/TUI/Drivers/HWMouse.pm ↑ Summary lib/TUI/Editors/Util.pm »
1 package TUI::Objects::NSCollection;
2 # ABSTRACT: defines the class TNSCollection
3
4 52 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 52 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 52 our $VERSION = '2.000003';
9 52 $VERSION =~ tr/_//d;
10 52 our $AUTHORITY = 'cpan:BRICKPOOL';
11
12 52 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 52 our @EXPORT = qw(
14 TNSCollection
15 new_TNSCollection
16 );
17
18 52 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 52 ▶use Errno qw( EFAULT EINVAL );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 52 ▶use Hash::Util::FieldHash qw( id );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 52 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 52 ▶use TUI::toolkit;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 52 ▶use TUI::toolkit::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
24 is_Object
25 :types
26 52 );
27
28 52 ▶use TUI::Objects::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
29 ccNotFound
30 maxCollectionSize
31 52 );
32 52 ▶use TUI::Objects::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
33
34 sub TNSCollection() { __PACKAGE__ }
35 1 ▶sub new_TNSCollection { __PACKAGE__->from(@_) }
Subroutine new_TNSCollection: called CC=1 SCAR=0.0
Pod undocumented
36
37 52 extends TObject;
38
39 # predeclare global variable
40 52 our %ITEMS = ();
41
42 # protected attributes
43 28 ▶has items => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 52 has count => ( is => 'ro', default => 0 );
45 52 has limit => ( is => 'ro', default => 0 );
46 52 has delta => ( is => 'ro', default => 0 );
47 52 has shouldDelete => ( is => 'ro', default => false );
48
49 # predeclare private methods
50 my (
51 52 $freeItem,
52 );
53
54 18 ▶sub BUILDARGS { # \%args (|%args)
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
55 18 state $sig = signature(
56 method => 1,
57 named => [
58 limit => Int, { alias => 'aLimit', optional => 1 },
59 delta => Int, { alias => 'aDelta', optional => 1 },
60 ],
61 caller_level => +1,
62 );
63 18 my ( $class, $args ) = $sig->( @_ );
64 18 return { %$args };
65 }
66
67 28 ▶sub BUILD { # void (\%args)
Subroutine BUILD: called CC=3 SCAR=11.2
Pod documented
68 28 my ( $self, $args ) = @_;
69 28 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 28 28
70 28 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 28 28
71 28 $self->setLimit( $self->{limit} );
72 28 return;
73 } #/ sub BUILD
74
75 6 ▶sub from { # $obj (|$aLimit, $aDelta)
Subroutine from: called CC=4 SCAR=14.2
Pod undocumented
76 6 state $sig = signature(
77 method => 1,
78 pos => [
79 Int, { optional => 1 },
80 Int, { optional => 1 },
81 ],
82 );
83 6 my ( $class, @args ) = $sig->( @_ );
84 6 SWITCH: for ( scalar @args ) {
85 6 ▶ $_ == 0 and return $class->new();
Branch: 1 branch
BranchTrueFalseTotal
$_ == 0 and return $class->new 4 2 6
86 2 ▶ $_ == 2 and return $class->new( limit => $args[0], delta => $args[1] );
Branch: 1 branch
BranchTrueFalseTotal
$_ == 2 and return $class->new("limit", $args[0], "delta", $args[1]) 2 0 2
87 }
88 0 return;
89 }
90
91 28 ▶sub DEMOLISH { # void ($in_global_destruction)
Subroutine DEMOLISH: called CC=3 SCAR=11.2
Pod documented
92 28 my ( $self, $in_global_destruction ) = @_;
93 28 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 28 28
94 28 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 28 28
95 28 $self->shutDown();
96 28 return;
97 }
98
99 29 ▶sub shutDown { # void ()
Subroutine shutDown: called CC=2 SCAR=6.9
Pod documented
100 29 state $sig = signature(
101 method => Object,
102 pos => [],
103 );
104 29 my ( $self ) = $sig->( @_ );
105 29 ▶ if ( $self->{shouldDelete} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'shouldDelete'}) { } 6 23 29
106 6 $self->freeAll();
107 }
108 else {
109 23 $self->removeAll();
110 }
111 29 $self->setLimit( 0 );
112 29 $self->SUPER::shutDown();
113 29 return;
114 } #/ sub shutDown
115
116 46 ▶sub at { # $item ($index)
Subroutine at: called CC=3 SCAR=12.0
Pod undocumented
117 46 state $sig = signature(
118 method => Object,
119 pos => [Int],
120 );
121 46 my ( $self, $index ) = $sig->( @_ );
122 $self->error( EINVAL, "Index out of bounds" )
123 46 ▶ if $index < 0 || $index >= $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $self->{"count"} 0 46 46
Condition33%1/3
$index < 0 or $index >= $self->{"count"}
l!l&&r!l&&!r
0046
MC/DC0%0/2
$index < 0 or $index >= $self->{"count"}
$index < 0 $index >= $self->{"count"}
Truth table33%1/3
A or B
A= $index < 0B= $index >= $self->{"count"}
ABresult
1X1
011
000
124 46 return $ITEMS{ $self->{items}->[$index] };
125 }
126
127 6 ▶sub atRemove { # void ($index)
Subroutine atRemove: called CC=3 SCAR=11.4
Pod undocumented
128 6 state $sig = signature(
129 method => Object,
130 pos => [Int],
131 );
132 6 my ( $self, $index ) = $sig->( @_ );
133 $self->error( EINVAL, "Index out of bounds" )
134 6 ▶ if $index < 0 || $index >= $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $self->{"count"} 0 6 6
Condition33%1/3
$index < 0 or $index >= $self->{"count"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$index < 0 or $index >= $self->{"count"}
$index < 0 $index >= $self->{"count"}
Truth table33%1/3
A or B
A= $index < 0B= $index >= $self->{"count"}
ABresult
1X1
011
000
135 6 $self->{count}--;
136 6 splice( @{ $self->{items} }, $index, 1 );
137 6 return;
138 }
139
140 2 ▶sub atFree { # void ($index)
Subroutine atFree: called CC=1 SCAR=0.0
Pod undocumented
141 2 state $sig = signature(
142 method => Object,
143 pos => [Int],
144 );
145 2 my ( $self, $index ) = $sig->( @_ );
146 2 my $item = $self->at( $index );
147 2 $self->atRemove( $index );
148 2 $self->$freeItem( $item );
149 2 return;
150 }
151
152 31 ▶sub atInsert { # void ($index, $item|undef)
Subroutine atInsert: called CC=4 SCAR=13.9
Pod undocumented
153 31 state $sig = signature(
154 method => Object,
155 pos => [Int, Item],
156 );
157 31 my ( $self, $index, $item ) = $sig->( @_ );
158 31 ▶ $self->error( EINVAL, "Index out of bounds" )
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 31 31
159 if $index < 0;
160 $self->setLimit( $self->{count} + $self->{delta} )
161 31 ▶ if $self->{count} == $self->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"count"} == $self->{"limit"} 1 30 31
162
163 31 ▶ my $id = id($item) || 0;
Condition100%2/2
&id($item) || 0
l!l
301
MC/DC100%1/1
&id($item) || 0
&id($item)
Truth table100%2/2
A
A= &id($item)
Aresult
11
00
164 31 $ITEMS{ $id } = $item;
165 31 $self->{count}++;
166
167 31 splice( @{ $self->{items} }, $index, 0, $id );
168 31 return;
169 }
170
171 6 ▶sub atPut { # void ($index, $item|undef)
Subroutine atPut: called CC=3 SCAR=11.0
Pod undocumented
172 6 state $sig = signature(
173 method => Object,
174 pos => [Int, Item],
175 );
176 6 my ( $self, $index, $item ) = $sig->( @_ );
177 $self->error( EINVAL, "Index out of bounds" )
178 6 ▶ if $index >= $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $index >= $self->{"count"} 0 6 6
179
180 6 ▶ my $id = id($item) || 0;
Condition100%2/2
&id($item) || 0
l!l
42
MC/DC100%1/1
&id($item) || 0
&id($item)
Truth table100%2/2
A
A= &id($item)
Aresult
11
00
181 6 $ITEMS{ $id } = $item;
182 6 $self->{items}->[$index] = $id;
183 6 return;
184 }
185
186 3 ▶sub remove { # void ($item)
Subroutine remove: called CC=1 SCAR=0.0
Pod undocumented
187 3 state $sig = signature(
188 method => Object,
189 pos => [Item],
190 );
191 3 my ( $self, $item ) = $sig->( @_ );
192 3 $self->atRemove( $self->indexOf( $item ) );
193 3 return;
194 }
195
196 24 ▶sub removeAll { # void ()
Subroutine removeAll: called CC=1 SCAR=0.0
Pod undocumented
197 24 state $sig = signature(
198 method => Object,
199 pos => [],
200 );
201 24 my ( $self ) = $sig->( @_ );
202 24 $self->{count} = 0;
203 24 $self->{items} = [];
204 24 return;
205 }
206
207 1 ▶sub free { # void ($item)
Subroutine free: called CC=1 SCAR=0.0
Pod undocumented
208 1 state $sig = signature(
209 method => Object,
210 pos => [Item],
211 );
212 1 my ( $self, $item ) = $sig->( @_ );
213 1 $self->remove( $item );
214 1 $self->$freeItem( $item );
215 1 return;
216 }
217
218 8 ▶sub freeAll { # void ()
Subroutine freeAll: called CC=2 SCAR=6.9
Pod undocumented
219 8 state $sig = signature(
220 method => Object,
221 pos => [],
222 );
223 8 my ( $self ) = $sig->( @_ );
224 $self->$freeItem( $self->at( $_ ) )
225 8 for 0 .. $self->{count} - 1;
226 8 $self->{count} = 0;
227 8 return;
228 }
229
230 6 ▶sub indexOf { # $index ($item|undef)
Subroutine indexOf: called CC=4 SCAR=14.1
Pod undocumented
231 6 state $sig = signature(
232 method => Object,
233 pos => [Item],
234 );
235 6 my ( $self, $item ) = $sig->( @_ );
236 6 for my $i ( 0 .. $self->{count} - 1 ) {
237 7 ▶ my $id = id($item) || 0;
Condition50%1/2
&id($item) || 0
l!l
70
MC/DC0%0/1
&id($item) || 0
&id($item)
Truth table50%1/2
A
A= &id($item)
Aresult
11
00
238 7 ▶ return $i if $self->{items}->[$i] eq $id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"items"}[$i] eq $id 5 2 7
239 }
240 1 $self->error( EFAULT, "Item not found" );
241 0 return ccNotFound;
242 }
243
244 3 ▶sub insert { # $index ($item|undef)
Subroutine insert: called CC=1 SCAR=0.0
Pod undocumented
245 3 state $sig = signature(
246 method => Object,
247 pos => [Item],
248 );
249 3 my ( $self, $item ) = $sig->( @_ );
250 3 my $loc = $self->{count};
251 3 $self->atInsert( $self->{count}, $item );
252 3 return $loc;
253 }
254
255 2 ▶sub error { # void ($code, $info)
Subroutine error: called CC=1 SCAR=0.0
Pod undocumented
256 2 state $sig = signature(
257 method => Object,
258 pos => [Int, Str],
259 );
260 2 my ( $self, $code, $info ) = $sig->( @_ );
261 2 Carp::croak sprintf("Error code: %d, Info: %s\n", $code, $info);
262 }
263
264 1 ▶sub firstThat { # $item|undef (\&Test, $arg|undef)
Subroutine firstThat: called CC=3 SCAR=11.3
Pod undocumented
265 1 state $sig = signature(
266 method => Object,
267 pos => [CodeRef, Any],
268 );
269 1 my ( $self, $Test, $arg ) = $sig->( @_ );
270 1 my $that;
271 1 for my $i ( 0 .. $self->{count} - 1 ) {
272 1 local $_ = $ITEMS{ $self->{items}->[$i] };
273 1 ▶ return $_ if $Test->( $_, $arg );
Branch: 1 branch
BranchTrueFalseTotal
if &$Test($_, $arg) 1 0 1
274 }
275 0 return undef;
276 }
277
278 1 ▶sub lastThat { # $item|undef (\&Test, $arg|undef)
Subroutine lastThat: called CC=3 SCAR=11.3
Pod undocumented
279 1 state $sig = signature(
280 method => Object,
281 pos => [CodeRef, Any],
282 );
283 1 my ( $self, $Test, $arg ) = $sig->( @_ );
284 1 my $that;
285 1 for my $i ( reverse 0 .. $self->{count} - 1 ) {
286 1 local $_ = $ITEMS{ $self->{items}->[$i] };
287 1 ▶ return $_ if $Test->( $_, $arg );
Branch: 1 branch
BranchTrueFalseTotal
if &$Test($_, $arg) 1 0 1
288 }
289 0 return undef;
290 }
291
292 1 ▶sub forEach { # void (\&action, $arg|undef)
Subroutine forEach: called CC=2 SCAR=6.9
Pod undocumented
293 1 state $sig = signature(
294 method => Object,
295 pos => [CodeRef, Any],
296 );
297 1 my ( $self, $action, $arg ) = $sig->( @_ );
298 1 for my $i ( 0 .. $self->{count} - 1 ) {
299 2 local $_ = $ITEMS{ $self->{items}->[$i] };
300 2 $action->( $_, $arg );
301 }
302 1 return;
303 }
304
305 1 ▶sub pack { # void ()
Subroutine pack: called CC=4 SCAR=13.9
Pod undocumented
306 1 state $sig = signature(
307 method => Object,
308 pos => [],
309 );
310 1 my ( $self ) = $sig->( @_ );
311 1 my $count = 0;
312 1 for my $i ( 0 .. $self->{count} - 1 ) {
313 3 ▶ if ( $ITEMS{ $self->{items}->[$i] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TUI::Objects::NSCollection::ITEMS{$self->{'items'}[$i]}) { } 2 1 3
314 2 $count++;
315 }
316 else {
317 1 splice( @{ $self->{items} }, $i, 1 );
318 }
319 }
320 1 ▶ if ( $self->{count} != $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"count"} != $count) 1 0 1
321 1 my $n = $self->{count} - $count;
322 1 push( @{ $self->{items} }, ( 0 ) x $n );
323 1 $self->{count} = $count;
324 }
325 1 return;
326 }
327
328 61 ▶sub setLimit { # void ($aLimit)
Subroutine setLimit: called CC=6 SCAR=17.9
Pod undocumented
329 61 state $sig = signature(
330 method => Object,
331 pos => [Int],
332 );
333 61 my ( $self, $aLimit ) = $sig->( @_ );
334 61 ▶ $aLimit = $self->{count} if $aLimit < $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $aLimit < $self->{"count"} 0 61 61
335 61 ▶ $aLimit = maxCollectionSize if $aLimit > maxCollectionSize;
Branch: 1 branch
BranchTrueFalseTotal
if $aLimit > '2305843009213693952' 0 61 61
336 61 ▶ if ( $aLimit != $self->{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aLimit != $self->{"limit"}) 16 45 61
337 16 my $size = @{ $self->{items} };
338 16 ▶ if ( $aLimit > $size ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($aLimit > $size) { } 2 14 16
elsif ($aLimit < $size) { } 6 8 14
339 2 my $n = $aLimit - $size;
340 2 push( @{ $self->{items} }, ( 0 ) x $n );
341 } elsif ( $aLimit < $size ) {
342 6 splice( @{ $self->{items} }, $aLimit );
343 }
344 16 $self->{limit} = $aLimit;
345 }
346 61 return;
347 } #/ sub setLimit
348
349 6 ▶sub getCount { # $count ()
Subroutine getCount: called CC=1 SCAR=0.0
Pod undocumented
350 6 state $sig = signature(
351 method => Object,
352 pos => [],
353 );
354 6 my ( $self ) = $sig->( @_ );
355 6 return $self->{count};
356 };
357
358 18 ▶$freeItem = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
359 18 my ( $self, $item ) = @_;
360 18 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 18 18
361 18 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 18 18
362 18 ▶ my $id = id($item) || 0;
Condition100%2/2
&id($item) || 0
l!l
162
MC/DC100%1/1
&id($item) || 0
&id($item)
Truth table100%2/2
A
A= &id($item)
Aresult
11
00
363 18 ▶ delete $ITEMS{ $id } if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 16 2 18
364 18 return;
365 52 };
366
367 1 ▶sub STORABLE_freeze { # @list ($cloning)
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
368 ...
369 1 }
370
371 1 ▶sub STORABLE_attach { # $obj ($cloning, $serialized)
Subroutine STORABLE_attach: called CC=1 SCAR=0.0
Pod undocumented
372 ...
373 1 }
374
375 52 1
376
377 __END__
« lib/TUI/Drivers/HWMouse.pm ↑ Summary lib/TUI/Editors/Util.pm »