1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.09003';
11 $VERSION = eval $VERSION;
13 # Class::XSAccessor is segfaulting on win32, so be careful
14 # Win32 users can set $hasXS to try to use it anyway
20 if (not defined $hasXS) {
23 if ($^O ne 'MSWin32') {
25 require Class::XSAccessor;
36 Class::Accessor::Grouped - Lets you build groups of accessors
42 This class lets you build groups of accessors that will call different
47 =head2 mk_group_accessors
51 =item Arguments: $group, @fieldspec
57 Creates a set of accessors in a given group.
59 $group is the name of the accessor group for the generated accessors; they
60 will call get_$group($field) on get and set_$group($field, $value) on set.
62 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
63 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
66 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
67 this is used as both field and accessor name, if a listref it is expected to
68 be of the form [ $accessor, $field ].
72 sub mk_group_accessors {
73 my ($self, $group, @fields) = @_;
75 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
82 no warnings 'redefine';
84 sub _mk_group_accessors {
85 my($self, $maker, $group, @fields) = @_;
86 my $class = Scalar::Util::blessed $self || $self;
88 # So we don't have to do lots of lookups inside the loop.
89 $maker = $self->can($maker) unless ref $maker;
93 foreach my $field (@fields) {
94 if( $field eq 'DESTROY' ) {
95 Carp::carp("Having a data accessor named DESTROY in ".
96 "'$class' is unwise.");
101 ($name, $field) = @$field if ref $field;
103 my $alias = "_${name}_accessor";
104 my $full_name = join('::', $class, $name);
105 my $full_alias = join('::', $class, $alias);
107 if ( $hasXS && $group eq 'simple' ) {
108 require Class::XSAccessor;
109 Class::XSAccessor::newxs_accessor("${class}::${name}", $field, 0);
110 Class::XSAccessor::newxs_accessor("${class}::${alias}", $field, 0);
112 # XXX: is the alias accessor really necessary?
115 my $accessor = $self->$maker($group, $field);
116 my $alias_accessor = $self->$maker($group, $field);
118 *$full_name = Sub::Name::subname($full_name, $accessor);
119 #unless defined &{$class."\:\:$field"}
121 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
122 #unless defined &{$class."\:\:$alias"}
128 =head2 mk_group_ro_accessors
132 =item Arguments: $group, @fieldspec
138 Creates a set of read only accessors in a given group. Identical to
139 L</mk_group_accessors> but accessors will throw an error if passed a value
140 rather than setting the value.
144 sub mk_group_ro_accessors {
145 my($self, $group, @fields) = @_;
147 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
150 =head2 mk_group_wo_accessors
154 =item Arguments: $group, @fieldspec
160 Creates a set of write only accessors in a given group. Identical to
161 L</mk_group_accessors> but accessors will throw an error if not passed a
162 value rather than getting the value.
166 sub mk_group_wo_accessors {
167 my($self, $group, @fields) = @_;
169 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
172 =head2 make_group_accessor
176 =item Arguments: $group, $field
178 Returns: $sub (\CODE)
182 Returns a single accessor in a given group; called by mk_group_accessors
183 for each entry in @fieldspec.
187 sub make_group_accessor {
188 my ($class, $group, $field) = @_;
190 my $set = "set_$group";
191 my $get = "get_$group";
193 # eval for faster fastiness
196 return shift->$set('$field', \@_);
199 return shift->$get('$field');
204 =head2 make_group_ro_accessor
208 =item Arguments: $group, $field
210 Returns: $sub (\CODE)
214 Returns a single read-only accessor in a given group; called by
215 mk_group_ro_accessors for each entry in @fieldspec.
219 sub make_group_ro_accessor {
220 my($class, $group, $field) = @_;
222 my $get = "get_$group";
226 my \$caller = caller;
227 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
228 \"objects of class '$class'\");
231 return shift->$get('$field');
236 =head2 make_group_wo_accessor
240 =item Arguments: $group, $field
242 Returns: $sub (\CODE)
246 Returns a single write-only accessor in a given group; called by
247 mk_group_wo_accessors for each entry in @fieldspec.
251 sub make_group_wo_accessor {
252 my($class, $group, $field) = @_;
254 my $set = "set_$group";
258 my \$caller = caller;
259 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
260 \"objects of class '$class'\");
263 return shift->$set('$field', \@_);
272 =item Arguments: $field
278 Simple getter for hash-based objects which returns the value for the field
279 name passed as an argument.
284 return $_[0]->{$_[1]};
291 =item Arguments: $field, $new_value
297 Simple setter for hash-based objects which sets and then returns the value
298 for the field name passed as an argument.
303 return $_[0]->{$_[1]} = $_[2];
311 =item Arguments: $field
317 Simple getter for Classes and hash-based objects which returns the value for
318 the field name passed as an argument. This behaves much like
319 L<Class::Data::Accessor> where the field can be set in a base class,
320 inherited and changed in subclasses, and inherited and changed for object
328 if (Scalar::Util::blessed $_[0]) {
329 my $reftype = Scalar::Util::reftype $_[0];
332 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
333 return $_[0]->{$_[1]};
334 } elsif ($reftype ne 'HASH') {
335 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
342 no warnings qw/uninitialized/;
343 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
345 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
346 my $pkg_gen = mro::get_pkg_gen ($class);
347 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
348 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
349 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
352 foreach (@{$class.'::__cag_supers'}) {
353 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
363 =item Arguments: $field, $new_value
369 Simple setter for Classes and hash-based objects which sets and then returns
370 the value for the field name passed as an argument. When called on a hash-based
371 object it will set the appropriate hash key value. When called on a class, it
372 will set a class level variable.
374 B<Note:>: This method will die if you try to set an object variable on a non
380 if (Scalar::Util::blessed $_[0]) {
381 if (Scalar::Util::reftype $_[0] eq 'HASH') {
382 return $_[0]->{$_[1]} = $_[2];
384 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
389 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
393 =head2 get_component_class
397 =item Arguments: $field
403 Gets the value of the specified component class.
405 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
407 $self->result_class->method();
410 $self->get_component_class('result_class')->method();
414 sub get_component_class {
415 return $_[0]->get_inherited($_[1]);
418 =head2 set_component_class
422 =item Arguments: $field, $class
428 Inherited accessor that automatically loads the specified class before setting
429 it. This method will die if the specified class could not be loaded.
431 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
432 __PACKAGE__->result_class('MyClass');
434 $self->result_class->method();
438 sub set_component_class {
441 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
444 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
448 return $_[0]->set_inherited($_[1], $_[2]);
451 =head2 get_super_paths
453 Returns a list of 'parent' or 'super' class names that the current class inherited from.
457 sub get_super_paths {
458 my $class = Scalar::Util::blessed $_[0] || $_[0];
460 return @{mro::get_linear_isa($class)};
467 You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
468 Note however that the use of this module is disabled by default on Win32
469 systems, as it causes yet unresolved segfaults. If you are a Win32 user, and
470 want to try this module with L<Class::XSAccessor>, set
471 C<$Class::Accessor::Grouped::hasXS> to a true value B<before> registering
472 your accessors (e.g. in a C<BEGIN> block)
476 Matt S. Trout <mst@shadowcatsystems.co.uk>
477 Christopher H. Laco <claco@chrislaco.com>
479 With contributions from:
481 Guillermo Roditi <groditi@cpan.org>
483 =head1 COPYRIGHT & LICENSE
485 Copyright (c) 2006-2010 Matt S. Trout <mst@shadowcatsystems.co.uk>
487 This program is free software; you can redistribute it and/or modify
488 it under the same terms as perl itself.