| 1 | package MRP::Interface; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use MRP::Introspection; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use vars qw($AUTOLOAD %interfaces %implementers $VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | $VERSION = 1.0; | |||||||||
| 11 | |||||||||||
| 12 | 0 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: not called CC=2 SCAR=17.9
| |||||||||||
| 13 | 0 | my ($name) = $AUTOLOAD =~ /([^:]+)$/; | |||||||||
| 14 | 0 | my $int = $interfaces{$name}; | |||||||||
| 15 | 0 | ▶ | $int or confess "Use of undefined interface: $name"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 0 | return $int; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 0 | ▶ | sub create { | ||||||||
|
Subroutine
create: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 20 | 0 | my $thingy = shift; | |||||||||
| 21 | 0 | my %interface = @_; | |||||||||
| 22 | |||||||||||
| 23 | 0 | my ($name, $definition); | |||||||||
| 24 | 0 | ▶ | while (($name, $definition) = each %interface) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 0 | ▶ | if(exists $interfaces{$name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | confess "interface $name has already been defined\n"; | |||||||||
| 27 | } else { | ||||||||||
| 28 | 0 | my $description = $definition->{''}; delete $definition->{''}; | |||||||||
| 29 | 0 | $interfaces{$name} = bless {name=>$name, | |||||||||
| 30 | definition=>$definition, | ||||||||||
| 31 | implementors=>{}, | ||||||||||
| 32 | description=>$description, | ||||||||||
| 33 | }, $thingy; | ||||||||||
| 34 | } | ||||||||||
| 35 | } | ||||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | sub implementedBy { | ||||||||
|
Subroutine
implementedBy: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 39 | 0 | my ($interface,$thingy) = (shift,shift); | |||||||||
| 40 | 0 | ▶ | if(my $package = ref($thingy)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | foreach (keys %{$interface->{implementors}}) { | |||||||||
| 42 | 0 | ▶ | return 1 if $thingy->isa($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | 0 | return; | |||||||||
| 45 | } else { | ||||||||||
| 46 | 0 | ▶ | return if(not $thingy); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | ▶ | my @errors = map { $thingy->can($_) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | ? () : "$_"; | ||||||||||
| 49 | 0 | } keys %{$interface->{definition}}; | |||||||||
| 50 | 0 | ▶ | if(@errors) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | confess | ||||||||||
| 52 | "$thingy does not implement interface ", | ||||||||||
| 53 | $interface->{name}, | ||||||||||
| 54 | ". The following ", scalar(@errors), " functions must be defined:\n ", | ||||||||||
| 55 | 0 | join("", map { $_."\n\t".$interface->{definition}->{$_}."\n" } @errors), | |||||||||
| 56 | "\n"; | ||||||||||
| 57 | } | ||||||||||
| 58 | 0 | $interface->{implementors}->{$thingy} = 1; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub name { | ||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 63 | 0 | my $interface = shift; | |||||||||
| 64 | 0 | return $interface->{name}; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub functions { | ||||||||
|
Subroutine
functions: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | 0 | my $interface = shift; | |||||||||
| 69 | 0 | return keys %{$interface->{definition}}; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub DESTROY {} | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 73 | |||||||||||
| 74 | 1 | $VERSION; | |||||||||
| 75 | |||||||||||
| 76 | __END__ | ||||||||||