| 1 | # -*- mode: perl; coding: utf-8 -*- | ||||||||||||||||||||||||||||
| 2 | package YATT::Types; | ||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use YATT::Util::Symbol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use YATT::Util qw(terse_dump); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | require YATT::Inc; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub Base () { 'YATT::Class::Configurable' } | ||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use base Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use YATT::Fields qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | classes | ||||||||||||||||||||||||||||
| 14 | aliases | ||||||||||||||||||||||||||||
| 15 | default_methods | ||||||||||||||||||||||||||||
| 16 | cf_rules | ||||||||||||||||||||||||||||
| 17 | ) | ||||||||||||||||||||||||||||
| 18 | , [cf_base => Base] | ||||||||||||||||||||||||||||
| 19 | , qw(cf_callpack | ||||||||||||||||||||||||||||
| 20 | cf_export_alias | ||||||||||||||||||||||||||||
| 21 | cf_type_name | ||||||||||||||||||||||||||||
| 22 | cf_debug | ||||||||||||||||||||||||||||
| 23 | 17 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #======================================== | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 38 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 38 | my $pack = shift; | |||||||||||||||||||||||||||
| 29 | 38 | my ($callpack) = caller; | |||||||||||||||||||||||||||
| 30 | 38 | my %rules = (struct => [], inheritance => []); | |||||||||||||||||||||||||||
| 31 | 38 | $pack->parse_args(\@_, \my @conf, \%rules, 'struct'); | |||||||||||||||||||||||||||
| 32 | # use Data::Dumper; print Dumper(\%rules), "\n"; | ||||||||||||||||||||||||||||
| 33 | 38 | $pack->new(callpack => $callpack, @conf, rules => \%rules) | |||||||||||||||||||||||||||
| 34 | ->export; | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # XXX: 交互でも行けるようになったはず。テストを。 | ||||||||||||||||||||||||||||
| 38 | # XXX: -constant も欲しい ← @EXPORT に入れない。 | ||||||||||||||||||||||||||||
| 39 | # XXX: \inheritance も。 | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 38 | ▶ | sub parse_args { | ||||||||||||||||||||||||||
|
Subroutine
parse_args: called CC=8 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 38 | my ($pack, $arglist, $conflist, $taskqueue, $default_task) = @_; | |||||||||||||||||||||||||||
| 43 | 38 | ▶ | while (@$arglist) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 116 | ▶ | if (ref $arglist->[0]) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 55 | my ($task_name, $task_arg) = do { | |||||||||||||||||||||||||||
| 46 | 55 | ▶ | if (ref $arglist->[0] eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 55 | ($default_task, shift @$arglist); | |||||||||||||||||||||||||||
| 48 | } elsif (ref $arglist->[0] eq 'SCALAR') { | ||||||||||||||||||||||||||||
| 49 | 0 | (${shift @$arglist}, shift @$arglist); | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 0 | croak "Invalid option '$arglist->[0]'"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | 55 | ▶ | unless (defined $taskqueue->{$task_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | croak "Invalid task: $task_name"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 55 | push @{$taskqueue->{$task_name}}, $task_arg; | |||||||||||||||||||||||||||
| 58 | } elsif (my ($flag, $key) = $arglist->[0] =~ /^([\-:])(\w+)/) { | ||||||||||||||||||||||||||||
| 59 | 61 | shift @$arglist; | |||||||||||||||||||||||||||
| 60 | 61 | ▶ | my $value = $flag eq ':' ? 1 : shift @$arglist; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 61 | push @$conflist, $key, $value; | |||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | 0 | croak "Invalid option '$arglist->[0]'"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 38 | ▶ | sub export { | ||||||||||||||||||||||||||
|
Subroutine
export: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 38 | my MY $opts = shift; | |||||||||||||||||||||||||||
| 70 | 38 | my $script = $opts->make; | |||||||||||||||||||||||||||
| 71 | 38 | ▶ | print STDERR $script if $opts->{cf_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 12 | ▶ | eval $script; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 38 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | #---------------------------------------- | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 42 | ▶ | sub configure_base { | ||||||||||||||||||||||||||
|
Subroutine
configure_base: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 42 | (my MY $opts, my ($value)) = @_; | |||||||||||||||||||||||||||
| 80 | 22 | ▶ | if (ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4 | push @{$$opts{aliases}}, $value; | |||||||||||||||||||||||||||
| 82 | 4 | $opts->{cf_base} = $value->[1]; | |||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||
| 84 | 18 | $opts->{cf_base} = $value; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 22 | $opts; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 14 | ▶ | sub configure_alias { | ||||||||||||||||||||||||||
|
Subroutine
configure_alias: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 14 | (my MY $opts, my ($value)) = @_; | |||||||||||||||||||||||||||
| 91 | 14 | push @{$opts->{aliases}}, chunklist($value); | |||||||||||||||||||||||||||
| 92 | 14 | $opts; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | ▶ | sub configure_default { | ||||||||||||||||||||||||||
|
Subroutine
configure_default: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 4 | (my MY $opts, my ($value)) = @_; | |||||||||||||||||||||||||||
| 97 | 4 | push @{$opts->{default_methods}}, chunklist($value); | |||||||||||||||||||||||||||
| 98 | 4 | $opts; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | #======================================== | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 38 | ▶ | sub make { | ||||||||||||||||||||||||||
|
Subroutine
make: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 38 | my MY $opts = shift; | |||||||||||||||||||||||||||
| 105 | 38 | my $script; | |||||||||||||||||||||||||||
| 106 | # 順番が有る。 | ||||||||||||||||||||||||||||
| 107 | 38 | foreach my $rule (qw(struct inheritance)) { | |||||||||||||||||||||||||||
| 108 | 76 | ▶ | next unless my $descs = $opts->{cf_rules}{$rule}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 76 | ▶ | next unless @$descs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 32 | $script .= $opts->can("make_$rule")->($opts, @$descs); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 38 | $script .= $opts->make_class_aliases; | |||||||||||||||||||||||||||
| 113 | 38 | $script .= $opts->make_default_methods; | |||||||||||||||||||||||||||
| 114 | 38 | $script; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 32 | ▶ | sub make_struct { | ||||||||||||||||||||||||||
|
Subroutine
make_struct: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 32 | my MY $opts = shift; | |||||||||||||||||||||||||||
| 119 | 32 | my @result; | |||||||||||||||||||||||||||
| 120 | 32 | foreach my $desc (@_) { | |||||||||||||||||||||||||||
| 121 | push @result, $opts->make_class_nesting | ||||||||||||||||||||||||||||
| 122 | ($desc, $$opts{cf_callpack} . '::' | ||||||||||||||||||||||||||||
| 123 | 55 | ▶ | , $$opts{cf_base} || $opts->Base); | ||||||||||||||||||||||||||
|
Condition33%1/3
$opts->{'cf_base'} || $opts->Base
MC/DC0%0/2
$opts->{'cf_base'} || $opts->Base
$opts->{'cf_base'}
$opts->Base
Truth table33%1/3
A || B
A= $opts->{'cf_base'}B= $opts->Base
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 32 | join "", @result; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 38 | ▶ | sub list_aliases { | ||||||||||||||||||||||||||
|
Subroutine
list_aliases: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 38 | my MY $opts = shift; | |||||||||||||||||||||||||||
| 130 | 38 | map {$$_[0]} @{$$opts{classes}}, @{$$opts{aliases}}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 38 | ▶ | sub make_class_aliases { | ||||||||||||||||||||||||||
|
Subroutine
make_class_aliases: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 38 | my MY $opts = shift; | |||||||||||||||||||||||||||
| 135 | 38 | my $aliases = join "\n ", $opts->list_aliases; | |||||||||||||||||||||||||||
| 136 | 38 | my $script = <<END; | |||||||||||||||||||||||||||
| 137 | package $$opts{cf_callpack}; | ||||||||||||||||||||||||||||
| 138 | push our \@EXPORT_OK, qw($aliases); | ||||||||||||||||||||||||||||
| 139 | END | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 38 | ▶ | $script .= <<END if $$opts{cf_export_alias}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | push our \@EXPORT, qw($aliases); | ||||||||||||||||||||||||||||
| 143 | END | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 38 | my $stash = *{globref($$opts{cf_callpack}, '')}{HASH}; | |||||||||||||||||||||||||||
| 146 | print STDERR "# [$$opts{cf_callpack} has] " | ||||||||||||||||||||||||||||
| 147 | , join(" ", sort keys %$stash), "\n" | ||||||||||||||||||||||||||||
| 148 | 38 | ▶ | if $opts->{cf_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 38 | foreach my $classdef (@{$$opts{classes}}, @{$$opts{aliases}}) { | |||||||||||||||||||||||||||
| 150 | # Ignore if alias is already defined. | ||||||||||||||||||||||||||||
| 151 | 92 | my $entry = $stash->{$classdef->[0]}; | |||||||||||||||||||||||||||
| 152 | 92 | ▶ | next if defined $entry and $entry->{CODE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $entry and $entry->{"CODE"}
MC/DC0%0/2
defined $entry and $entry->{"CODE"}
defined $entry
$entry->{"CODE"}
Truth table66%2/3
A and B
A= defined $entryB= $entry->{"CODE"}
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 92 | $script .= qq{sub $classdef->[0] () {'$classdef->[1]'}\n}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 38 | $script; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 60 | ▶ | sub make_class_nesting { | ||||||||||||||||||||||||||
|
Subroutine
make_class_nesting: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 161 | 60 | (my MY $opts, my ($desc, $prefix, $super)) = @_; | |||||||||||||||||||||||||||
| 162 | 60 | my ($class, $slots) = splice @$desc, 0, 2; | |||||||||||||||||||||||||||
| 163 | 60 | push @{$$opts{classes}}, [$class, $prefix.$class]; | |||||||||||||||||||||||||||
| 164 | my $script = $opts->make_class($prefix.$class, $super | ||||||||||||||||||||||||||||
| 165 | , terse_dump(@$slots | ||||||||||||||||||||||||||||
| 166 | 60 | ▶ | , map {ref $_ ? $$_[0] : $_} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | @$desc)); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 60 | ▶ | $script .= <<END if $opts->{cf_type_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | sub $prefix${class}::type_name () {'$class'} | ||||||||||||||||||||||||||||
| 171 | END | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 60 | foreach my $child (@$desc) { | |||||||||||||||||||||||||||
| 174 | 9 | ▶ | next unless ref $child; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 5 | $script .= $opts->make_class_nesting($child, $prefix, $super); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 60 | $script; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 60 | ▶ | sub make_class { | ||||||||||||||||||||||||||
|
Subroutine
make_class: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 60 | my ($self, $class, $super, $slots) = @_; | |||||||||||||||||||||||||||
| 182 | 60 | YATT::Inc->add_inc($class); | |||||||||||||||||||||||||||
| 183 | 60 | ▶ | <<END . ($super ? <<END : "") . ($slots ? <<END : "") . "\n"; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 184 | package $class; | ||||||||||||||||||||||||||||
| 185 | END | ||||||||||||||||||||||||||||
| 186 | use base qw($super); | ||||||||||||||||||||||||||||
| 187 | END | ||||||||||||||||||||||||||||
| 188 | use YATT::Fields $slots; | ||||||||||||||||||||||||||||
| 189 | END | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 38 | ▶ | sub make_default_methods { | ||||||||||||||||||||||||||
|
Subroutine
make_default_methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | 38 | my MY $opts = shift; | |||||||||||||||||||||||||||
| 194 | 38 | join "", map {<<END} @{$$opts{default_methods}}; | |||||||||||||||||||||||||||
| 195 | sub default_$$_[0] {'$$_[1]'} | ||||||||||||||||||||||||||||
| 196 | END | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | #---------------------------------------- | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 18 | ▶ | sub chunklist { | ||||||||||||||||||||||||||
|
Subroutine
chunklist: called CC=6 SCAR=23.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 203 | 18 | my ($arg) = @_; | |||||||||||||||||||||||||||
| 204 | 18 | my @list; | |||||||||||||||||||||||||||
| 205 | 18 | ▶ | if (ref $arg eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 206 | 18 | ▶ | push @list, [splice @$arg, 0, 2] while @$arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } elsif (ref $arg eq 'HASH') { | ||||||||||||||||||||||||||||
| 208 | 0 | ▶ | while (my ($k, $v) = each %$arg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | push @list, [$k, $v]; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } else { | ||||||||||||||||||||||||||||
| 212 | 0 | croak "Invalid arg for -alias"; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 18 | ▶ | wantarray ? @list : \@list; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 10 | 1; | |||||||||||||||||||||||||||