Big speedup for get_inherited under heavy usage
[p5sagit/Class-Accessor-Grouped.git] / lib / Class / Accessor / Grouped.pm
CommitLineData
963a69a5 1package Class::Accessor::Grouped;
2use strict;
3use warnings;
0fea2bfb 4use Carp;
e6f2a0fd 5use Class::ISA;
6use Scalar::Util qw/blessed reftype/;
963a69a5 7use vars qw($VERSION);
8
a49c32d9 9$VERSION = '0.03';
963a69a5 10
11=head1 NAME
12
1ad8d8c6 13Class::Accessor::Grouped - Lets you build groups of accessors
963a69a5 14
15=head1 SYNOPSIS
16
17=head1 DESCRIPTION
18
19This class lets you build groups of accessors that will call different
20getters and setters.
21
22=head1 METHODS
23
24=head2 mk_group_accessors
25
26=over 4
27
28=item Arguments: $group, @fieldspec
29
30Returns: none
31
32=back
33
34Creates a set of accessors in a given group.
35
36$group is the name of the accessor group for the generated accessors; they
37will call get_$group($field) on get and set_$group($field, $value) on set.
38
39@fieldspec is a list of field/accessor names; if a fieldspec is a scalar
40this is used as both field and accessor name, if a listref it is expected to
41be of the form [ $accessor, $field ].
42
43=cut
44
45sub mk_group_accessors {
46 my ($self, $group, @fields) = @_;
47
48 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
49 return;
50}
51
52
53{
54 no strict 'refs';
55 no warnings 'redefine';
56
57 sub _mk_group_accessors {
58 my($self, $maker, $group, @fields) = @_;
59 my $class = ref $self || $self;
60
61 # So we don't have to do lots of lookups inside the loop.
62 $maker = $self->can($maker) unless ref $maker;
63
64 foreach my $field (@fields) {
65 if( $field eq 'DESTROY' ) {
66 carp("Having a data accessor named DESTROY in ".
67 "'$class' is unwise.");
68 }
69
70 my $name = $field;
71
72 ($name, $field) = @$field if ref $field;
73
74 my $accessor = $self->$maker($group, $field);
75 my $alias = "_${name}_accessor";
76
77 #warn "$class $group $field $alias";
78
79 *{$class."\:\:$name"} = $accessor;
80 #unless defined &{$class."\:\:$field"}
81
82 *{$class."\:\:$alias"} = $accessor;
83 #unless defined &{$class."\:\:$alias"}
84 }
85 }
86}
87
88=head2 mk_group_ro_accessors
89
90=over 4
91
92=item Arguments: $group, @fieldspec
93
94Returns: none
95
96=back
97
98Creates a set of read only accessors in a given group. Identical to
99<L:/mk_group_accessors> but accessors will throw an error if passed a value
100rather than setting the value.
101
102=cut
103
104sub mk_group_ro_accessors {
105 my($self, $group, @fields) = @_;
106
107 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
108}
109
110=head2 mk_group_wo_accessors
111
112=over 4
113
114=item Arguments: $group, @fieldspec
115
116Returns: none
117
118=back
119
120Creates a set of write only accessors in a given group. Identical to
121<L:/mk_group_accessors> but accessors will throw an error if not passed a
122value rather than getting the value.
123
124=cut
125
126sub mk_group_wo_accessors {
127 my($self, $group, @fields) = @_;
128
129 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
130}
131
132=head2 make_group_accessor
133
134=over 4
135
136=item Arguments: $group, $field
137
138Returns: $sub (\CODE)
139
140=back
141
142Returns a single accessor in a given group; called by mk_group_accessors
143for each entry in @fieldspec.
144
145=cut
146
147sub make_group_accessor {
148 my ($class, $group, $field) = @_;
149
150 my $set = "set_$group";
151 my $get = "get_$group";
152
153 # Build a closure around $field.
154 return sub {
155 my $self = shift;
156
157 if(@_) {
158 return $self->$set($field, @_);
159 }
160 else {
161 return $self->$get($field);
162 }
163 };
164}
165
166=head2 make_group_ro_accessor
167
168=over 4
169
170=item Arguments: $group, $field
171
172Returns: $sub (\CODE)
173
174=back
175
176Returns a single read-only accessor in a given group; called by
177mk_group_ro_accessors for each entry in @fieldspec.
178
179=cut
180
181sub make_group_ro_accessor {
182 my($class, $group, $field) = @_;
183
184 my $get = "get_$group";
185
186 return sub {
187 my $self = shift;
188
189 if(@_) {
190 my $caller = caller;
191 croak("'$caller' cannot alter the value of '$field' on ".
192 "objects of class '$class'");
193 }
194 else {
195 return $self->$get($field);
196 }
197 };
198}
199
200=head2 make_group_wo_accessor
201
202=over 4
203
204=item Arguments: $group, $field
205
206Returns: $sub (\CODE)
207
208=back
209
210Returns a single write-only accessor in a given group; called by
211mk_group_wo_accessors for each entry in @fieldspec.
212
213=cut
214
215sub make_group_wo_accessor {
216 my($class, $group, $field) = @_;
217
218 my $set = "set_$group";
219
220 return sub {
221 my $self = shift;
222
223 unless (@_) {
224 my $caller = caller;
225 croak("'$caller' cannot access the value of '$field' on ".
226 "objects of class '$class'");
227 }
228 else {
229 return $self->$set($field, @_);
230 }
231 };
232}
233
234=head2 get_simple
235
236=over 4
237
238=item Arguments: $field
239
240Returns: $value
241
242=back
243
244Simple getter for hash-based objects which returns the value for the field
245name passed as an argument.
246
247=cut
248
249sub get_simple {
250 my ($self, $get) = @_;
251 return $self->{$get};
252}
253
254=head2 set_simple
255
256=over 4
257
258=item Arguments: $field, $new_value
259
260Returns: $new_value
261
262=back
263
264Simple setter for hash-based objects which sets and then returns the value
265for the field name passed as an argument.
266
267=cut
268
269sub set_simple {
270 my ($self, $set, $val) = @_;
271 return $self->{$set} = $val;
272}
273
e6f2a0fd 274
275=head2 get_inherited
276
277=over 4
278
279=item Arguments: $field
280
281Returns: $value
282
283=back
284
285Simple getter for Classes and hash-based objects which returns the value for the field name passed as
286an argument. This behaves much like L<Class::Data::Accessor> where the field can be set in a
287base class, inherited and changed in subclasses, and inherited and changed for object instances.
288
289=cut
290
291sub get_inherited {
292 my ($self, $get) = @_;
a49c32d9 293 my $class;
e6f2a0fd 294
295 if (blessed $self) {
a49c32d9 296 my $reftype = reftype $self;
297 $class = ref $self;
298
299 if ($reftype eq 'HASH' && exists $self->{$get}) {
e6f2a0fd 300 return $self->{$get};
a49c32d9 301 } elsif ($reftype ne 'HASH') {
e6f2a0fd 302 croak('Cannot get inherited value on an object instance that is not hash-based');
303 };
a49c32d9 304 } else {
305 $class = $self;
e6f2a0fd 306 };
307
308 no strict 'refs';
a49c32d9 309 return ${$class.'::__cag_'.$get} if defined(${$class.'::__cag_'.$get});
e6f2a0fd 310
a49c32d9 311 if (!@{$class.'::__cag_supers'}) {
312 @{$class.'::__cag_supers'} = $self->get_super_paths;
313 };
314
315 foreach (@{$class.'::__cag_supers'}) {
316 return ${$_.'::__cag_'.$get} if defined(${$_.'::__cag_'.$get});
e6f2a0fd 317 };
c46050d3 318
319 return;
e6f2a0fd 320}
321
322=head2 set_inherited
323
324=over 4
325
326=item Arguments: $field, $new_value
327
328Returns: $new_value
329
330=back
331
332Simple setter for Classes and hash-based objects which sets and then returns the value
333for the field name passed as an argument. When called on a hash-based object it will set the appropriate
334hash key value. When called on a class, it will set a class level variable.
335
336B<Note:>: This method will die if you try to set an object variable on a non hash-based object.
337
338=cut
339
340sub set_inherited {
341 my ($self, $set, $val) = @_;
342
343 if (blessed $self) {
344 if (reftype($self) eq 'HASH') {
345 return $self->{$set} = $val;
346 } else {
347 croak('Cannot set inherited value on an object instance that is not hash-based');
348 };
349 } else {
350 no strict 'refs';
351
a49c32d9 352 return ${$self.'::__cag_'.$set} = $val;
e6f2a0fd 353 };
354}
355
a49c32d9 356=head2 get_super_paths
357
358Returns a list of 'parent' or 'super' class names that the current class inherited from.
359
360=cut
361
362sub get_super_paths {
363 my $class = blessed $_[0] || $_[0];
364
365 return Class::ISA::super_path($class);
366};
367
963a69a5 3681;
369
370=head1 AUTHORS
371
372Matt S. Trout <mst@shadowcatsystems.co.uk>
97972dcb 373Christopher H. Laco <claco@chrislaco.com>
963a69a5 374
375=head1 LICENSE
376
377You may distribute this code under the same terms as Perl itself.
378
379=cut
380