From: Dave Rolsky Date: Wed, 22 Sep 2010 21:49:23 +0000 (-0500) Subject: Moved even more code up to Native/Writer X-Git-Tag: 1.15~119 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a6ae743834ab40ad7ce85880c85cf2c748d423be;p=gitmo%2FMoose.git Moved even more code up to Native/Writer --- diff --git a/lib/Moose/Meta/Attribute/Native/Trait.pm b/lib/Moose/Meta/Attribute/Native/Trait.pm index 2a77fca..6727e33 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait.pm @@ -92,6 +92,7 @@ around '_make_delegation_method' => sub { package_name => $self->associated_class->name, attribute => $self, curried_arguments => \@curried_args, + root_types => [ $self->_root_types ], ); } # XXX - bridge code @@ -115,6 +116,10 @@ around '_make_delegation_method' => sub { } }; +sub _root_types { + return $_[0]->_helper_type; +} + sub _native_accessor_class_for { my ( $self, $suffix ) = @_; diff --git a/lib/Moose/Meta/Method/Accessor/Native.pm b/lib/Moose/Meta/Method/Accessor/Native.pm index de7ec8b..432800c 100644 --- a/lib/Moose/Meta/Method/Accessor/Native.pm +++ b/lib/Moose/Meta/Method/Accessor/Native.pm @@ -58,6 +58,8 @@ sub _new { return bless $options, $class; } +sub root_types { (shift)->{'root_types'} } + sub _initialize_body { my $self = shift; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm index fe22c03..ab4c7f4 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Array/Writer.pm @@ -37,16 +37,6 @@ sub _inline_tc_code { } } -sub _constraint_must_be_checked { - my $self = shift; - - my $attr = $self->associated_attribute; - - return $attr->has_type_constraint - && ( $attr->type_constraint->name ne 'ArrayRef' - || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) ); -} - sub _check_new_members_only { my $self = shift; @@ -83,17 +73,6 @@ sub _inline_check_member_constraint { ) . " for $new_value;"; } -sub _inline_check_coercion { - my ( $self, $value ) = @_; - - my $attr = $self->associated_attribute; - - return '' - unless $attr->should_coerce && $attr->type_constraint->has_coercion; - - return "$value = \$type_constraint_obj->coerce($value);"; -} - sub _inline_check_constraint { my $self = shift; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/Writer.pm deleted file mode 100644 index de6fa33..0000000 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/Writer.pm +++ /dev/null @@ -1,20 +0,0 @@ -package Moose::Meta::Method::Accessor::Native::Bool::Writer; - -use strict; -use warnings; - -our $VERSION = '1.13'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - -use base 'Moose::Meta::Method::Accessor::Native::Writer'; - -sub _new_value {q{}} -sub _potential_value {q{}} - -sub _value_needs_copy {0} - -# The Bool type does not have any methods that take a user-provided value -sub _inline_tc_code {q{}} - -1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm index ca3a8bd..917643f 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/set.pm @@ -7,11 +7,13 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::Bool::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 0 } sub _maximum_arguments { 0 } +sub _potential_value { 1 } + sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm index d5ed290..2640cc9 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/toggle.pm @@ -7,11 +7,17 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::Bool::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 0 } sub _maximum_arguments { 0 } +sub _potential_value { + my ( $self, $slot_access ) = @_; + + return "$slot_access ? 0 : 1;"; +} + sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; diff --git a/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm b/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm index d7732b5..f7dd86d 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Bool/unset.pm @@ -7,11 +7,13 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::Bool::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 0 } sub _maximum_arguments { 0 } +sub _potential_value { 0 } + sub _inline_optimized_set_new_value { my ( $self, $inv, $new, $slot_access ) = @_; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/String/Writer.pm deleted file mode 100644 index b8583bc..0000000 --- a/lib/Moose/Meta/Method/Accessor/Native/String/Writer.pm +++ /dev/null @@ -1,37 +0,0 @@ -package Moose::Meta::Method::Accessor::Native::String::Writer; - -use strict; -use warnings; - -our $VERSION = '1.13'; -$VERSION = eval $VERSION; -our $AUTHORITY = 'cpan:STEVAN'; - -use base 'Moose::Meta::Method::Accessor::Native::Writer'; - -sub _new_value {'$_[0]'} - -sub _constraint_must_be_checked { - my $self = shift; - - my $attr = $self->associated_attribute; - - return $attr->has_type_constraint - && ( $attr->type_constraint->name ne 'Str' - || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) ); -} - -sub _inline_check_coercion { - my ( $self, $value ) = @_; - - my $attr = $self->associated_attribute; - - return '' - unless $attr->should_coerce && $attr->type_constraint->has_coercion; - - # We want to break the aliasing in @_ in case the coercion tries to make a - # destructive change to an array member. - return '@_ = @{ $attr->type_constraint->coerce($value) };'; -} - -1; diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/append.pm b/lib/Moose/Meta/Method/Accessor/Native/String/append.pm index 7652d87..d4f4043 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/append.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/append.pm @@ -7,7 +7,7 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::String::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 1 } sub _maximum_arguments { 1 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm b/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm index eb2e333..6876dab 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/chomp.pm @@ -7,7 +7,7 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::String::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 0 } sub _maximum_arguments { 0 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm b/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm index 118eec8..dc24c8a 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/chop.pm @@ -7,7 +7,7 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::String::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 0 } sub _maximum_arguments { 0 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm b/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm index 95ea12b..403f8bc 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/clear.pm @@ -7,7 +7,7 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::String::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 0 } sub _maximum_arguments { 0 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm b/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm index bdbfb57..5ca4a50 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/inc.pm @@ -7,7 +7,7 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::String::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 0 } sub _maximum_arguments { 0 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm b/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm index afa145b..22465a0 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/prepend.pm @@ -7,7 +7,7 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::String::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 1 } sub _maximum_arguments { 1 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm b/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm index 780f099..c46f961 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/replace.pm @@ -7,7 +7,7 @@ our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Moose::Meta::Method::Accessor::Native::String::Writer'; +use base 'Moose::Meta::Method::Accessor::Native::Writer'; sub _minimum_arguments { 1 } sub _maximum_arguments { 2 } diff --git a/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm b/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm index 77338f5..18f736d 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/String/substr.pm @@ -9,7 +9,7 @@ our $AUTHORITY = 'cpan:STEVAN'; use base qw( Moose::Meta::Method::Accessor::Native::Reader - Moose::Meta::Method::Accessor::Native::String::Writer + Moose::Meta::Method::Accessor::Native::Writer ); sub _generate_method { diff --git a/lib/Moose/Meta/Method/Accessor/Native/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Writer.pm index 1d124ec..22549ab 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Writer.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Writer.pm @@ -3,6 +3,8 @@ package Moose::Meta::Method::Accessor::Native::Writer; use strict; use warnings; +use List::MoreUtils qw( any ); + our $VERSION = '1.13'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -72,12 +74,32 @@ sub _inline_process_arguments {q{}} sub _inline_check_arguments {q{}} +sub _new_value {'$_[0]'} + sub _value_needs_copy { my $self = shift; return $self->_constraint_must_be_checked; } +sub _constraint_must_be_checked { + my $self = shift; + + my $attr = $self->associated_attribute; + + return $attr->has_type_constraint + && ( !$self->_is_root_type( $attr->type_constraint ) + || ( $attr->should_coerce && $attr->type_constraint->has_coercion ) ); +} + +sub _is_root_type { + my ($self, $type) = @_; + + my $name = $type->name(); + + return any { $name eq $_ } @{ $self->root_types }; +} + sub _inline_copy_value { my ( $self, $potential_ref ) = @_; @@ -100,7 +122,16 @@ sub _inline_tc_code { } sub _inline_check_coercion { - die '_inline_check_coercion must be overridden by ' . ref $_[0]; + my ( $self, $value ) = @_; + + my $attr = $self->associated_attribute; + + return '' + unless $attr->should_coerce && $attr->type_constraint->has_coercion; + + # We want to break the aliasing in @_ in case the coercion tries to make a + # destructive change to an array member. + return "$value = \$type_constraint_obj->coerce($value);"; } sub _inline_check_constraint { @@ -111,10 +142,6 @@ sub _inline_check_constraint { return $self->SUPER::_inline_check_constraint( $_[0] ); } -sub _constraint_must_be_checked { - die '_constraint_must_be_checked must be overridden by ' . ref $_[0]; -} - sub _inline_capture_return_value { return q{} } sub _inline_set_new_value {