Class/Fields/Fuxor.pm

filter: Statementstmt 89.8% 71 / 79 Branchbran 57.1% 8 / 14 Conditioncond 40.0% 10 / 25 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 93.7% 15 / 16 Podpod 100% 6 / 6 totaltotal 71.6% 111 / 155 CC 18 SCAR 20.5
CC
18
Cov
75%
add_field_set
18.5
has_attr
6.0
has_fields
4.1
CRAP 7.8
« Class/Fields.pm ↑ Summary private.pm »
1 package Class::Fields::Fuxor;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use vars qw(@ISA @EXPORT $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 $VERSION = '0.06';
10  
11 6 require Exporter;
12 6 @ISA = qw(Exporter);
13  
14 6 @EXPORT = qw(add_fields
15 add_field_set
16 has_fields
17 get_fields
18 get_attr
19 has_attr
20 );
21  
22  
23 6 ▶use constant TRUE => (1==1);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use constant FALSE => !TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use constant SUCCESS => TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use constant FAILURE => !SUCCESS;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 ▶use Class::Fields::Attribs;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =pod
31  
32 =head1 NAME
33  
34 Class::Fields::Fuxor - Low level manipuation of object data members
35  
36 =head1 SYNOPSIS
37  
38 # As functions.
39 use Class::Fields::Fuxor;
40 add_fields($class, $attrib, @fields);
41 add_field_set($class, \@fields, \@attribs);
42 has_fields($class);
43 $fields = get_fields($class);
44 $fattr = get_attr($class);
45  
46  
47 # As methods.
48 package Foo;
49 use base qw( Class::Fields::Fuxor );
50  
51 Foo->add_fields($attrib, @fields);
52 Foo->has_fields;
53 $fields = Foo->get_fields;
54 $fattr = Foo->get_attr;
55
56  
57 =head1 DESCRIPTION
58  
59 This is a module for low level manipuation of the %FIELDS hash and its
60 accompying %fields::attr hash without actually touching them. Modules
61 like fields.pm, base.pm and public.pm make use of this module.
62  
63 %FIELDS and %fields::attr are currently used to store information
64 about the data members of classes. Since the current data inheritance
65 system, built around pseudo-hashes, is considered a bit twitchy, it is
66 wise to encapsulate and rope it off in the expectation that it will be
67 replaced with something better.
68  
69 Typically one does not want to mess with this stuff and instead uses
70 fields.pm and friends or perhaps Class::Fields.
71  
72 =cut
73  
74  
75 # The %attr hash holds the attributes of the currently assigned fields
76 # per class. The hash is indexed by class names and the hash value is
77 # an array reference. The array is indexed with the field numbers
78 # (minus one) and the values are integer bit masks (or undef). The
79 # size of the array also indicates the next field index to assign for
80 # additional fields in this class.
81 #
82 # BTW %attr is part of fields for legacy reasons. We alias it here to make
83 # life easier.
84 6 ▶use vars qw(%attr);
Subroutine BEGIN: called CC=1 SCAR=0.0
85 6 *attr = \%fields::attr;
86  
87 =pod
88  
89 =over 4
90  
91 =item B<add_fields>
92  
93 add_fields($class, $attrib, @fields);
94  
95 Adds a bunch of @fields to the given $class using the given $attrib.
96 For example:
97  
98 # Add the public fields 'this' and 'that' to the class Foo.
99 use Class::Fields::Attribs;
100 add_fields('Foo', PUBLIC, qw(this that));
101  
102 $attrib is built from the constants in Class::Fields::Attribs
103  
104 =cut
105  
106 22 ▶sub add_fields {
Subroutine add_fields: called CC=1 SCAR=0.0
Pod documented
107 22 my($proto, $attrib, @fields) = @_;
108 22 add_field_set($proto, \@fields, [($attrib) x @fields]);
109 }
110  
111 =pod
112  
113 =item B<add_field_set>
114  
115 add_field_set($class, \@fields, \@attribs);
116  
117 Functionally similar to add_fields(), excepting that it can add a
118 group of fields with different attributes all at once. This is
119 necessary for the proper functioning of fields.pm.
120  
121 Each element in @fields matches up with one in @attribs. Obviously,
122 the two arrays must be the same size.
123  
124 =cut
125  
126 22 ▶sub add_field_set {
Subroutine add_field_set: called CC=11 SCAR=29.2
Pod documented
127 # Read the first two parameters. The rest are field names.
128 22 my($proto, $new_fields, $new_attribs) = @_;
129  
130 22 assert(@$new_fields == @$new_attribs) if DEBUG;
131  
132 # Quick bail out if nothing is to be added.
133 22 ▶ return SUCCESS unless @$new_fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @$new_fields 2 20 22
134  
135 20 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
136
137 20 my $fields = get_fields($class);
138 20 my $fattr = get_attr($class);
139 20 my $next_fno = @$fattr;
140  
141  
142 # Check for existing fields not belonging to base classes.
143 # Indicates a possible module reload.
144 20 ▶ if ($next_fno > $fattr->[0]
Branch: 1 branch
BranchTrueFalseTotal
if ($next_fno > $fattr->[0] and ($$fields{$new_fields->[0]} || 0) >= $fattr->[0]) 0 20 20
Condition60%3/5
$next_fno > $fattr->[0] and ($$fields{$new_fields->[0]} || 0) >= $fattr->[0]
!ll&&!rl&&r
9110
$$fields{$new_fields->[0]} || 0
l!l
011
MC/DC0%0/2
$next_fno > $fattr->[0] and ($$fields{$new_fields->[0]} || 0) >= $fattr->[0]
$next_fno > $fattr->[0] $$fields{$new_fields->[0]}
Truth table66%2/3
A and B
A= $next_fno > $fattr->[0]B= $$fields{$new_fields->[0]}
ABresult
0X0
100
111
145 and ($fields->{$new_fields->[0]} || 0) >= $fattr->[0])
146 {
147 # Reset the next pointer to let the reload work.
148 0 $next_fno = $fattr->[0];
149 }
150  
151 # Go through the fields and attach attributes.
152 20 foreach my $idx (0..$#{$new_fields}) {
153 48 my $f = $new_fields->[$idx];
154 48 my $attrib = $new_attribs->[$idx];
155 48 my $fno = $fields->{$f};
156  
157 # Allow the module to be reloaded so long as field positions
158 # have not changed.
159 48 ▶ if ($fno and $fno != $next_fno) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fno and $fno != $next_fno) 0 48 48
Condition33%1/3
$fno and $fno != $next_fno
!ll&&!rl&&r
4800
MC/DC0%0/2
$fno and $fno != $next_fno
$fno $fno != $next_fno
Truth table33%1/3
A and B
A= $fnoB= $fno != $next_fno
ABresult
0X0
100
111
160 0 require Carp;
161 0 ▶ if ($fno < $fattr->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fno < $fattr->[0]) { } 0 0 0
162 0 ▶ Carp::carp("Hides field '$f' in base class") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
163 } else {
164 0 Carp::croak("Field name '$f' already in use");
165 }
166 }
167 48 $fields->{$f} = $next_fno;
168 48 $fattr->[$next_fno] = $attrib;
169 48 $next_fno++;
170 }
171 }
172  
173  
174 =item B<has_fields>
175  
176 has_fields($class);
177  
178 A simple check to see if the given $class has a %FIELDS hash defined.
179 A simple test like (defined %{"$class\::FIELDS"}) will sometimes
180 produce typo warnings because it would create the hash if it was not
181 present before.
182  
183 =cut
184  
185 15 ▶sub has_fields {
Subroutine has_fields: called CC=4 SCAR=14.0
Pod documented
186 15 my($proto) = shift;
187 15 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0150
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
188 15 my $fglob;
189 15 ▶ return ($fglob = ${"$class\::"}{"FIELDS"} and *$fglob{HASH}) ? TRUE
Branch: 1 branch
BranchTrueFalseTotal
($fglob = do { "${class}::" }->{'FIELDS'}) && *$fglob{'HASH'} ? : 10 5 15
Condition100%2/2
$fglob = do { "${class}::" }->{"FIELDS"} and *$fglob{"HASH"}
!ll
411
MC/DC100%1/1
$fglob = do { "${class}::" }->{"FIELDS"} and *$fglob{"HASH"}
$fglob = do { "${class}::" }->{"FIELDS"}
Truth table100%2/2
A
A= $fglob = do { "${class}::" }->{"FIELDS"}
Aresult
00
11
190 : FALSE;
191 }
192  
193 =item B<has_attr>
194  
195 has_attr($class);
196  
197 A simple check to see if the given $class has attributes.
198  
199 =cut
200  
201 0 ▶sub has_attr {
Subroutine has_attr: not called CC=2 SCAR=17.9
Pod documented
202 0 my($proto) = shift;
203 0 ▶ my($class) = ref $proto || $proto;
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
204 0 return exists $attr{$class};
205 }
206  
207 =item B<get_attr>
208  
209 $fattr = get_attr($class);
210  
211 Get's the field attribute array for the given $class. This is roughly
212 equivalent to $fields::attr{$class} but we put a nice wrapper around
213 it for compatibility and readability.
214  
215 $fattr is an array reference containing the attributes of the fields
216 in the given $class. Each entry in $fattr corresponds to the position
217 indicated by the $class's %FIELDS has. For example:
218  
219 package Foo;
220 use fields qw(this _that);
221  
222 $fattr = get_attr('Foo');
223  
224 # Get the attributes for '_that' in the class 'Foo'.
225 $that_attribs = print $fattr->[$Foo::FIELDS->{_that}];
226  
227 When possible, one should avoid using this function since it exposes
228 more implementation detail than I'd like. Class::Fields
229 should provide most of the functionality you'll need.
230  
231 =cut
232  
233 45 ▶sub get_attr {
Subroutine get_attr: called CC=3 SCAR=11.2
Pod documented
234 45 my($proto) = shift;
235 45 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0450
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
236 45 ▶ unless ( defined $attr{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Class::Fields::Fuxor::attr{$class}) 9 36 45
237 9 $attr{$class} = [1];
238 }
239 45 return $attr{$class};
240 }
241  
242 =pod
243  
244 =item B<get_fields>
245  
246 $fields = get_fields($class);
247  
248 Gets a reference to the %FIELDS hash for the given $class. It will
249 autogenerate a %FIELDS hash if one doesn't already exist. If you
250 don't want this behavior, be sure to check beforehand with
251 has_fields().
252  
253 When possible, one should avoid using this function since it exposes
254 more implementation detail than I'd like. Class::Fields
255 should provide most of the functionality you'll need.
256  
257 =cut
258  
259 48 ▶sub get_fields {
Subroutine get_fields: called CC=2 SCAR=7.1
Pod documented
260 48 my($proto) = shift;
261 48 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0480
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
262  
263 # Shut up a possible typo warning.
264 48 () = \%{$class.'::FIELDS'};
265  
266 48 return \%{$class.'::FIELDS'};
267 }
268  
269 =pod
270  
271 =back
272  
273 =head1 AUTHOR
274  
275 Michael G Schwern <schwern@pobox.com> based heavily on code liberated
276 from the original fields.pm and base.pm.
277  
278  
279 =head1 SEE ALSO
280  
281 L<fields>, L<base>, L<public>, L<private>, L<protected>,
282 L<Class::Fields>, L<Class::Fields::Attribs>
283  
284 =cut
285  
286 6 return 'Maybe we should have stopped with Smalltalk.';
« Class/Fields.pm ↑ Summary private.pm »