lib/TUI/Objects/NSCollection.pm

filter: Statementstmt 95.2% 202 / 212 Branchbran 62.0% 31 / 50 Conditioncond 50.0% 7 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 97.5% 39 / 40 Podpod 15.3% 4 / 26 totaltotal 81.1% 284 / 350 CC 39 SCAR 10.9
CC
39
Cov
87%
setLimit
6.3
__ANON__
5.4
from
4.1
CRAP 3.0
« lib/TUI/Memory.pm ↑ Summary lib/TUI/Drivers/HWMouse.pm »
1 package TUI::Objects::NSCollection;
2 # ABSTRACT: defines the class TNSCollection
3
4 43 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 43 our $VERSION = '2.000002';
9 43 $VERSION =~ tr/_//d;
10 43 our $AUTHORITY = 'cpan:BRICKPOOL';
11
12 43 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 43 our @EXPORT = qw(
14 TNSCollection
15 new_TNSCollection
16 );
17
18 43 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 43 ▶use Errno qw( EFAULT EINVAL );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 43 ▶use Hash::Util::FieldHash qw( id );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 43 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 43 ▶use TUI::toolkit;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 43 ▶use TUI::toolkit::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
24 is_Object
25 :types
26 43 );
27
28 43 ▶use TUI::Objects::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
29 ccNotFound
30 maxCollectionSize
31 43 );
32 43 ▶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 43 extends TObject;
38
39 # predeclare global variable
40 43 our %ITEMS = ();
41
42 # protected attributes
43 22 ▶has items => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 43 has count => ( is => 'ro', default => 0 );
45 43 has limit => ( is => 'ro', default => 0 );
46 43 has delta => ( is => 'ro', default => 0 );
47 43 has shouldDelete => ( is => 'ro', default => false );
48
49 # predeclare private methods
50 my (
51 43 $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 22 ▶sub BUILD { # void (\%args)
Subroutine BUILD: called CC=3 SCAR=11.2
Pod documented
68 22 my ( $self, $args ) = @_;
69 22 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 22 22
70 22 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 22 22
71 22 $self->setLimit( $self->{limit} );
72 22 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 22 ▶sub DEMOLISH { # void ($in_global_destruction)
Subroutine DEMOLISH: called CC=3 SCAR=11.2
Pod documented
92 22 my ( $self, $in_global_destruction ) = @_;
93 22 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 22 22
94 22 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 22 22
95 22 $self->shutDown();
96 22 return;
97 }
98
99 23 ▶sub shutDown { # void ()
Subroutine shutDown: called CC=2 SCAR=7.1
Pod documented
100 23 state $sig = signature(
101 method => Object,
102 pos => [],
103 );
104 23 my ( $self ) = $sig->( @_ );
105 23 ▶ if ( $self->{shouldDelete} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'shouldDelete'}) { } 0 23 23
106 0 $self->freeAll();
107 }
108 else {
109 23 $self->removeAll();
110 }
111 23 $self->setLimit( 0 );
112 23 $self->SUPER::shutDown();
113 23 return;
114 } #/ sub shutDown
115
116 17 ▶sub at { # $item ($index)
Subroutine at: called CC=3 SCAR=12.0
Pod undocumented
117 17 state $sig = signature(
118 method => Object,
119 pos => [Int],
120 );
121 17 my ( $self, $index ) = $sig->( @_ );
122 $self->error( EINVAL, "Index out of bounds" )
123 17 ▶ if $index < 0 || $index >= $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $self->{"count"} 0 17 17
Condition33%1/3
$index < 0 or $index >= $self->{"count"}
l!l&&r!l&&!r
0017
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 17 return $ITEMS{ $self->{items}->[$index] };
125 }
126
127 5 ▶sub atRemove { # void ($index)
Subroutine atRemove: called CC=3 SCAR=11.4
Pod undocumented
128 5 state $sig = signature(
129 method => Object,
130 pos => [Int],
131 );
132 5 my ( $self, $index ) = $sig->( @_ );
133 $self->error( EINVAL, "Index out of bounds" )
134 5 ▶ if $index < 0 || $index >= $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $self->{"count"} 0 5 5
Condition33%1/3
$index < 0 or $index >= $self->{"count"}
l!l&&r!l&&!r
005
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 5 $self->{count}--;
136 5 splice( @{ $self->{items} }, $index, 1 );
137 5 return;
138 }
139
140 1 ▶sub atFree { # void ($index)
Subroutine atFree: called CC=1 SCAR=0.0
Pod undocumented
141 1 state $sig = signature(
142 method => Object,
143 pos => [Int],
144 );
145 1 my ( $self, $index ) = $sig->( @_ );
146 1 my $item = $self->at( $index );
147 1 $self->atRemove( $index );
148 1 $self->$freeItem( $item );
149 1 return;
150 }
151
152 19 ▶sub atInsert { # void ($index, $item|undef)
Subroutine atInsert: called CC=4 SCAR=13.9
Pod undocumented
153 19 state $sig = signature(
154 method => Object,
155 pos => [Int, Item],
156 );
157 19 my ( $self, $index, $item ) = $sig->( @_ );
158 19 ▶ $self->error( EINVAL, "Index out of bounds" )
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 19 19
159 if $index < 0;
160 $self->setLimit( $self->{count} + $self->{delta} )
161 19 ▶ if $self->{count} == $self->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"count"} == $self->{"limit"} 1 18 19
162
163 19 ▶ my $id = id($item) || 0;
Condition100%2/2
&id($item) || 0
l!l
181
MC/DC100%1/1
&id($item) || 0
&id($item)
Truth table100%2/2
A
A= &id($item)
Aresult
11
00
164 19 $ITEMS{ $id } = $item;
165 19 $self->{count}++;
166
167 19 splice( @{ $self->{items} }, $index, 0, $id );
168 19 return;
169 }
170
171 1 ▶sub atPut { # void ($index, $item|undef)
Subroutine atPut: called CC=3 SCAR=11.2
Pod undocumented
172 1 state $sig = signature(
173 method => Object,
174 pos => [Int, Item],
175 );
176 1 my ( $self, $index, $item ) = $sig->( @_ );
177 $self->error( EINVAL, "Index out of bounds" )
178 1 ▶ if $index >= $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $index >= $self->{"count"} 0 1 1
179
180 1 ▶ my $id = id($item) || 0;
Condition50%1/2
&id($item) || 0
l!l
10
MC/DC0%0/1
&id($item) || 0
&id($item)
Truth table50%1/2
A
A= &id($item)
Aresult
11
00
181 1 $ITEMS{ $id } = $item;
182 1 $self->{items}->[$index] = $id;
183 1 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 1 ▶sub freeAll { # void ()
Subroutine freeAll: called CC=2 SCAR=6.9
Pod undocumented
219 1 state $sig = signature(
220 method => Object,
221 pos => [],
222 );
223 1 my ( $self ) = $sig->( @_ );
224 $self->$freeItem( $self->at( $_ ) )
225 1 for 0 .. $self->{count} - 1;
226 1 $self->{count} = 0;
227 1 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 48 ▶sub setLimit { # void ($aLimit)
Subroutine setLimit: called CC=6 SCAR=18.4
Pod undocumented
329 48 state $sig = signature(
330 method => Object,
331 pos => [Int],
332 );
333 48 my ( $self, $aLimit ) = $sig->( @_ );
334 48 ▶ $aLimit = $self->{count} if $aLimit < $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $aLimit < $self->{"count"} 0 48 48
335 48 ▶ $aLimit = maxCollectionSize if $aLimit > maxCollectionSize;
Branch: 1 branch
BranchTrueFalseTotal
if $aLimit > '2305843009213693952' 0 48 48
336 48 ▶ if ( $aLimit != $self->{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aLimit != $self->{"limit"}) 9 39 48
337 9 my $size = @{ $self->{items} };
338 9 ▶ if ( $aLimit > $size ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($aLimit > $size) { } 1 8 9
elsif ($aLimit < $size) { } 0 8 8
339 1 my $n = $aLimit - $size;
340 1 push( @{ $self->{items} }, ( 0 ) x $n );
341 } elsif ( $aLimit < $size ) {
342 0 splice( @{ $self->{items} }, $aLimit );
343 }
344 9 $self->{limit} = $aLimit;
345 }
346 48 return;
347 } #/ sub setLimit
348
349 0 ▶sub getCount { # $count ()
Subroutine getCount: not called CC=1 SCAR=6.9
Pod undocumented
350 0 state $sig = signature(
351 method => Object,
352 pos => [],
353 );
354 0 my ( $self ) = $sig->( @_ );
355 0 return $self->{count};
356 };
357
358 4 ▶$freeItem = sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
359 4 my ( $self, $item ) = @_;
360 4 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 4 4
361 4 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 4 4
362 4 ▶ my $id = id($item) || 0;
Condition50%1/2
&id($item) || 0
l!l
40
MC/DC0%0/1
&id($item) || 0
&id($item)
Truth table50%1/2
A
A= &id($item)
Aresult
11
00
363 4 ▶ delete $ITEMS{ $id } if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 4 0 4
364 4 return;
365 43 };
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 43 1
376
377 __END__
« lib/TUI/Memory.pm ↑ Summary lib/TUI/Drivers/HWMouse.pm »