Tangence/Property.pm

filter: Statementstmt 95.7% 205 / 214 Branchbran 76.3% 55 / 72 Conditioncond 37.5% 3 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 97.6% 41 / 42 Podpod 0.0% 0 / 7 totaltotal 87.6% 305 / 348 CC 126 SCAR 14.7
CC
126
Cov
89%
__ANON__
9.1
new_prop_array
9.1
new_prop_hash
9.1
CRAP 4.4
« Tangence/Compiler/Parser.pm ↑ Summary Tangence/Stream.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2013-2024 -- leonerd@leonerd.org.uk
5  
6 14 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Tangence::Property 0.33;
11  
12 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use base qw( Tangence::Meta::Property );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 ▶use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 14 require Tangence::Type;
20  
21 14 ▶use Struct::Dumb;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 14 struct Instance => [qw( value callbacks cursors )];
23  
24 =head1 NAME
25  
26 C<Tangence::Property> - server implementation of a C<Tangence> property
27  
28 =head1 DESCRIPTION
29  
30 This module is a component of L<Tangence::Server>. It is not intended for
31 end-user use directly.
32  
33 =cut
34  
35 sub build_accessor
36 94 ▶{
Subroutine build_accessor: called CC=2 SCAR=7.2
Pod undocumented
37 94 my $prop = shift;
38 94 my ( $subs ) = @_;
39  
40 94 my $pname = $prop->name;
41 94 my $dim = $prop->dimension;
42  
43 3 ▶ $subs->{"new_prop_$pname"} = sub {
Subroutine __ANON__: called CC=7 SCAR=22.0
Subroutine new_prop_s_array: called CC=7 SCAR=22.0
Pod undocumented
44 3 my $self = shift;
45  
46 3 my $initial;
47  
48 3 ▶ if( my $code = $self->can( "init_prop_$pname" ) ) {
Branch: 10 branches
BranchTrueFalseTotal
if (my $code = $self->can("init_prop_$pname")) { } 0 3 3
elsif ($dim == 1) { } 2 1 3
elsif ($dim == 2) { } 1 0 1
elsif ($dim == 3 or $dim == 4) { } 0 0 0
elsif ($dim == 5) { } 0 0 0
if (my $code = $self->can("init_prop_$pname")) { } 52 64 116
elsif ($dim == 1) { } 26 36 62
elsif ($dim == 2) { } 10 26 36
elsif ($dim == 3 or $dim == 4) { } 13 13 26
elsif ($dim == 5) { } 13 0 13
49 0 $initial = $code->( $self );
50 }
51 elsif( $dim == DIM_SCALAR ) {
52 2 $initial = $prop->type->default_value;
53 }
54 elsif( $dim == DIM_HASH ) {
55 1 $initial = {};
56 }
57 ▶ elsif( $dim == DIM_QUEUE or $dim == DIM_ARRAY ) {
Condition33%2/6
$dim == 3 or $dim == 4
l!l&&r!l&&!r
000
$dim == 3 or $dim == 4
l!l&&r!l&&!r
01313
MC/DC0%0/2
$dim == 3 or $dim == 4
$dim == 3 $dim == 4
MC/DC50%1/2
$dim == 3 or $dim == 4
$dim == 3 $dim == 4
Truth table0%0/3
A or B
A= $dim == 3B= $dim == 4
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $dim == 3B= $dim == 4
ABresult
1X1
011
000
58 0 $initial = [];
59 }
60 elsif( $dim == DIM_OBJSET ) {
61 0 $initial = {}; # these have hashes internally
62 }
63 else {
64 0 croak "Unrecognised dimension $dim for property $pname";
65 }
66  
67 3 $self->{properties}->{$pname} = Instance( $initial, [], [] );
68 94 };
69  
70 4 ▶ $subs->{"get_prop_$pname"} = sub {
Subroutine get_prop_objects: called CC=1 SCAR=0.0
Subroutine get_prop_array: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
71 4 my $self = shift;
72 4 return $self->{properties}->{$pname}->value;
73 94 };
74  
75 10 ▶ $subs->{"set_prop_$pname"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
Subroutine set_prop_s_array: called CC=3 SCAR=11.0
Pod undocumented
76 10 my $self = shift;
77 10 my ( $newval ) = @_;
78 10 $self->{properties}->{$pname}->value = $newval;
79 10 my $cbs = $self->{properties}->{$pname}->callbacks;
80 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
81 10 ▶ : $_->{on_set}->( $self, $newval ) for @$cbs;
Branch: 2 branches
BranchTrueFalseTotal
$_->{'on_updated'} ? : 0 6 6
$_->{'on_updated'} ? : 5 10 15
82 94 };
83  
84 94 my $dimname = DIMNAMES->[$dim];
85 94 ▶ if( my $code = __PACKAGE__->can( "_accessor_for_$dimname" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = __PACKAGE__->can("_accessor_for_$dimname")) { } 94 0 94
86 94 $code->( $prop, $subs, $pname );
87 }
88 else {
89 0 croak "Unrecognised property dimension $dim for $pname";
90 }
91 }
92  
93 sub _accessor_for_scalar
94 32 ▶{
Subroutine _accessor_for_scalar: called CC=1 SCAR=0.0
95 # Nothing needed
96 }
97  
98 sub _accessor_for_hash
99 22 ▶{
Subroutine _accessor_for_hash: called CC=1 SCAR=0.0
100 22 my $prop = shift;
101 22 my ( $subs, $pname ) = @_;
102  
103 31 ▶ $subs->{"add_prop_$pname"} = sub {
Subroutine add_prop_objects: called CC=3 SCAR=11.0
Pod undocumented
104 31 my $self = shift;
105 31 my ( $key, $value ) = @_;
106 31 $self->{properties}->{$pname}->value->{$key} = $value;
107 31 my $cbs = $self->{properties}->{$pname}->callbacks;
108 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
109 31 ▶ : $_->{on_add}->( $self, $key, $value ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 2 6 8
110 22 };
111  
112 5 ▶ $subs->{"del_prop_$pname"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
113 5 my $self = shift;
114 5 my ( $key ) = @_;
115 5 delete $self->{properties}->{$pname}->value->{$key};
116 5 my $cbs = $self->{properties}->{$pname}->callbacks;
117 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
118 5 ▶ : $_->{on_del}->( $self, $key ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 1 3 4
119 22 };
120 }
121  
122 sub _accessor_for_queue
123 10 ▶{
Subroutine _accessor_for_queue: called CC=1 SCAR=0.0
124 10 my $prop = shift;
125 10 my ( $subs, $pname ) = @_;
126  
127 4 ▶ $subs->{"push_prop_$pname"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
128 4 my $self = shift;
129 4 my @values = @_;
130 4 push @{ $self->{properties}->{$pname}->value }, @values;
131 4 my $cbs = $self->{properties}->{$pname}->callbacks;
132 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
133 4 ▶ : $_->{on_push}->( $self, @values ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 1 4 5
134 10 };
135  
136 5 ▶ $subs->{"shift_prop_$pname"} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
137 5 my $self = shift;
138 5 my ( $count ) = @_;
139 5 ▶ $count = 1 unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 4 5
140 5 splice @{ $self->{properties}->{$pname}->value }, 0, $count, ();
141 5 my $cbs = $self->{properties}->{$pname}->callbacks;
142 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
143 5 ▶ : $_->{on_shift}->( $self, $count ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 2 5 7
144 5 my $cursors = $self->{properties}->{$pname}->cursors;
145 5 $_->idx -= $count for @$cursors;
146 10 };
147  
148 6 ▶ $subs->{"cursor_prop_$pname"} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
149 6 my $self = shift;
150 6 my ( $from ) = @_;
151 my $idx = $from == CUSR_FIRST ? 0 :
152 6 ▶ $from == CUSR_LAST ? scalar @{ $self->{properties}->{$pname}->value } :
Branch: 2 branches
BranchTrueFalseTotal
$from == 1 ? : 5 1 6
$from == 2 ? : 1 0 1
153 die "Unrecognised from";
154 6 ▶ my $cursors = $self->{properties}->{$pname}->cursors ||= [];
Condition50%1/2
$self->{"properties"}{$pname}->cursors ||= []
l!l
60
MC/DC0%0/1
$self->{"properties"}{$pname}->cursors ||= []
$self->{"properties"}{$pname}->cursors
Truth table50%1/2
A
A= $self->{"properties"}{$pname}->cursors
Aresult
11
00
155 6 push @$cursors, my $cursor = Tangence::Property::_Cursor->new( $self->{properties}->{$pname}->value, $prop, $idx );
156 6 return $cursor;
157 10 };
158  
159 6 ▶ $subs->{"uncursor_prop_$pname"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
160 6 my $self = shift;
161 6 my ( $cursor ) = @_;
162 6 ▶ my $cursors = $self->{properties}->{$pname}->cursors or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cursors = $self->{"properties"}{$pname}->cursors or return 6 0 6
163 6 @$cursors = grep { $_ != $cursor } @$cursors;
164 10 };
165 }
166  
167 sub _accessor_for_array
168 20 ▶{
Subroutine _accessor_for_array: called CC=1 SCAR=0.0
169 20 my $prop = shift;
170 20 my ( $subs, $pname ) = @_;
171  
172 4 ▶ $subs->{"push_prop_$pname"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
173 4 my $self = shift;
174 4 my @values = @_;
175 4 push @{ $self->{properties}->{$pname}->value }, @values;
176 4 my $cbs = $self->{properties}->{$pname}->callbacks;
177 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
178 4 ▶ : $_->{on_push}->( $self, @values ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 1 4 5
179 20 };
180  
181 3 ▶ $subs->{"shift_prop_$pname"} = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
182 3 my $self = shift;
183 3 my ( $count ) = @_;
184 3 ▶ $count = 1 unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
185 3 splice @{ $self->{properties}->{$pname}->value }, 0, $count, ();
186 3 my $cbs = $self->{properties}->{$pname}->callbacks;
187 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
188 3 ▶ : $_->{on_shift}->( $self, $count ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 2 3 5
189 20 };
190  
191 4 ▶ $subs->{"splice_prop_$pname"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
192 4 my $self = shift;
193 4 my ( $index, $count, @values ) = @_;
194 4 splice @{ $self->{properties}->{$pname}->value }, $index, $count, @values;
195 4 my $cbs = $self->{properties}->{$pname}->callbacks;
196 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
197 4 ▶ : $_->{on_splice}->( $self, $index, $count, @values ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 2 4 6
198 20 };
199  
200 6 ▶ $subs->{"move_prop_$pname"} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
201 6 my $self = shift;
202 6 my ( $index, $delta ) = @_;
203 6 ▶ return if $delta == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delta == 0 0 6 6
204 # it turns out that exchanging neighbours is quicker by list assignment,
205 # but other times it's generally best to use splice() to extract then
206 # insert
207 6 my $cache = $self->{properties}->{$pname}->value;
208 6 ▶ if( abs($delta) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $delta == 1) { } 1 5 6
209 1 @{$cache}[$index,$index+$delta] = @{$cache}[$index+$delta,$index];
210 }
211 else {
212 5 my $elem = splice @$cache, $index, 1, ();
213 5 splice @$cache, $index + $delta, 0, ( $elem );
214 }
215 6 my $cbs = $self->{properties}->{$pname}->callbacks;
216 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
217 6 ▶ : $_->{on_move}->( $self, $index, $delta ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 3 6 9
218 20 };
219 }
220  
221 sub _accessor_for_objset
222 10 ▶{
Subroutine _accessor_for_objset: called CC=1 SCAR=0.0
223 10 my $prop = shift;
224 10 my ( $subs, $pname ) = @_;
225  
226 # Different get and set methods
227 5 ▶ $subs->{"get_prop_$pname"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 5 my $self = shift;
229 5 return [ values %{ $self->{properties}->{$pname}->value } ];
230 10 };
231  
232 3 ▶ $subs->{"set_prop_$pname"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
233 3 my $self = shift;
234 3 my ( $newval ) = @_;
235 3 $self->{properties}->{$pname}->value = $newval;
236 3 my $cbs = $self->{properties}->{$pname}->callbacks;
237 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
238 3 ▶ : $_->{on_set}->( $self, [ values %$newval ] ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 0 2 2
239 10 };
240  
241 2 ▶ $subs->{"add_prop_$pname"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
242 2 my $self = shift;
243 2 my ( $obj ) = @_;
244 2 $self->{properties}->{$pname}->value->{$obj->id} = $obj;
245 2 my $cbs = $self->{properties}->{$pname}->callbacks;
246 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
247 2 ▶ : $_->{on_add}->( $self, $obj ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 0 2 2
248 10 };
249  
250 2 ▶ $subs->{"del_prop_$pname"} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
251 2 my $self = shift;
252 2 my ( $obj_or_id ) = @_;
253 2 ▶ my $id = ref $obj_or_id ? $obj_or_id->id : $obj_or_id;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj_or_id ? : 2 0 2
254 2 delete $self->{properties}->{$pname}->value->{$id};
255 2 my $cbs = $self->{properties}->{$pname}->callbacks;
256 $_->{on_updated} ? $_->{on_updated}->( $self, $self->{properties}->{$pname}->value )
257 2 ▶ : $_->{on_del}->( $self, $id ) for @$cbs;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'on_updated'} ? : 0 2 2
258 10 };
259 }
260  
261 sub make_type
262 31 ▶{
Subroutine make_type: called CC=1 SCAR=0.0
Pod undocumented
263 31 shift;
264 31 return Tangence::Type->make( @_ );
265 }
266  
267 class # hide from CPAN
268 Tangence::Property::_Cursor
269 {
270 14 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
271  
272 14 ▶ use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274 0 ▶ field $queue :param :reader;
Subroutine queue: not called CC=1 SCAR=2.6
275 6 ▶ field $prop :param :reader;
Subroutine prop: called CC=1 SCAR=0.0
276 2 ▶ field $idx :param :mutator;
Subroutine idx: called CC=1 SCAR=0.0
277  
278 6 ▶ sub BUILDARGS ( $class, $queue, $prop, $idx )
Subroutine BUILDARGS: called CC=1 SCAR=0.0
279 {
280 6 return ( queue => $queue, prop => $prop, idx => $idx );
281 }
282  
283 method handle_request_CUSR_NEXT
284 12 ▶ {
Subroutine handle_request_CUSR_NEXT: called CC=5 SCAR=16.1
285 12 my ( $ctx, $message ) = @_;
286  
287 12 my $direction = $message->unpack_int();
288 12 my $count = $message->unpack_int();
289  
290 12 my $start_idx = $idx;
291  
292 12 ▶ if( $direction == CUSR_FWD ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($direction == 1) { } 7 5 12
elsif ($direction == 2) { } 5 0 5
293 7 ▶ $count = scalar @$queue - $idx if $count > scalar @$queue - $idx;
Branch: 1 branch
BranchTrueFalseTotal
if $count > scalar @$queue - $idx 2 5 7
294  
295 7 $idx += $count;
296 }
297 elsif( $direction == CUSR_BACK ) {
298 5 ▶ $count = $idx if $count > $idx;
Branch: 1 branch
BranchTrueFalseTotal
if $count > $idx 1 4 5
299 5 $idx -= $count;
300 5 $start_idx = $idx;
301 }
302 else {
303 0 return $ctx->responderr( "Unrecognised cursor direction $direction" );
304 }
305  
306 12 my @result = @{$queue}[$start_idx .. $start_idx + $count - 1];
307  
308 12 $ctx->respond( Tangence::Message->new( $ctx->stream, MSG_CUSR_RESULT )
309 ->pack_int( $start_idx )
310 ->pack_all_sametype( $prop->type, @result )
311 );
312 14 }
313 }
314  
315 14 =head1 AUTHOR
316  
317 Paul Evans <leonerd@leonerd.org.uk>
318  
319 =cut
320  
321 14 0x55AA;
« Tangence/Compiler/Parser.pm ↑ Summary Tangence/Stream.pm »