line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: perl; coding: utf-8 -*- |
2
|
3
|
|
|
3
|
|
2816
|
package YATT::ArgTypes; use YATT::Inc; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
20
|
|
3
|
3
|
|
|
3
|
|
13
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
104
|
|
4
|
3
|
|
|
3
|
|
15
|
use warnings qw(FATAL all NONFATAL misc); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
139
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
16
|
use base qw(YATT::Class::Configurable); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
697
|
|
7
|
3
|
|
|
|
|
20
|
use YATT::Fields qw(cf_callpack |
8
|
|
|
|
|
|
|
cf_base cf_type_map cf_type_fmt |
9
|
|
|
|
|
|
|
cf_type_name |
10
|
|
|
|
|
|
|
cf_debug |
11
|
3
|
|
|
3
|
|
521
|
); |
|
3
|
|
|
|
|
5
|
|
12
|
3
|
|
|
3
|
|
15
|
use YATT::Util; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
457
|
|
13
|
3
|
|
|
3
|
|
18
|
use YATT::Util::Symbol; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
274
|
|
14
|
3
|
|
|
3
|
|
17
|
use Carp; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
2964
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub import { |
17
|
3
|
|
|
3
|
|
9
|
my $pack = shift; |
18
|
3
|
|
|
|
|
12
|
my ($callpack) = caller; |
19
|
3
|
|
|
|
|
7
|
my @types; |
20
|
3
|
|
|
|
|
15
|
my $opts = $pack->new(callpack => $callpack |
21
|
|
|
|
|
|
|
, $pack->parse_args(\@_, \@types)); |
22
|
3
|
|
|
|
|
20
|
$opts->add_type($_) for @types; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub parse_args { |
26
|
3
|
|
|
3
|
0
|
10
|
my ($pack, $arglist, $taskqueue) = @_; |
27
|
3
|
|
|
|
|
5
|
my @confs; |
28
|
3
|
|
|
|
|
13
|
while (@$arglist) { |
29
|
33
|
100
|
|
|
|
125
|
if (ref $arglist->[0]) { |
|
|
50
|
|
|
|
|
|
30
|
21
|
|
|
|
|
55
|
push @$taskqueue, shift @$arglist; |
31
|
|
|
|
|
|
|
} elsif (my ($flag, $key) = $arglist->[0] =~ /^([\-:])(\w+)/) { |
32
|
12
|
|
|
|
|
19
|
shift @$arglist; |
33
|
12
|
100
|
|
|
|
34
|
my $value = $flag eq ':' ? 1 : shift @$arglist; |
34
|
12
|
|
|
|
|
50
|
push @confs, $key, $value; |
35
|
|
|
|
|
|
|
} else { |
36
|
0
|
|
|
|
|
0
|
croak "Invalid option '$arglist->[0]'"; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
3
|
|
|
|
|
40
|
@confs; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub add_type { |
43
|
21
|
|
|
21
|
0
|
38
|
(my MY $self, my ($desc)) = @_; |
44
|
21
|
|
|
|
|
34
|
my $type = shift @$desc; |
45
|
21
|
|
|
|
|
85
|
my $fullclass = sprintf $self->{cf_type_fmt}, $type; |
46
|
|
|
|
|
|
|
|
47
|
21
|
|
|
|
|
53
|
$self->{cf_type_map}{$type} = $fullclass; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
define_const(globref($fullclass, "type_name"), $type) |
50
|
21
|
50
|
|
|
|
97
|
if $self->{cf_type_name}; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# t_zzz typealias. |
53
|
21
|
|
|
|
|
92
|
define_const(globref($self->{cf_callpack}, "t_$type"), $fullclass); |
54
|
|
|
|
|
|
|
|
55
|
21
|
|
|
|
|
68
|
my $fields = fields_hash($self); |
56
|
|
|
|
|
|
|
|
57
|
21
|
|
|
|
|
43
|
my (@symbols, @tasks, %config); |
58
|
21
|
|
|
|
|
53
|
while (@$desc) { |
59
|
27
|
100
|
|
|
|
164
|
if (ref $desc->[0] eq 'SCALAR') { |
|
|
50
|
|
|
|
|
|
60
|
9
|
|
|
|
|
23
|
my ($nameref, $value) = splice @$desc, 0, 2; |
61
|
9
|
|
|
|
|
14
|
my $code = do { |
62
|
9
|
50
|
|
|
|
24
|
unless (ref $value) { |
|
|
0
|
|
|
|
|
|
63
|
9
|
|
|
0
|
|
55
|
sub () { $value }; |
|
0
|
|
|
|
|
0
|
|
64
|
|
|
|
|
|
|
} elsif (ref $value eq 'CODE') { |
65
|
0
|
|
|
|
|
0
|
$value; |
66
|
|
|
|
|
|
|
} else { |
67
|
0
|
|
|
|
|
0
|
die "Unknown ArgType desc for $$nameref : '$value'"; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
}; |
70
|
9
|
|
|
|
|
38
|
push @symbols, [$$nameref, $code]; |
71
|
|
|
|
|
|
|
# *{globref($fullclass, $$nameref)} = $code; |
72
|
|
|
|
|
|
|
} elsif (my ($flag, $key) = $desc->[0] =~ /^([\-:])(\w+)/) { |
73
|
18
|
|
|
|
|
25
|
shift @$desc; |
74
|
18
|
50
|
|
|
|
46
|
my $value = $flag eq ':' ? 1 : shift @$desc; |
75
|
18
|
100
|
|
|
|
47
|
if ($fields->{"cf_$key"}) { |
76
|
3
|
|
|
|
|
16
|
$config{"cf_$key"} = $value; |
77
|
|
|
|
|
|
|
} else { |
78
|
15
|
50
|
|
|
|
85
|
my $sub = $self->can("option_$key") |
79
|
|
|
|
|
|
|
or die "Unknown ArgType option $key"; |
80
|
15
|
|
|
|
|
67
|
push @tasks, [$sub, $value]; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
# $sub->($self, $fullclass, $value); |
83
|
|
|
|
|
|
|
} else { |
84
|
0
|
|
|
|
|
0
|
die "Unknown desc type $desc" |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# base だけは eval を使う。 さもないと %FIELDS が作られない。 |
89
|
|
|
|
|
|
|
# *{globref($fullclass, 'ISA')} = [$self->{cf_base}]; |
90
|
|
|
|
|
|
|
$self->checked_eval |
91
|
|
|
|
|
|
|
(sprintf qq{package %s; use base qw(%s)} |
92
|
|
|
|
|
|
|
, $fullclass |
93
|
|
|
|
|
|
|
, $self->lookup_in($self->{cf_type_map}, $config{cf_base}) |
94
|
21
|
|
66
|
|
|
82
|
|| $$self{cf_base}); |
95
|
|
|
|
|
|
|
|
96
|
21
|
|
|
|
|
54
|
foreach my $rec (@symbols) { |
97
|
9
|
|
|
|
|
24
|
my ($sym, $code) = @$rec; |
98
|
9
|
|
|
|
|
17
|
*{globref($fullclass, $sym)} = $code; |
|
9
|
|
|
|
|
26
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
21
|
|
|
|
|
63
|
foreach my $rec (@tasks) { |
102
|
15
|
|
|
|
|
35
|
my ($sub, $value) = @$rec; |
103
|
15
|
|
|
|
|
42
|
$sub->($self, $fullclass, $value, \%config); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub lookup_in { |
108
|
21
|
|
|
21
|
0
|
49
|
my ($self, $hash, $key) = @_; |
109
|
21
|
100
|
|
|
|
149
|
return unless defined $key; |
110
|
3
|
|
|
|
|
22
|
$hash->{$key}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub option_alias { |
114
|
9
|
|
|
9
|
0
|
21
|
(my MY $self, my ($class, $value)) = @_; |
115
|
9
|
100
|
|
|
|
31
|
foreach my $alias (ref $value ? @$value : $value) { |
116
|
12
|
|
|
|
|
69
|
$self->{cf_type_map}{$alias} = $class; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub option_fields { |
121
|
6
|
|
|
6
|
0
|
13
|
(my MY $self, my ($class, $value)) = @_; |
122
|
6
|
|
|
|
|
24
|
my $fields = terse_dump(@$value); |
123
|
6
|
|
|
|
|
302
|
$self->checked_eval(<
|
124
|
|
|
|
|
|
|
package $class; use YATT::Fields $fields; |
125
|
|
|
|
|
|
|
END |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; |