blib/lib/ExtUtils/XSpp/Node/Class.pm

filter: Statementstmt 90.5% 86 / 95 Branchbran 63.3% 19 / 30 Conditioncond 70.0% 7 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.9% 20 / 22 Podpod 55.5% 5 / 9 totaltotal 80.8% 139 / 172 CC 24 SCAR 15.9
CC
24
Cov
83%
print
10.4
add_methods
7.1
init
6.2
CRAP 4.9
« blib/lib/ExtUtils/XSpp/Lexer.pm ↑ Summary blib/lib/ExtUtils/XSpp/Typemap.pm »
1 package ExtUtils::XSpp::Node::Class;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use base 'ExtUtils::XSpp::Node::Package';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 ExtUtils::XSpp::Node::Class - A class (inherits from Package).
9  
10 =head1 DESCRIPTION
11  
12 An L<ExtUtils::XSpp::Node::Package> sub-class representing a class
13 declaration such as
14  
15 %name{PerlClassName} class MyClass : public BaseClass
16 {
17 ...
18 }
19  
20 The Perl-level class name and the C++ class name attributes
21 are inherited from the L<ExtUtils::XSpp::Node::Package> class.
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 Creates a new C<ExtUtils::XSpp::Node::Class> object.
28  
29 Optional named parameters:
30 C<methods> can be a reference to an array of methods
31 (L<ExtUtils::XSpp::Node::Method>) of the class,
32 and C<base_classes>, a reference to an array of
33 base classes (C<ExtUtils::XSpp::Node::Class> objects).
34 C<catch> may be a list of exception names that all
35 methods in the class handle.
36  
37 =cut
38  
39 # internal list of all the non-empty class objects, either defined by the
40 # parser or created by plugins; does not include dummy base class objects
41 21 my %all_classes;
42  
43 54 ▶sub init {
Subroutine init: called CC=6 SCAR=18.2
Pod documented
44 54 my $this = shift;
45 54 my %args = @_;
46  
47 54 $this->SUPER::init( @_ );
48 54 $this->{METHODS} = [];
49 54 ▶ $this->{BASE_CLASSES} = $args{base_classes} || [];
Condition100%2/2
$args{'base_classes'} || []
l!l
549
MC/DC100%1/1
$args{'base_classes'} || []
$args{'base_classes'}
Truth table100%2/2
A
A= $args{'base_classes'}
Aresult
11
00
50 0 ▶ $this->add_methods( @{$args{methods}} ) if $args{methods};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"methods"} 0 54 54
51 54 $this->{CATCH} = $args{catch};
52 54 $this->{CONDITION} = $args{condition};
53 54 $this->{EMIT_CONDITION} = $args{emit_condition};
54 54 $this->{GETTER_STYLE} = $this->{SETTER_STYLE} = 'underscore';
55  
56 54 ▶ $all_classes{$this->cpp_name} = $this unless $this->empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $this->empty 0 54 54
57  
58 # TODO check the Perl name of the base class?
59 54 foreach my $base ( @{$this->base_classes} ) {
60 $base = $all_classes{$base->cpp_name}
61 3 ▶ if $all_classes{$base->cpp_name};
Branch: 1 branch
BranchTrueFalseTotal
if $all_classes{$base->cpp_name} 0 3 3
62 }
63 }
64  
65 =head2 add_methods
66  
67 Adds new methods to the class. By default, their
68 scope is C<public>. Takes arbitrary number of arguments
69 which are processed in order.
70  
71 If an argument is an L<ExtUtils::XSpp::Node::Access>,
72 the current method scope is changed accordingly for
73 all following methods.
74  
75 If an argument is an L<ExtUtils::XSpp::Node::Method>
76 it is added to the list of methods of the class.
77 The method's class name is set to the current class
78 and its scope is set to the current method scope.
79  
80 =cut
81  
82 107 ▶sub add_methods {
Subroutine add_methods: called CC=7 SCAR=19.7
Pod documented
83 107 my $this = shift;
84 107 my $access = 'public'; # good enough for now
85 107 foreach my $meth ( @_ ) {
86 71 ▶ if( $meth->isa( 'ExtUtils::XSpp::Node::Function' ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($meth->isa('ExtUtils::XSpp::Node::Function')) { } 51 20 71
elsif ($meth->isa('ExtUtils::XSpp::Node::Member')) { } 12 8 20
elsif ($meth->isa('ExtUtils::XSpp::Node::Access')) { } 0 8 8
87 51 $meth->{CLASS} = $this;
88 51 $meth->{ACCESS} = $access;
89 51 ▶ $meth->add_exception_handlers( @{$this->{CATCH} || []} );
Condition50%1/2
$this->{'CATCH'} || []
l!l
510
MC/DC0%0/1
$this->{'CATCH'} || []
$this->{'CATCH'}
Truth table50%1/2
A
A= $this->{'CATCH'}
Aresult
11
00
90 51 $meth->resolve_typemaps;
91 51 $meth->resolve_exceptions;
92 } elsif( $meth->isa( 'ExtUtils::XSpp::Node::Member' ) ) {
93 12 $meth->{CLASS} = $this;
94 12 $meth->{ACCESS} = $access;
95 12 $meth->resolve_typemaps;
96 } elsif( $meth->isa( 'ExtUtils::XSpp::Node::Access' ) ) {
97 0 $access = $meth->access;
98 0 next;
99 }
100 # FIXME: Should there be else{croak}?
101 71 push @{$this->{METHODS}}, $meth;
102 }
103  
104 107 ▶ $all_classes{$this->cpp_name} = $this unless $this->empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $this->empty 103 4 107
105 }
106  
107 0 ▶sub delete_methods {
Subroutine delete_methods: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my( $this, @methods ) = @_;
109 0 my %methods = map { $_ => 1 } @methods;
110  
111 0 $this->{METHODS} = [ grep !$methods{$_}, @{$this->{METHODS}} ];
112 }
113  
114 50 ▶sub print {
Subroutine print: called CC=10 SCAR=23.4
Pod documented
115 50 my $this = shift;
116 50 my $state = shift;
117 50 my $out = $this->SUPER::print( $state );
118  
119 50 ▶ $out .= '#if ' . $this->emit_condition . "\n" if $this->emit_condition;
Branch: 1 branch
BranchTrueFalseTotal
if $this->emit_condition 0 50 50
120  
121 50 foreach my $m ( @{$this->methods} ) {
122 67 ▶ next if $m->can( 'access' ) && $m->access ne 'public';
Branch: 1 branch
BranchTrueFalseTotal
if $m->can("access") and $m->access ne "public" 0 67 67
Condition66%2/3
$m->can("access") and $m->access ne "public"
!ll&&!rl&&r
6610
MC/DC0%0/2
$m->can("access") and $m->access ne "public"
$m->can("access") $m->access ne "public"
Truth table66%2/3
A and B
A= $m->can("access")B= $m->access ne "public"
ABresult
0X0
100
111
123 67 $out .= $m->print( $state );
124 }
125  
126 # add a BOOT block for base classes
127 50 ▶ if( @{$this->base_classes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$this->base_classes;}) 2 48 50
128 2 my $class = $this->perl_name;
129  
130 2 $out .= <<EOT;
131 BOOT:
132 {
133 EOT
134  
135 2 ▶ $out .= '#ifdef ' . $this->condition . "\n" if $this->condition;
Branch: 1 branch
BranchTrueFalseTotal
if $this->condition 0 2 2
136 2 $out .= <<EOT;
137 AV* isa = get_av( "${class}::ISA", 1 );
138 EOT
139  
140 2 foreach my $b ( @{$this->base_classes} ) {
141 3 my $base = $b->perl_name;
142  
143 3 $out .= <<EOT;
144 av_store( isa, 0, newSVpv( "$base", 0 ) );
145 EOT
146 }
147  
148 # close block in BOOT
149 2 ▶ $out .= '#endif // ' . $this->condition . "\n" if $this->condition;
Branch: 1 branch
BranchTrueFalseTotal
if $this->condition 0 2 2
150 2 $out .= <<EOT;
151 } // blank line here is important
152  
153 EOT
154 }
155  
156 50 ▶ $out .= '#endif // ' . $this->emit_condition . "\n" if $this->emit_condition;
Branch: 1 branch
BranchTrueFalseTotal
if $this->emit_condition 0 50 50
157  
158 50 return $out;
159 }
160  
161 my %getter_maker =
162 3 ▶ ( no_prefix => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
163 3 ▶ underscore => sub { 'get_' . $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
164 1 ▶ camelcase => sub { 'get' . ucfirst $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
165 1 ▶ uppercase => sub { 'Get' . ucfirst $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
166 21 );
167  
168 my %setter_maker =
169 0 ▶ ( no_prefix => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 4 ▶ underscore => sub { 'set_' . $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
171 2 ▶ camelcase => sub { 'set' . ucfirst $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
172 2 ▶ uppercase => sub { 'Set' . ucfirst $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
173 21 );
174  
175 8 ▶sub _getter_name {
Subroutine _getter_name: called CC=1 SCAR=0.0
176 8 my( $this, $base ) = @_;
177  
178 8 return $getter_maker{$this->{GETTER_STYLE}}->( $base );
179 }
180  
181 8 ▶sub _setter_name {
Subroutine _setter_name: called CC=1 SCAR=0.0
182 8 my( $this, $base ) = @_;
183  
184 8 return $setter_maker{$this->{SETTER_STYLE}}->( $base );
185 }
186  
187 6 ▶sub set_getter_style {
Subroutine set_getter_style: called CC=2 SCAR=7.1
Pod undocumented
188 6 my( $this, $style ) = @_;
189  
190 6 ▶ die "Invalid accessor style '$style'" unless exists $getter_maker{$style};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $getter_maker{$style} 0 6 6
191 6 $this->{GETTER_STYLE} = $style;
192 }
193  
194 6 ▶sub set_setter_style {
Subroutine set_setter_style: called CC=2 SCAR=7.1
Pod undocumented
195 6 my( $this, $style ) = @_;
196  
197 6 ▶ die "Invalid accessor style '$style'" unless exists $setter_maker{$style};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $setter_maker{$style} 0 6 6
198 6 $this->{SETTER_STYLE} = $style;
199 }
200  
201 =head1 ACCESSORS
202  
203 =head2 methods
204  
205 Returns the internal reference to the array of methods in this class.
206 Each of the methods is an C<ExtUtils::XSpp::Node::Method>
207  
208 =head2 base_classes
209  
210 Returns the internal reference to the array of base classes of
211 this class.
212  
213 If the base classes have been defined in the same file, these are the
214 complete class objects including method definitions, otherwise only
215 the C++ and Perl name of the class are available as attributes.
216  
217 =cut
218  
219 373 ▶sub methods { $_[0]->{METHODS} }
Subroutine methods: called CC=1 SCAR=0.0
Pod documented
220 106 ▶sub base_classes { $_[0]->{BASE_CLASSES} }
Subroutine base_classes: called CC=1 SCAR=0.0
Pod documented
221 161 ▶sub empty { !$_[0]->methods || !@{$_[0]->methods} }
Subroutine empty: called CC=2 SCAR=7.1
Pod undocumented
Condition66%2/3
$_[0]->methods && @{$_[0]->methods;}
!ll&&!rl&&r
058103
MC/DC50%1/2
$_[0]->methods && @{$_[0]->methods;}
$_[0]->methods @{$_[0]->methods;}
Truth table66%2/3
A && B
A= $_[0]->methodsB= @{$_[0]->methods;}
ABresult
0X0
100
111
222  
223 21 1;
« blib/lib/ExtUtils/XSpp/Lexer.pm ↑ Summary blib/lib/ExtUtils/XSpp/Typemap.pm »