| 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-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::Class 0.33; | ||||||||||||||||||||||||||||
| 11 | 14 | ▶ | class Tangence::Class :isa(Tangence::Meta::Class); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use Tangence::Constants; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use Tangence::Property; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 14 | ▶ | use Tangence::Meta::Method; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use Tangence::Meta::Event; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 14 | ▶ | use Tangence::Meta::Argument; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 14 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 14 | ▶ | use meta 0.007; # set_subname | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 14 | ▶ | no warnings 'meta::experimental'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 NAME | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | C<Tangence::Class> - server implementation of a C<Tangence> class | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This module is a component of L<Tangence::Server>. It is not intended for | ||||||||||||||||||||||||||||
| 33 | end-user use directly. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 14 | our %CLASSES; # cache one per class, keyed by _Tangence_ class name | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 38 | ▶ | sub make ( $class, %args ) | ||||||||||||||||||||||||||
|
Subroutine
make: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | 38 | my $name = $args{name}; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 38 | ▶ | return $CLASSES{$name} //= $class->new( %args ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$Tangence::Class::CLASSES{$name} //= $class->new(%args)
MC/DC0%0/2
$Tangence::Class::CLASSES{$name} //= $class->new(%args)
$Tangence::Class::CLASSES{$name}
$class->new(%args)
Truth table33%1/3
A //= B
A= $Tangence::Class::CLASSES{$name}B= $class->new(%args)
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 60 | ▶ | sub _new_type ( $sig ) | ||||||||||||||||||||||||||
|
Subroutine
_new_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 60 | return Tangence::Type->make_from_sig( $sig ); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 26 | ▶ | sub declare ( $class, $perlname, %args ) | ||||||||||||||||||||||||||
|
Subroutine
declare: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 26 | ( my $name = $perlname ) =~ s{::}{.}g; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 26 | ▶ | if( exists $CLASSES{$name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | croak "Cannot re-declare $name"; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 26 | my $self = $class->make( name => $name ); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 26 | my %methods; | |||||||||||||||||||||||||||
| 62 | 26 | foreach ( keys %{ $args{methods} } ) { | |||||||||||||||||||||||||||
| 63 | 12 | my %params = %{ $args{methods}{$_} }; | |||||||||||||||||||||||||||
| 64 | $methods{$_} = Tangence::Meta::Method->new( | ||||||||||||||||||||||||||||
| 65 | class => $self, | ||||||||||||||||||||||||||||
| 66 | name => $_, | ||||||||||||||||||||||||||||
| 67 | arguments => [ map { | ||||||||||||||||||||||||||||
| 68 | 12 | Tangence::Meta::Argument->new( name => $_->[0], type => _new_type( $_->[1] ) ) | |||||||||||||||||||||||||||
| 69 | 12 | } @{ delete $params{args} } ], | |||||||||||||||||||||||||||
| 70 | 12 | ret => _new_type( delete $params{ret} ), | |||||||||||||||||||||||||||
| 71 | %params, | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 26 | my %events; | |||||||||||||||||||||||||||
| 76 | 26 | foreach ( keys %{ $args{events} } ) { | |||||||||||||||||||||||||||
| 77 | 38 | my %params = %{ $args{events}{$_} }; | |||||||||||||||||||||||||||
| 78 | $events{$_} = Tangence::Meta::Event->new( | ||||||||||||||||||||||||||||
| 79 | class => $self, | ||||||||||||||||||||||||||||
| 80 | name => $_, | ||||||||||||||||||||||||||||
| 81 | arguments => [ map { | ||||||||||||||||||||||||||||
| 82 | 24 | Tangence::Meta::Argument->new( name => $_->[0], type => _new_type( $_->[1] ) ) | |||||||||||||||||||||||||||
| 83 | 38 | } @{ delete $params{args} } ], | |||||||||||||||||||||||||||
| 84 | %params, | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 26 | my %properties; | |||||||||||||||||||||||||||
| 89 | 26 | foreach ( keys %{ $args{props} } ) { | |||||||||||||||||||||||||||
| 90 | 12 | my %params = %{ $args{props}{$_} }; | |||||||||||||||||||||||||||
| 91 | $properties{$_} = Tangence::Property->new( | ||||||||||||||||||||||||||||
| 92 | class => $self, | ||||||||||||||||||||||||||||
| 93 | name => $_, | ||||||||||||||||||||||||||||
| 94 | dimension => ( delete $params{dim} ) || DIM_SCALAR, | ||||||||||||||||||||||||||||
| 95 | 12 | ▶ | type => _new_type( delete $params{type} ), | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{'dim'} || 1
MC/DC0%0/1
delete $params{'dim'} || 1
delete $params{'dim'}
Truth table50%1/2
A
A= delete $params{'dim'}
| |||||||||||||||||||||||||||||
| 96 | %params, | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 26 | my @superclasses; | |||||||||||||||||||||||||||
| 101 | 26 | foreach ( @{ $args{superclasses} } ) { | |||||||||||||||||||||||||||
| 102 | 0 | push @superclasses, Tangence::Class->for_perlname( $_ ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | $self->define( | ||||||||||||||||||||||||||||
| 106 | 26 | methods => \%methods, | |||||||||||||||||||||||||||
| 107 | events => \%events, | ||||||||||||||||||||||||||||
| 108 | properties => \%properties, | ||||||||||||||||||||||||||||
| 109 | superclasses => \@superclasses, | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | method define | ||||||||||||||||||||||||||||
| 114 | 38 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
define: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 38 | $self->SUPER::define( @_ ); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 38 | my $class = $self->perlname; | |||||||||||||||||||||||||||
| 118 | 38 | my $classmeta = meta::package->get( $class ); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 38 | my %subs; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 38 | foreach my $prop ( values %{ $self->direct_properties } ) { | |||||||||||||||||||||||||||
| 123 | 94 | $prop->build_accessor( \%subs ); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 38 | foreach my $name ( keys %subs ) { | |||||||||||||||||||||||||||
| 127 | 466 | ▶ | next if $classmeta->try_get_symbol( '&' . $name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | $classmeta->add_symbol( | ||||||||||||||||||||||||||||
| 129 | 466 | '&' . $name => $subs{$name} | |||||||||||||||||||||||||||
| 130 | )->set_subname( "${class}::${name}" ); | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 14 | } | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 19 | ▶ | sub for_name ( $class, $name ) | ||||||||||||||||||||||||||
|
Subroutine
for_name: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||
| 136 | 19 | ▶ | return $CLASSES{$name} // croak "Unknown Tangence::Class for '$name'"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$Tangence::Class::CLASSES{$name} // croak("Unknown Tangence::Class for '${name}'")
MC/DC0%0/1
$Tangence::Class::CLASSES{$name} // croak("Unknown Tangence::Class for '${name}'")
$Tangence::Class::CLASSES{$name}
Truth table50%1/2
A
A= $Tangence::Class::CLASSES{$name}
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 335 | ▶ | sub for_perlname ( $class, $perlname ) | ||||||||||||||||||||||||||
|
Subroutine
for_perlname: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 335 | ( my $name = $perlname ) =~ s{::}{.}g; | |||||||||||||||||||||||||||
| 142 | 335 | ▶ | return $CLASSES{$name} // croak "Unknown Tangence::Class for '$perlname'"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$Tangence::Class::CLASSES{$name} // croak("Unknown Tangence::Class for '${perlname}'")
MC/DC0%0/1
$Tangence::Class::CLASSES{$name} // croak("Unknown Tangence::Class for '${perlname}'")
$Tangence::Class::CLASSES{$name}
Truth table50%1/2
A
A= $Tangence::Class::CLASSES{$name}
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub superclasses | ||||||||||||||||||||||||||||
| 146 | 455 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
superclasses: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 455 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 455 | my @supers = $self->SUPER::superclasses; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 455 | ▶ | if( !@supers and $self->perlname ne "Tangence::Object" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @supers and $self->perlname ne "Tangence::Object"
MC/DC100%2/2
not @supers and $self->perlname ne "Tangence::Object"
not @supers
$self->perlname ne "Tangence::Object"
Truth table100%3/3
not(A) and B
A= not @supersB= $self->perlname ne "Tangence::Object"
| |||||||||||||||||||||||||||||
| 152 | 296 | @supers = Tangence::Class->for_perlname( "Tangence::Object" ); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 455 | return @supers; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | ▶ | method method ( $name ) | ||||||||||||||||||||||||||
|
Subroutine
method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||
| 160 | 6 | return $self->methods->{$name}; | |||||||||||||||||||||||||||
| 161 | 14 | } | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 49 | ▶ | method event ( $name ) | ||||||||||||||||||||||||||
|
Subroutine
event: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | { | ||||||||||||||||||||||||||||
| 165 | 49 | return $self->events->{$name}; | |||||||||||||||||||||||||||
| 166 | 14 | } | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 194 | ▶ | method property ( $name ) | ||||||||||||||||||||||||||
|
Subroutine
property: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||
| 170 | 194 | return $self->properties->{$name}; | |||||||||||||||||||||||||||
| 171 | 14 | } | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 14 | field $smashkeys; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | method smashkeys | ||||||||||||||||||||||||||||
| 176 | 41 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
smashkeys: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 41 | ▶ | return $smashkeys //= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$smashkeys //= do {
my %smash;
$smash{$_->name} = 1 foreach (grep {$_->smashed;} values %{$self->properties;});
$Tangence::Message::SORT_HASH_KEYS ? [sort(keys %smash)] : [keys %smash]
}
MC/DC0%0/2
$smashkeys //= do {
my %smash;
$smash{$_->name} = 1 foreach (grep {$_->smashed;} values %{$self->properties;});
$Tangence::Message::SORT_HASH_KEYS ? [sort(keys %smash)] : [keys %smash]
}
$smashkeys
do {
my %smash;
$smash{$_->name} = 1 foreach (grep {$_->smashed;} values %{$self->properties;});
$Tangence::Message::SORT_HASH_KEYS ? [sort(keys %smash)] : [keys %smash]
}
Truth table66%2/3
A //= B
A= $smashkeysB= do {
my %smash;
$smash{$_->name} = 1 foreach (grep {$_->smashed;} values %{$self->properties;});
$Tangence::Message::SORT_HASH_KEYS ? [sort(keys %smash)] : [keys %smash]
}
| |||||||||||||||||||||||||||||
| 178 | 19 | my %smash; | |||||||||||||||||||||||||||
| 179 | 19 | $smash{$_->name} = 1 for grep { $_->smashed } values %{ $self->properties }; | |||||||||||||||||||||||||||
| 180 | 19 | ▶ | $Tangence::Message::SORT_HASH_KEYS ? [ sort keys %smash ] : [ keys %smash ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||
| 182 | 14 | } | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 14 | 0x55AA; | |||||||||||||||||||||||||||