}
sub set_value { shift->set_raw_value(@_) }
-sub get_value { shift->get_raw_value(@_) }
sub set_raw_value {
- my ($self, $instance, $value) = @_;
+ my $self = shift;
+ my ($instance, $value) = @_;
+
+ my $mi = Class::MOP::Class->initialize(ref($instance))->get_meta_instance;
+ return $mi->set_slot_value($instance, $self->name, $value);
+}
+
+sub _inline_set_value {
+ my $self = shift;
+ return $self->_inline_instance_set(@_) . ';';
+}
- Class::MOP::Class->initialize(ref($instance))
- ->get_meta_instance
- ->set_slot_value($instance, $self->name, $value);
+sub _inline_instance_set {
+ my $self = shift;
+ my ($instance, $value) = @_;
+
+ my $mi = $self->associated_class->get_meta_instance;
+ return $mi->inline_set_slot_value($instance, $self->name, $value);
}
+sub get_value { shift->get_raw_value(@_) }
+
sub get_raw_value {
- my ($self, $instance) = @_;
+ my $self = shift;
+ my ($instance) = @_;
- Class::MOP::Class->initialize(ref($instance))
- ->get_meta_instance
- ->get_slot_value($instance, $self->name);
+ my $mi = Class::MOP::Class->initialize(ref($instance))->get_meta_instance;
+ return $mi->get_slot_value($instance, $self->name);
+}
+
+sub _inline_get_value {
+ my $self = shift;
+ return $self->_inline_instance_get(@_) . ';';
+}
+
+sub _inline_instance_get {
+ my $self = shift;
+ my ($instance) = @_;
+
+ my $mi = $self->associated_class->get_meta_instance;
+ return $mi->inline_get_slot_value($instance, $self->name);
}
sub has_value {
- my ($self, $instance) = @_;
+ my $self = shift;
+ my ($instance) = @_;
- Class::MOP::Class->initialize(ref($instance))
- ->get_meta_instance
- ->is_slot_initialized($instance, $self->name);
+ my $mi = Class::MOP::Class->initialize(ref($instance))->get_meta_instance;
+ return $mi->is_slot_initialized($instance, $self->name);
+}
+
+sub _inline_has_value {
+ my $self = shift;
+ return $self->_inline_instance_has(@_) . ';';
+}
+
+sub _inline_instance_has {
+ my $self = shift;
+ my ($instance) = @_;
+
+ my $mi = $self->associated_class->get_meta_instance;
+ return $mi->inline_is_slot_initialized($instance, $self->name);
}
sub clear_value {
- my ($self, $instance) = @_;
+ my $self = shift;
+ my ($instance) = @_;
+
+ my $mi = Class::MOP::Class->initialize(ref($instance))->get_meta_instance;
+ return $mi->deinitialize_slot($instance, $self->name);
+}
- Class::MOP::Class->initialize(ref($instance))
- ->get_meta_instance
- ->deinitialize_slot($instance, $self->name);
+sub _inline_clear_value {
+ my $self = shift;
+ return $self->_inline_instance_clear(@_) . ';';
+}
+
+sub _inline_instance_clear {
+ my $self = shift;
+ my ($instance) = @_;
+
+ my $mi = $self->associated_class->get_meta_instance;
+ return $mi->inline_deinitialize_slot($instance, $self->name);
}
## load em up ...
}
-sub inline_get {
- my $self = shift;
- my ($instance) = @_;
-
- return $self->associated_class->get_meta_instance->inline_get_slot_value(
- $instance, $self->name );
-}
-
-sub inline_set {
- my $self = shift;
- my ( $instance, $value ) = @_;
-
- return $self->associated_class->get_meta_instance->inline_set_slot_value(
- $instance, $self->name, $value );
-}
-
-sub inline_has {
- my $self = shift;
- my ($instance) = @_;
-
- return
- $self->associated_class->get_meta_instance
- ->inline_is_slot_initialized( $instance, $self->name );
-}
-
-sub inline_clear {
- my $self = shift;
- my ($instance) = @_;
-
- return
- $self->associated_class->get_meta_instance
- ->inline_deinitialize_slot( $instance, $self->name );
-}
-
1;
__END__
## generators
sub _generate_accessor_method {
- my $attr = (shift)->associated_attribute;
- return sub {
- $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
- $attr->get_value($_[0]);
- };
-}
+ my $self = shift;
+ my $attr = $self->associated_attribute;
-sub _generate_reader_method {
- my $attr = (shift)->associated_attribute;
return sub {
- confess "Cannot assign a value to a read-only accessor" if @_ > 1;
+ if (@_ >= 2) {
+ $attr->set_value($_[0], $_[1]);
+ }
$attr->get_value($_[0]);
};
}
-
-sub _generate_writer_method {
- my $attr = (shift)->associated_attribute;
- return sub {
- $attr->set_value($_[0], $_[1]);
- };
-}
-
-sub _generate_predicate_method {
- my $attr = (shift)->associated_attribute;
- return sub {
- $attr->has_value($_[0])
- };
-}
-
-sub _generate_clearer_method {
- my $attr = (shift)->associated_attribute;
- return sub {
- $attr->clear_value($_[0])
- };
-}
-
-## Inline methods
-
sub _generate_accessor_method_inline {
my $self = shift;
my $attr = $self->associated_attribute;
- my $code = try {
+ return try {
$self->_compile_code([
'sub {',
- $attr->inline_set('$_[0]', '$_[1]'),
- 'if scalar(@_) == 2;',
- $attr->inline_get('$_[0]') . ';',
+ 'if (@_ >= 2) {',
+ $attr->_inline_set_value('$_[0]', '$_[1]'),
+ '}',
+ $attr->_inline_get_value('$_[0]'),
'}',
]);
}
catch {
confess "Could not generate inline accessor because : $_";
};
+}
+
+sub _generate_reader_method {
+ my $self = shift;
+ my $attr = $self->associated_attribute;
- return $code;
+ return sub {
+ confess "Cannot assign a value to a read-only accessor"
+ if @_ > 1;
+ $attr->get_value($_[0]);
+ };
}
sub _generate_reader_method_inline {
my $self = shift;
my $attr = $self->associated_attribute;
- my $code = try {
+ return try {
$self->_compile_code([
'sub {',
'confess "Cannot assign a value to a read-only accessor"',
'if @_ > 1;',
- $attr->inline_get('$_[0]') . ';',
+ $attr->_inline_get_value('$_[0]'),
'}',
]);
}
catch {
confess "Could not generate inline reader because : $_";
};
+}
- return $code;
+sub _generate_writer_method {
+ my $self = shift;
+ my $attr = $self->associated_attribute;
+
+ return sub {
+ $attr->set_value($_[0], $_[1]);
+ };
}
sub _generate_writer_method_inline {
my $self = shift;
my $attr = $self->associated_attribute;
- my $code = try {
+ return try {
$self->_compile_code([
'sub {',
- $attr->inline_set('$_[0]', '$_[1]') . ';',
+ $attr->_inline_set_value('$_[0]', '$_[1]'),
'}',
]);
}
catch {
confess "Could not generate inline writer because : $_";
};
+}
+
+sub _generate_predicate_method {
+ my $self = shift;
+ my $attr = $self->associated_attribute;
- return $code;
+ return sub {
+ $attr->has_value($_[0])
+ };
}
sub _generate_predicate_method_inline {
my $self = shift;
my $attr = $self->associated_attribute;
- my $code = try {
+ return try {
$self->_compile_code([
'sub {',
- $attr->inline_has('$_[0]') . ';',
+ $attr->_inline_has_value('$_[0]'),
'}',
]);
}
catch {
confess "Could not generate inline predicate because : $_";
};
+}
- return $code;
+sub _generate_clearer_method {
+ my $self = shift;
+ my $attr = $self->associated_attribute;
+
+ return sub {
+ $attr->clear_value($_[0])
+ };
}
sub _generate_clearer_method_inline {
my $self = shift;
my $attr = $self->associated_attribute;
- my $code = try {
+ return try {
$self->_compile_code([
'sub {',
- $attr->inline_clear('$_[0]') . ';',
+ $attr->_inline_clear_value('$_[0]'),
'}',
]);
}
catch {
confess "Could not generate inline clearer because : $_";
};
-
- return $code;
}
1;
if (defined(my $init_arg = $attr->init_arg)) {
my @source = (
'if (exists $params->{\'' . $init_arg . '\'}) {',
- $attr->inline_set(
+ $attr->_inline_set_value(
'$instance', '$params->{\'' . $init_arg . '\'}'
- ) . ';',
+ ),
'}',
);
if (defined $default) {
push @source, (
'else {',
- $attr->inline_set('$instance', $default) . ';',
+ $attr->_inline_set_value('$instance', $default),
'}',
);
}
return @source;
}
elsif (defined $default) {
- return ($attr->inline_set('$instance', $default) . ';');
+ return $attr->_inline_set_value('$instance', $default);
}
else {
return ();
install_accessors
remove_accessors
- inline_get
- inline_set
- inline_has
- inline_clear
+ _inline_get_value
+ _inline_set_value
+ _inline_has_value
+ _inline_clear_value
+ _inline_instance_get
+ _inline_instance_set
+ _inline_instance_has
+ _inline_instance_clear
_new
);