Tangence/Object.pm

filter: Statementstmt 96.0% 193 / 201 Branchbran 65.6% 63 / 96 Conditioncond 63.6% 7 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 97.3% 37 / 38 Podpod 59.0% 13 / 22 totaltotal 84.0% 315 / 375 CC 58 SCAR 13.4
CC
58
Cov
85%
watch_property
8.2
handle_request_GETPROPELEM
6.3
new
5.2
CRAP 3.8
« Tangence/Stream.pm ↑ Summary Tangence/Server.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, 2010-2021 -- leonerd@leonerd.org.uk
5  
6 package Tangence::Object 0.33;
7  
8 14 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 14 ▶use Tangence::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 14 ▶use Tangence::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 14 ▶use Tangence::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 14 Tangence::Class->declare(
23 __PACKAGE__,
24  
25 events => {
26 destroy => {
27 args => [],
28 },
29 },
30 );
31  
32 =head1 NAME
33  
34 C<Tangence::Object> - base class for accessible objects in a C<Tangence> server
35  
36 =head1 DESCRIPTION
37  
38 This class acts as a base class for the accessible objects in a L<Tangence>
39 server. All the objects actually created and made accessible to clients will
40 be subclasses of this one, including internally-created objects such as
41 L<Tangence::Registry>.
42  
43 These objects are not directly constructed by calling the C<new> class method;
44 instead the C<Tangence::Registry> should be used to construct one.
45  
46 =cut
47  
48 25 ▶sub new ( $class, %args )
Subroutine new: called CC=5 SCAR=16.5
Pod undocumented
49 {
50 25 ▶ defined( my $id = delete $args{id} ) or croak "Need a id";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $id = delete $args{"id"}) or croak("Need a id") 25 0 25
51 25 ▶ my $registry = delete $args{registry} or croak "Need a registry";
Branch: 1 branch
BranchTrueFalseTotal
my $registry = delete $args{"registry"} or croak("Need a registry") 25 0 25
52  
53 my $self = bless {
54 id => $id,
55 registry => $registry,
56 25 ▶ meta => $args{meta} || Tangence::Class->for_perlname( $class ),
Condition66%2/3
$args{'meta'} || 'Tangence::Class'->for_perlname($class)
l!l&&r!l&&!r
11140
MC/DC0%0/2
$args{'meta'} || 'Tangence::Class'->for_perlname($class)
$args{'meta'} 'Tangence::Class'->for_perlname($class)
Truth table66%2/3
A || B
A= $args{'meta'}B= 'Tangence::Class'->for_perlname($class)
ABresult
1X1
011
000
57  
58 event_subs => {}, # {$event} => [ @cbs ]
59  
60 properties => {}, # {$prop} => T:P::Instance struct
61 }, $class;
62  
63 25 my $properties = $self->class->properties;
64 25 foreach my $prop ( keys %$properties ) {
65 117 my $meth = "new_prop_$prop";
66 117 $self->$meth();
67 }
68  
69 25 return $self;
70 }
71  
72 =head1 METHODS
73  
74 =cut
75  
76 =head2 destroy
77  
78 $obj->destroy;
79  
80 Requests that the object destroy itself, informing all clients that are aware
81 of it. Once they all report that they have dropped the object, the object is
82 deconstructed for real.
83  
84 Not to be confused with Perl's own C<DESTROY> method.
85  
86 =cut
87  
88 2 ▶sub destroy ( $self, %args )
Subroutine destroy: called CC=2 SCAR=6.9
Pod documented
89 {
90 2 $self->{destroying} = 1;
91  
92 2 my $outstanding = 1;
93  
94 2 my $on_destroyed = $args{on_destroyed};
95  
96 2 ▶ my $incsub = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 2 $outstanding++
98 2 };
99  
100 4 ▶ my $decsub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
101 4 ▶ --$outstanding and return;
Branch: 1 branch
BranchTrueFalseTotal
--$outstanding and return 2 2 4
102 2 $self->_destroy_really;
103 2 ▶ $on_destroyed->() if $on_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $on_destroyed 2 0 2
104 2 };
105  
106 2 foreach my $cb ( @{ $self->{event_subs}->{destroy} } ) {
107 2 $cb->( $self, $incsub, $decsub );
108 }
109  
110 2 $decsub->();
111 }
112  
113 sub _destroy_really
114 2 ▶{
Subroutine _destroy_really: called CC=1 SCAR=0.0
115 2 my $self = shift;
116  
117 2 $self->registry->destroy_object( $self );
118  
119 2 undef %$self; # Now I am dead
120 2 $self->{destroyed} = 1;
121 }
122  
123 =head2 id
124  
125 $id = $obj->id;
126  
127 Returns the object's C<Tangence> ID number
128  
129 =cut
130  
131 sub id
132 106 ▶{
Subroutine id: called CC=1 SCAR=0.0
Pod documented
133 106 my $self = shift;
134 106 return $self->{id};
135 }
136  
137 =head2 describe
138  
139 $description = $obj->describe;
140  
141 Returns a textual description of the object, for internal debugging purposes.
142 Subclasses are encouraged to override this method to return something more
143 descriptive within their domain of interest
144  
145 =cut
146  
147 sub describe
148 12 ▶{
Subroutine describe: called CC=1 SCAR=0.0
Pod documented
149 12 my $self = shift;
150 12 return ref $self;
151 }
152  
153 =head2 registry
154  
155 $registry = $obj->registry;
156  
157 Returns the L<Tangence::Registry> that constructed this object.
158  
159 =cut
160  
161 sub registry
162 3 ▶{
Subroutine registry: called CC=1 SCAR=0.0
Pod documented
163 3 my $self = shift;
164 3 return $self->{registry};
165 }
166  
167 11 ▶sub smash ( $self, $smashkeys )
Subroutine smash: called CC=4 SCAR=15.2
Pod undocumented
168 {
169 11 ▶ return undef unless $smashkeys and @$smashkeys;
Branch: 1 branch
BranchTrueFalseTotal
unless $smashkeys and @$smashkeys 0 11 11
Condition33%1/3
$smashkeys and @$smashkeys
!ll&&!rl&&r
0011
MC/DC0%0/2
$smashkeys and @$smashkeys
$smashkeys @$smashkeys
Truth table33%1/3
A and B
A= $smashkeysB= @$smashkeys
ABresult
0X0
100
111
170  
171 11 my @keys;
172 11 ▶ if( ref $smashkeys eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $smashkeys eq 'HASH') { } 0 11 11
173 0 @keys = keys %$smashkeys;
174 }
175 else {
176 11 @keys = @$smashkeys;
177 }
178  
179 return { map {
180 11 my $m = "get_prop_$_";
181 21 $_ => $self->$m()
182 } @keys };
183 }
184  
185 =head2 class
186  
187 $class = $obj->class;
188  
189 Returns the L<Tangence::Meta::Class> object representing the class of this
190 object.
191  
192 =cut
193  
194 sub class
195 273 ▶{
Subroutine class: called CC=2 SCAR=7.2
Pod documented
196 273 my $self = shift;
197 273 ▶ return ref $self ? $self->{meta} : Tangence::Class->for_perlname( $self );
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 273 0 273
198 }
199  
200 =head2 can_method
201  
202 $method = $obj->can_method( $name );
203  
204 Returns the L<Tangence::Meta::Method> object representing the named method, or
205 C<undef> if no such method exists.
206  
207 =cut
208  
209 sub can_method
210 5 ▶{
Subroutine can_method: called CC=1 SCAR=0.0
Pod documented
211 5 my $self = shift;
212 5 return $self->class->method( @_ );
213 }
214  
215 =head2 can_event
216  
217 $event = $obj->can_event( $name );
218  
219 Returns the L<Tangence::Meta::Event> object representing the named event, or
220 C<undef> if no such event exists.
221  
222 =cut
223  
224 sub can_event
225 48 ▶{
Subroutine can_event: called CC=1 SCAR=0.0
Pod documented
226 48 my $self = shift;
227 48 return $self->class->event( @_ );
228 }
229  
230 =head2 can_property
231  
232 $property = $obj->can_property( $name );
233  
234 Returns the L<Tangence::Meta::Property> object representing the named
235 property, or C<undef> if no such property exists.
236  
237 =cut
238  
239 sub can_property
240 170 ▶{
Subroutine can_property: called CC=1 SCAR=0.0
Pod documented
241 170 my $self = shift;
242 170 return $self->class->property( @_ );
243 }
244  
245 sub smashkeys
246 1 ▶{
Subroutine smashkeys: called CC=1 SCAR=0.0
Pod undocumented
247 1 my $self = shift;
248 1 return $self->class->smashkeys;
249 }
250  
251 =head2 fire_event
252  
253 $obj->fire_event( $event, @args );
254  
255 Fires the named event on the object. Each event subscription function will be
256 invoked with the given arguments.
257  
258 =cut
259  
260 18 ▶sub fire_event ( $self, $event, @args )
Subroutine fire_event: called CC=5 SCAR=16.4
Pod documented
261 {
262 18 ▶ $event eq "destroy" and croak "$self cannot fire destroy event directly";
Branch: 1 branch
BranchTrueFalseTotal
$event eq "destroy" and croak("$self cannot fire destroy event directly") 0 18 18
263  
264 18 ▶ $self->can_event( $event ) or croak "$self has no event $event";
Branch: 1 branch
BranchTrueFalseTotal
$self->can_event($event) or croak("$self has no event $event") 18 0 18
265  
266 18 ▶ my $sublist = $self->{event_subs}->{$event} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sublist = $self->{"event_subs"}{$event} or return 3 15 18
267  
268 3 foreach my $cb ( @$sublist ) {
269 3 $cb->( $self, @args );
270 }
271 }
272  
273 =head2 subscribe_event
274  
275 $id = $obj->subscribe_event( $event, $callback );
276  
277 Subscribes an event-handling callback CODE ref to the named event. When the
278 event is fired by C<fire_event> this callback will be invoked, being passed
279 the object reference and the event's arguments.
280  
281 $callback->( $obj, @args );
282  
283 Returns an opaque ID value that can be used to remove this subscription by
284 calling C<unsubscribe_event>.
285  
286 =cut
287  
288 23 ▶sub subscribe_event ( $self, $event, $callback )
Subroutine subscribe_event: called CC=3 SCAR=11.0
Pod documented
289 {
290 23 ▶ $self->can_event( $event ) or croak "$self has no event $event";
Branch: 1 branch
BranchTrueFalseTotal
$self->can_event($event) or croak("$self has no event $event") 23 0 23
291  
292 23 ▶ my $sublist = ( $self->{event_subs}->{$event} ||= [] );
Condition100%2/2
$self->{"event_subs"}{$event} ||= []
l!l
221
MC/DC100%1/1
$self->{"event_subs"}{$event} ||= []
$self->{"event_subs"}{$event}
Truth table100%2/2
A
A= $self->{"event_subs"}{$event}
Aresult
11
00
293  
294 23 push @$sublist, $callback;
295  
296 23 my $ref = \@{$sublist}[$#$sublist]; # reference to last element
297 23 return $ref + 0; # force numeric context
298 }
299  
300 =head2 unsubscribe_event
301  
302 $obj->unsubscribe_event( $event, $id );
303  
304 Removes an event-handling callback previously registered with
305 C<subscribe_event>.
306  
307 =cut
308  
309 6 ▶sub unsubscribe_event ( $self, $event, $id )
Subroutine unsubscribe_event: called CC=4 SCAR=14.0
Pod documented
310 {
311 6 ▶ my $sublist = $self->{event_subs}->{$event} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sublist = $self->{"event_subs"}{$event} or return 6 0 6
312  
313 6 my $index;
314 6 ▶ for( $index = 0; $index < @$sublist; $index++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$sublist) 4 2 6
315 4 ▶ last if \@{$sublist}[$index] + 0 == $id;
Branch: 1 branch
BranchTrueFalseTotal
if \@{$sublist;}[$index] + 0 == $id 4 0 4
316 }
317  
318 6 splice @$sublist, $index, 1, ();
319 }
320  
321 =head2 watch_property
322  
323 $id = $obj->watch_property( $prop, %callbacks );
324  
325 Watches a named property for changes, registering a set of callback functions
326 to be invoked when the property changes in certain ways. The set of callbacks
327 required depends on the dimension of the property being watched.
328  
329 For all property types:
330  
331 $on_set->( $obj, $value );
332  
333 For hash properties:
334  
335 $on_add->( $obj, $key, $value );
336 $on_del->( $obj, $key );
337  
338 For queue properties:
339  
340 $on_push->( $obj, @values );
341 $on_shift->( $obj, $count );
342  
343 For array properties:
344  
345 $on_push->( $obj, @values );
346 $on_shift->( $obj, $count );
347 $on_splice->( $obj, $index, $count, @values );
348 $on_move->( $obj, $index, $delta );
349  
350 For objset properties:
351  
352 $on_add->( $obj, $added_object );
353 $on_del->( $obj, $deleted_object_id );
354  
355 Alternatively, a single callback may be installed that is invoked after any
356 change of the property, being passed the new value entirely:
357  
358 $on_updated->( $obj, $value );
359  
360 Returns an opaque ID value that can be used to remove this watch by calling
361 C<unwatch_property>.
362  
363 =cut
364  
365 47 ▶sub watch_property ( $self, $prop, %callbacks )
Subroutine watch_property: called CC=8 SCAR=21.0
Pod documented
366 {
367 47 ▶ my $pdef = $self->can_property( $prop ) or croak "$self has no property $prop";
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($prop) or croak("$self has no property $prop") 47 0 47
368  
369 47 my $callbacks = {};
370 47 my $on_updated;
371  
372 47 ▶ if( $callbacks{on_updated} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callbacks{'on_updated'}) { } 4 43 47
373 4 $on_updated = delete $callbacks{on_updated};
374 4 ▶ ref $on_updated eq "CODE" or croak "Expected 'on_updated' to be a CODE ref";
Branch: 1 branch
BranchTrueFalseTotal
ref $on_updated eq "CODE" or croak("Expected 'on_updated' to be a CODE ref") 4 0 4
375 4 ▶ keys %callbacks and croak "Expected no key other than 'on_updated'";
Branch: 1 branch
BranchTrueFalseTotal
keys %callbacks and croak("Expected no key other than 'on_updated'") 0 4 4
376 4 $callbacks->{on_updated} = $on_updated;
377 }
378 else {
379 43 foreach my $name ( @{ CHANGETYPES->{$pdef->dimension} } ) {
380 121 ▶ ref( $callbacks->{$name} = delete $callbacks{$name} ) eq "CODE"
Branch: 1 branch
BranchTrueFalseTotal
ref($callbacks->{$name} = delete $callbacks{$name}) eq "CODE" or croak("Expected '${name}' as a CODE ref") 121 0 121
381 or croak "Expected '$name' as a CODE ref";
382 }
383 }
384  
385 47 my $watchlist = $self->{properties}->{$prop}->callbacks;
386  
387 47 push @$watchlist, $callbacks;
388  
389 47 ▶ $on_updated->( $self, $self->{properties}->{$prop}->value ) if $on_updated;
Branch: 1 branch
BranchTrueFalseTotal
if $on_updated 4 43 47
390  
391 47 my $ref = \@{$watchlist}[$#$watchlist]; # reference to last element
392 47 return $ref + 0; # force numeric context
393 }
394  
395 =head2 unwatch_property
396  
397 $obj->unwatch_property( $prop, $id );
398  
399 Removes the set of callback functions previously registered with
400 C<watch_property>.
401  
402 =cut
403  
404 16 ▶sub unwatch_property ( $self, $prop, $id )
Subroutine unwatch_property: called CC=4 SCAR=14.0
Pod documented
405 {
406 16 ▶ my $watchlist = $self->{properties}->{$prop}->callbacks or return;
Branch: 1 branch
BranchTrueFalseTotal
my $watchlist = $self->{"properties"}{$prop}->callbacks or return 16 0 16
407  
408 16 my $index;
409 16 ▶ for( $index = 0; $index < @$watchlist; $index++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$watchlist) 14 2 16
410 14 ▶ last if \@{$watchlist}[$index] + 0 == $id;
Branch: 1 branch
BranchTrueFalseTotal
if \@{$watchlist;}[$index] + 0 == $id 14 0 14
411 }
412  
413 16 splice @$watchlist, $index, 1, ();
414 }
415  
416 ### Message handling
417  
418 3 ▶sub handle_request_CALL ( $self, $ctx, $message )
Subroutine handle_request_CALL: called CC=4 SCAR=13.9
Pod undocumented
419 {
420 3 my $method = $message->unpack_str();
421  
422 3 ▶ my $mdef = $self->can_method( $method ) or die "Object cannot respond to method $method\n";
Branch: 1 branch
BranchTrueFalseTotal
my $mdef = $self->can_method($method) or die "Object cannot respond to method $method\n" 3 0 3
423  
424 3 my $m = "method_$method";
425 3 ▶ $self->can( $m ) or die "Object cannot run method $method\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->can($m) or die "Object cannot run method $method\n" 3 0 3
426  
427 3 my @args = map { $_->unpack_value( $message ) } $mdef->argtypes;
428  
429 3 my $result = $self->$m( $ctx, @args );
430  
431 3 my $response = Tangence::Message->new( $ctx->stream, MSG_RESULT );
432 3 ▶ $mdef->ret->pack_value( $response, $result ) if $mdef->ret;
Branch: 1 branch
BranchTrueFalseTotal
if $mdef->ret 2 1 3
433  
434 3 return $response;
435 }
436  
437 2 ▶sub generate_message_EVENT ( $self, $conn, $event, @args )
Subroutine generate_message_EVENT: called CC=3 SCAR=11.1
Pod undocumented
438 {
439 2 ▶ my $edef = $self->can_event( $event ) or die "Object cannot respond to event $event";
Branch: 1 branch
BranchTrueFalseTotal
my $edef = $self->can_event($event) or die "Object cannot respond to event $event" 2 0 2
440  
441 2 my $response = Tangence::Message->new( $conn, MSG_EVENT )
442 ->pack_int( $self->id )
443 ->pack_str( $event );
444  
445 2 my @argtypes = $edef->argtypes;
446 2 $argtypes[$_]->pack_value( $response, $args[$_] ) for 0..$#argtypes;
447  
448 2 return $response;
449 }
450  
451 4 ▶sub handle_request_GETPROP ( $self, $ctx, $message )
Subroutine handle_request_GETPROP: called CC=3 SCAR=11.1
Pod undocumented
452 {
453 4 my $prop = $message->unpack_str();
454  
455 4 ▶ my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop";
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($prop) or die "Object does not have property $prop" 4 0 4
456  
457 4 my $m = "get_prop_$prop";
458 4 ▶ $self->can( $m ) or die "Object cannot get property $prop\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->can($m) or die "Object cannot get property $prop\n" 4 0 4
459  
460 4 my $result = $self->$m();
461  
462 4 my $response = Tangence::Message->new( $ctx->stream, MSG_RESULT );
463 4 $pdef->overall_type->pack_value( $response, $result );
464  
465 4 return $response;
466 }
467  
468 4 ▶sub handle_request_GETPROPELEM ( $self, $ctx, $message )
Subroutine handle_request_GETPROPELEM: called CC=6 SCAR=18.3
Pod undocumented
469 {
470 4 my $prop = $message->unpack_str();
471  
472 4 ▶ my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop";
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($prop) or die "Object does not have property $prop" 4 0 4
473 4 my $dim = $pdef->dimension;
474  
475 4 my $m = "get_prop_$prop";
476 4 ▶ $self->can( $m ) or die "Object cannot get property $prop\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->can($m) or die "Object cannot get property $prop\n" 4 0 4
477  
478 4 my $result;
479 match( $dim : == ) {
480 case( DIM_QUEUE ), case( DIM_ARRAY ) {
481 2 my $idx = $message->unpack_int();
482 2 ▶ $result = $self->$m()->[$idx];
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 2 ? : 2 0 2
483 }
484 case( DIM_HASH ) {
485 2 my $key = $message->unpack_str();
486 2 $result = $self->$m()->{$key};
487 }
488 4 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 3 || $(Syntax::Keyword::Match/topic) == 4 ? : 2 2 4
Condition66%2/3
$(Syntax::Keyword::Match/topic) == 3 or $(Syntax::Keyword::Match/topic) == 4
l!l&&r!l&&!r
022
MC/DC50%1/2
$(Syntax::Keyword::Match/topic) == 3 or $(Syntax::Keyword::Match/topic) == 4
$(Syntax::Keyword::Match/topic) == 3 $(Syntax::Keyword::Match/topic) == 4
Truth table66%2/3
A or B
A= $(Syntax::Keyword::Match/topic) == 3B= $(Syntax::Keyword::Match/topic) == 4
ABresult
1X1
011
000
489 0 die "Property $prop cannot fetch elements";
490 }
491 }
492  
493 4 my $response = Tangence::Message->new( $ctx->stream, MSG_RESULT );
494 4 $pdef->type->pack_value( $response, $result );
495  
496 4 return $response;
497 }
498  
499 8 ▶sub handle_request_SETPROP ( $self, $ctx, $message )
Subroutine handle_request_SETPROP: called CC=3 SCAR=11.2
Pod undocumented
500 {
501 8 my $prop = $message->unpack_str();
502  
503 8 ▶ my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop\n";
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($prop) or die "Object does not have property $prop\n" 8 0 8
504  
505 8 my $value = $pdef->overall_type->unpack_value( $message );
506  
507 7 my $m = "set_prop_$prop";
508 7 ▶ $self->can( $m ) or die "Object cannot set property $prop\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->can($m) or die "Object cannot set property $prop\n" 7 0 7
509  
510 7 $self->$m( $value );
511  
512 7 return Tangence::Message->new( $self, MSG_OK );
513 }
514  
515 45 ▶sub generate_message_UPDATE ( $self, $conn, $prop, $how, @args )
Subroutine generate_message_UPDATE: called CC=4 SCAR=14.1
Pod undocumented
516 {
517 45 ▶ my $pdef = $self->can_property( $prop ) or die "Object does not have property $prop\n";
Branch: 1 branch
BranchTrueFalseTotal
my $pdef = $self->can_property($prop) or die "Object does not have property $prop\n" 45 0 45
518 45 my $dim = $pdef->dimension;
519  
520 45 my $message = Tangence::Message->new( $conn, MSG_UPDATE )
521 ->pack_int( $self->id )
522 ->pack_str( $prop );
523 45 TYPE_U8->pack_value( $message, $how );
524  
525 45 my $dimname = DIMNAMES->[$dim];
526 45 ▶ if( $how == CHANGE_SET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($how == 1) { } 22 23 45
elsif (my $code = $self->can("_generate_message_UPDATE_$dimname")) { } 23 0 23
527 22 my ( $value ) = @args;
528 22 $pdef->overall_type->pack_value( $message, $value );
529 }
530 elsif( my $code = $self->can( "_generate_message_UPDATE_$dimname" ) ) {
531 23 $code->( $self, $message, $how, $pdef, @args );
532 }
533 else {
534 0 croak "Unrecognised property dimension $dim for $prop";
535 }
536  
537 45 return $message;
538 }
539  
540 0 ▶sub _generate_message_UPDATE_scalar ( $self, $message, $how, $pdef, @args )
Subroutine _generate_message_UPDATE_scalar: not called CC=1 SCAR=6.9
541 {
542 0 croak "Change type $how is not valid for a scalar property";
543 }
544  
545 6 ▶sub _generate_message_UPDATE_hash ( $self, $message, $how, $pdef, @args )
Subroutine _generate_message_UPDATE_hash: called CC=3 SCAR=11.2
546 {
547 match( $how : == ) {
548 case( CHANGE_ADD ) {
549 4 my ( $key, $value ) = @args;
550 4 $message->pack_str( $key );
551 4 ▶ $pdef->type->pack_value( $message, $value );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 3 ? : 2 0 2
552 }
553 case( CHANGE_DEL ) {
554 2 my ( $key ) = @args;
555 2 $message->pack_str( $key );
556 }
557 6 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 2 ? : 4 2 6
558 0 croak "Change type $how is not valid for a hash property";
559 }
560 }
561 }
562  
563 6 ▶sub _generate_message_UPDATE_queue ( $self, $message, $how, $pdef, @args )
Subroutine _generate_message_UPDATE_queue: called CC=3 SCAR=11.3
564 {
565 match( $how : == ) {
566 case( CHANGE_PUSH ) {
567 3 ▶ $message->pack_all_sametype( $pdef->type, @args );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 5 ? : 3 0 3
568 }
569 case( CHANGE_SHIFT ) {
570 3 my ( $count ) = @args;
571 3 $message->pack_int( $count );
572 }
573 6 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 4 ? : 3 3 6
574 0 croak "Change type $how is not valid for a queue property";
575 }
576 }
577 }
578  
579 9 ▶sub _generate_message_UPDATE_array ( $self, $message, $how, $pdef, @args )
Subroutine _generate_message_UPDATE_array: called CC=5 SCAR=16.1
580 {
581 match( $how : == ) {
582 case( CHANGE_PUSH ) {
583 3 ▶ $message->pack_all_sametype( $pdef->type, @args );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 5 ? : 1 5 6
584 }
585 case( CHANGE_SHIFT ) {
586 1 my ( $count ) = @args;
587 1 ▶ $message->pack_int( $count );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 6 ? : 2 3 5
588 }
589 case( CHANGE_SPLICE ) {
590 2 my ( $start, $count, @values ) = @args;
591 2 $message->pack_int( $start );
592 2 $message->pack_int( $count );
593 2 ▶ $message->pack_all_sametype( $pdef->type, @values );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 7 ? : 3 0 3
594 }
595 case( CHANGE_MOVE ) {
596 3 my ( $index, $delta ) = @args;
597 3 $message->pack_int( $index );
598 3 $message->pack_int( $delta );
599 }
600 9 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 4 ? : 3 6 9
601 0 croak "Change type $how is not valid for an array property";
602 }
603 }
604 }
605  
606 2 ▶sub _generate_message_UPDATE_objset ( $self, $message, $how, $pdef, @args )
Subroutine _generate_message_UPDATE_objset: called CC=3 SCAR=11.2
607 {
608 match( $how : == ) {
609 case( CHANGE_ADD ) {
610 1 my ( $value ) = @args;
611 1 ▶ $pdef->type->pack_value( $message, $value );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 3 ? : 1 0 1
612 }
613 case( CHANGE_DEL ) {
614 1 my ( $id ) = @args;
615 1 $message->pack_int( $id );
616 }
617 2 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 2 ? : 1 1 2
618 0 croak "Change type $how is not valid for an objset property";
619 }
620 }
621 }
622  
623 =head1 AUTHOR
624  
625 Paul Evans <leonerd@leonerd.org.uk>
626  
627 =cut
628  
629 14 0x55AA;
« Tangence/Stream.pm ↑ Summary Tangence/Server.pm »