Refactored native trait inlining some more - added an optimized path to avoid copying...
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native.pm
CommitLineData
f7fd22b6 1package Moose::Meta::Method::Accessor::Native;
2
3use strict;
4use warnings;
5
6use Carp qw( confess );
7use Scalar::Util qw( blessed weaken );
8
9our $VERSION = '1.13';
10$VERSION = eval $VERSION;
11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Moose::Meta::Method::Accessor', 'Moose::Meta::Method::Delegation';
14
15sub new {
16 my $class = shift;
17 my %options = @_;
18
19 die "Cannot instantiate a $class object directly"
20 if $class eq __PACKAGE__;
21
22 ( exists $options{attribute} )
23 || confess "You must supply an attribute to construct with";
24
25 ( blessed( $options{attribute} )
26 && $options{attribute}->isa('Class::MOP::Attribute') )
27 || confess
28 "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
29
30 ( $options{package_name} && $options{name} )
31 || confess "You must supply the package_name and name parameters";
32
33 exists $options{curried_arguments}
34 || ( $options{curried_arguments} = [] );
35
36 ( $options{curried_arguments}
37 && ( 'ARRAY' eq ref $options{curried_arguments} ) )
38 || confess
39 'You must supply a curried_arguments which is an ARRAY reference';
40
41 $options{delegate_to_method} = lc( ( split /::/, $class)[-1] );
42
f5f08b5f 43 $options{definition_context} = $options{attribute}->definition_context;
44
f7fd22b6 45 my $self = $class->_new( \%options );
46
47 weaken( $self->{'attribute'} );
48
49 $self->_initialize_body;
50
51 return $self;
52}
53
54sub _new {
55 my $class = shift;
56 my $options = @_ == 1 ? $_[0] : {@_};
57
58 return bless $options, $class;
59}
60
61sub _initialize_body {
62 my $self = shift;
63
64 $self->{'body'} = $self->_eval_code( $self->_generate_method );
65
66 return;
67}
68
f5f08b5f 69sub _eval_environment {
70 my $self = shift;
71
72 my $env = $self->SUPER::_eval_environment;
73
74 $env->{'@curried'} = $self->curried_arguments;
75
76 return $env;
77}
78
855f4af8 79sub _inline_curried_arguments {
80 my $self = shift;
81
82 return q{} unless @{ $self->curried_arguments };
83
84 return 'unshift @_, @curried;'
85}
86
87sub _inline_check_argument_count {
88 my $self = shift;
89
90 my $code = q{};
91
92 if ( my $min = $self->_minimum_arguments ) {
93 my $err_msg = sprintf(
94 q{"Cannot call %s without at least %s argument%s"},
95 $self->delegate_to_method,
96 $min,
97 ( $min == 1 ? q{} : 's' )
98 );
99
100 $code
101 .= "\n"
102 . $self->_inline_throw_error($err_msg)
103 . " unless \@_ >= $min;";
104 }
105
106 if ( defined( my $max = $self->_maximum_arguments ) ) {
107 my $err_msg = sprintf(
108 q{"Cannot call %s with %s argument%s"},
109 $self->delegate_to_method,
110 ( $max ? "more than $max" : 'any' ),
111 ( $max == 1 ? q{} : 's' )
112 );
113
114 $code
115 .= "\n"
116 . $self->_inline_throw_error($err_msg)
117 . " if \@_ > $max;";
118 }
119
120 return $code;
121}
122
123sub _minimum_arguments { 0 }
124sub _maximum_arguments { undef }
125
126sub _inline_check_arguments { q{} }
127
f7fd22b6 1281;