blib/lib/MarpaX/ESLIF/Base.pm

filter: Statementstmt 40.2% 35 / 87 Branchbran 17.8% 5 / 28 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 53.8% 7 / 13 Podpod 100% 2 / 2 totaltotal 34.5% 56 / 162 CC 25 SCAR 28.4
CC
25
Cov
35%
new
31.2
_destroy
20.0
_clone
12.0
CRAP 17.1
« blib/lib/MarpaX/ESLIF/Symbol.pm ↑ Summary blib/lib/MarpaX/ESLIF/JSON/Encoder.pm »
1 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 18 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package MarpaX::ESLIF::Base;
5 18 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Devel::GlobalDestruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use namespace::clean; # to avoid having an "in_global_destruction" method
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: ESLIF base
10  
11 18 our $AUTHORITY = 'cpan:JDDPAUSE'; # AUTHORITY
12  
13 18 our $VERSION = '6.0.35.1'; # VERSION
14  
15  
16 #
17 # We keep two internal hashes:
18 # - MULTITONS is holding multiton constants
19 # key is the engine (an IV) and value is the object instance
20 #
21 # - MULTITONS_FOR_GLOBAL_DESTRUCTION is used for global destruction
22 # key is the engine (an IV) and value is the object destructor code
23 #
24 # This is because at global destruction we cannot afford to use something
25 # that still constain object instances. At this stage, only pure perl constants
26 # do survive.
27 #
28 # DESTROY() is using:
29 # - MULTITONS when not in global destruction
30 # - MULTITONS_FOR_GLOBAL_DESTRUCTION when in global destruction
31 #
32 # CLONE() is completely reaffecting the keys of both hashes.
33 # -
34 #
35 18 my %MULTITONS;
36 my %MULTITONS_FOR_GLOBAL_DESTRUCTION;
37  
38 8 ▶sub _find {
Subroutine _find: called CC=3 SCAR=15.4
39 8 my ($class, $eq, @arguments) = @_;
40  
41 0 foreach (grep { $_->{class} eq $class } values %MULTITONS) {
42 0 ▶ return $_ if $_->$eq($_->{arguments}, @arguments)
Branch: 1 branch
BranchTrueFalseTotal
if $_->$eq($_->{"arguments"}, @arguments) 0 0 0
43 }
44  
45 return
46 8 }
47  
48  
49 8 ▶sub new {
Subroutine new: called CC=14 SCAR=34.4
Pod documented
50 8 my $proto = shift;
51  
52 8 ▶ my $class = ref($proto) || $proto; # Because of MarpaX::ESLIF::Recognizer::newFrom that is an instance method
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
53 #
54 # Some class variables MUST be provided. This will croak natively
55 # if that is not the case.
56 #
57 8 my $clonable = $proto->_CLONABLE();
58 8 my $eq = $proto->_EQ();
59 8 ▶ my $allocate = $proto->_ALLOCATE() // croak "$class allocate callback is not defined";
Condition50%1/2
$proto->_ALLOCATE // croak("$class allocate callback is not defined")
l!l
80
MC/DC0%0/1
$proto->_ALLOCATE // croak("$class allocate callback is not defined")
$proto->_ALLOCATE
Truth table50%1/2
A
A= $proto->_ALLOCATE
Aresult
11
00
60 8 ▶ my $dispose = $proto->_DISPOSE() // croak "$class dispose callback is not defined";
Condition50%1/2
$proto->_DISPOSE // croak("$class dispose callback is not defined")
l!l
80
MC/DC0%0/1
$proto->_DISPOSE // croak("$class dispose callback is not defined")
$proto->_DISPOSE
Truth table50%1/2
A
A= $proto->_DISPOSE
Aresult
11
00
61  
62 #
63 # eq, allocate and dispose must be CODE references when defined
64 #
65 8 ▶ croak "$class eq callback must be a CODE reference" if (defined($eq) && ((ref($eq) // '') ne 'CODE'));
Branch: 1 branch
BranchTrueFalseTotal
if defined $eq and (ref $eq // "") ne "CODE" 0 8 8
Condition40%2/5
defined $eq and (ref $eq // "") ne "CODE"
!ll&&!rl&&r
080
ref $eq // ""
l!l
80
MC/DC0%0/2
defined $eq and (ref $eq // "") ne "CODE"
defined $eq ref $eq
Truth table33%1/3
A and B
A= defined $eqB= ref $eq
ABresult
0X0
100
111
66 8 ▶ croak "$class allocate callback must be a CODE reference" if ((ref($allocate) // '') ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if (ref $allocate // "") ne "CODE" 0 8 8
Condition50%1/2
ref $allocate // ""
l!l
80
MC/DC0%0/1
ref $allocate // ""
ref $allocate
Truth table50%1/2
A
A= ref $allocate
Aresult
11
00
67 8 ▶ croak "$class dispose callback must be a CODE reference" if ((ref($dispose) // '') ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dispose // "") ne "CODE" 0 8 8
Condition50%1/2
ref $dispose // ""
l!l
80
MC/DC0%0/1
ref $dispose // ""
ref $dispose
Truth table50%1/2
A
A= ref $dispose
Aresult
11
00
68  
69 8 my $multiton = defined($eq);
70 8 ▶ my $self = $multiton ? $class->_find($eq, @_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$multiton ? : 8 0 8
71 8 ▶ return $self if defined($self);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self 0 8 8
72  
73 8 my $engine = $proto->$allocate(@_);
74 #
75 # For debugging
76 #
77 # printf STDERR "NEW %s engine %d\n", $class, $engine;
78 #
79 0 $self = bless { engine => $engine,
80 allocate => $allocate,
81 dispose => $dispose,
82 multiton => $multiton,
83 clonable => $clonable,
84 class => $class,
85 shallow => 0,
86 arguments => \@_}, $class;
87  
88 0 ▶ if ($multiton) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiton) 0 0 0
89 #
90 # Look to _global_destroy(), you will understand why MULTITONS_FOR_GLOBAL_DESTRUCTION must constain three elements
91 #
92 0 $MULTITONS{$engine} = $self, $MULTITONS_FOR_GLOBAL_DESTRUCTION{$engine} = { engine => $engine, class => $class, dispose => $dispose }
93 }
94  
95 0 return $self
96 }
97  
98  
99 0 ▶sub _destroy {
Subroutine _destroy: not called CC=4 SCAR=30.0
100 0 my $self = shift;
101  
102 #
103 # No op if it is a shallow'ed instance
104 #
105 0 ▶ return if $self->{shallow};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shallow"} 0 0 0
106  
107 #
108 # Here it should never happen that engine is not set
109 #
110 #
111 # For debugging
112 #
113 # {
114 # my $class = $self->{class};
115 # my $engine = $self->{engine};
116 # printf STDERR "DISPOSE %s engine %d\n", $class, $engine;
117 # }
118 0 my $engine = $self->{engine};
119 0 ▶ if (! $engine) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $engine) { } 0 0 0
120 0 warn "$self has no engine"
121 } else {
122 0 my $dispose = $self->{dispose};
123 #
124 # We voluntarily do not say $self->$dispose() because of
125 # global destruction mode, where instances are not used
126 #
127 0 $dispose->($self)
128 }
129  
130 #
131 # Delete from constants
132 #
133 0 ▶ if ($self->{multiton}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"multiton"}) 0 0 0
134 0 delete $MULTITONS{$engine}, delete $MULTITONS_FOR_GLOBAL_DESTRUCTION{$engine}
135 }
136 }
137  
138 0 ▶sub _global_destroy {
Subroutine _global_destroy: not called CC=1 SCAR=6.9
139 0 my %COPY = %MULTITONS_FOR_GLOBAL_DESTRUCTION;
140 0 %MULTITONS_FOR_GLOBAL_DESTRUCTION = ();
141  
142 0 my @engines = keys %COPY;
143 #
144 # We always look at MarpaX::ESLIF children first, then the MarpaX::ESLIF instances.
145 # Take care, we do not have instances anymore.
146 #
147 0 map { _destroy($COPY{$_}) } grep { $COPY{$_}->{class} ne 'MarpaX::ESLIF' } @engines;
148 0 map { _destroy($COPY{$_}) } grep { $COPY{$_}->{class} eq 'MarpaX::ESLIF' } @engines;
149 }
150  
151 #
152 # Note that we rely on at least one object to be referenced to have global destruction working.
153 # This is in theory always happening as soon as a multiton is created, and there is always at
154 # least one multiton: a MarpaX::ESLIF instance.
155 #
156 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
157 0 ▶ if (in_global_destruction) {
Branch: 1 branch
BranchTrueFalseTotal
if (&in_global_destruction()) { } 0 0 0
158 0 goto \&_global_destroy
159 } else {
160 0 goto \&_destroy
161 }
162 }
163  
164  
165 0 ▶sub _clone {
Subroutine _clone: not called CC=3 SCAR=24.8
166 0 my $self = shift;
167  
168 0 my $engine;
169 0 ▶ if ($self->{clonable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'clonable'}) { } 0 0 0
170 #
171 # For debugging
172 #
173 # {
174 # my $class = $self->{class};
175 # my $engine = $self->{engine};
176 # printf STDERR "CLONE %s engine %d", $class, $engine;
177 # }
178 0 my $allocate = $self->{allocate};
179 0 my $arguments = $self->{arguments};
180 0 $engine = $self->{engine} = $self->$allocate(@{$arguments});
181 #
182 # For debugging
183 #
184 # {
185 # my $engine = $self->{engine};
186 # printf STDERR " ==> engine %d\n", $engine;
187 # }
188 } else {
189 0 $engine = $self->{engine} = 0;
190 }
191  
192 0 ▶ if ($self->{multiton}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"multiton"}) 0 0 0
193 $MULTITONS{$engine} = $self, $MULTITONS_FOR_GLOBAL_DESTRUCTION{$engine} = { engine => $engine, class => $self->{class}, dispose => $self->{dispose} }
194 0 }
195 }
196  
197 #
198 # Note that it is very important to execute the CLONE method once. This is why
199 # we test on $class eq __PACKAGE__, because all MarpaX::ESLIF objects inherit
200 # from it.
201 # Another way to prevent this is to have a CLONE_SKIP in all sub-classes. Still
202 # the test remains, because it is safe.
203 #
204 0 ▶sub CLONE {
Subroutine CLONE: not called CC=2 SCAR=17.9
205 0 my $class = shift;
206 0 ▶ return unless $class eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq __PACKAGE__ 0 0 0
207  
208 0 my @multitons = values %MULTITONS;
209 0 %MULTITONS = ();
210 0 %MULTITONS_FOR_GLOBAL_DESTRUCTION = ();
211  
212 #
213 # For debugging
214 #
215 # print STDERR "CLONE @_ on @multitons\n";
216  
217 #
218 # We always look at MarpaX::ESLIF multitons first, then the others.
219 #
220 0 map { $_->_clone() } grep { $_->{class} eq 'MarpaX::ESLIF' } @multitons;
221 0 map { $_->_clone() } grep { $_->{class} ne 'MarpaX::ESLIF' } @multitons;
222 }
223  
224  
225 0 ▶sub SHALLOW {
Subroutine SHALLOW: not called CC=3 SCAR=24.8
Pod documented
226 0 my $proto = shift;
227  
228 0 ▶ my $class = ref($proto) || $proto; # Because of MarpaX::ESLIF::Recognizer::newFrom that is an instance method
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
229  
230 0 ▶ my $engine = shift // croak "\$engine is not defined";
Condition0%0/2
shift() // croak("\$engine is not defined")
l!l
00
MC/DC0%0/1
shift() // croak("\$engine is not defined")
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
231  
232 0 return bless { engine => $engine,
233 allocate => undef,
234 dispose => undef,
235 multiton => 0,
236 clonable => 0,
237 class => $class,
238 shallow => 1,
239 arguments => undef}, $class;
240 }
241  
242 18 1;
243  
244 __END__
« blib/lib/MarpaX/ESLIF/Symbol.pm ↑ Summary blib/lib/MarpaX/ESLIF/JSON/Encoder.pm »