Implement an idea of reducing inline constructors in basic metaclasses
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Constructor.pm
CommitLineData
d90b42a6 1
2package Class::MOP::Method::Constructor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
9
074ec38f 10our $VERSION = '0.89';
d519662a 11$VERSION = eval $VERSION;
d90b42a6 12our $AUTHORITY = 'cpan:STEVAN';
13
29d4e92a 14use base 'Class::MOP::Method::Inlined';
d90b42a6 15
16sub new {
17 my $class = shift;
18 my %options = @_;
8d2d4c67 19
ad315b75 20 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
21 || confess "You must pass a metaclass instance if you want to inline"
8d2d4c67 22 if $options{is_inline};
23
b38f3848 24 ($options{package_name} && $options{name})
32202ce2 25 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 26
0bfc85b8 27 my $self = $class->_new(\%options);
d90b42a6 28
8d2d4c67 29 # we don't want this creating
30 # a cycle in the code, if not
d90b42a6 31 # needed
8683db0e 32 weaken($self->{'associated_metaclass'});
d90b42a6 33
8f7852d8 34 $self->_initialize_body;
d90b42a6 35
8d2d4c67 36 return $self;
d90b42a6 37}
38
28b97bef 39sub _new {
0bfc85b8 40 my $class = shift;
ec9e38e5 41 return Class::MOP::Class->initialize($class)->new_object(@_)
42 if $class ne __PACKAGE__;
43
44 my $params = @_ == 1 ? $_[0] : {@_};
45
46 return bless {
47 # inherited from Class::MOP::Method
48 body => $params->{body},
49 # associated_metaclass => $params->{associated_metaclass}, # overriden
50 package_name => $params->{package_name},
51 name => $params->{name},
52 original_method => $params->{original_method},
53
54 # inherited from Class::MOP::Generated
55 is_inline => $params->{is_inline} || 0,
56 definition_context => $params->{definition_context},
57
58 # inherited from Class::MOP::Inlined
59 _expected_method_class => $params->{_expected_method_class},
60
61 # defined in this subclass
62 options => $params->{options} || {},
63 associated_metaclass => $params->{metaclass},
28b97bef 64 }, $class;
65}
66
8d2d4c67 67## accessors
c23184fc 68
8683db0e 69sub options { (shift)->{'options'} }
70sub associated_metaclass { (shift)->{'associated_metaclass'} }
c23184fc 71
565f0cbb 72## cached values ...
d90b42a6 73
8d2d4c67 74sub meta_instance {
c7e28c19 75 Carp::cluck('The meta_instance method has been made private.'
76 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 77 shift->_meta_instance;
5a49fb79 78}
79
80sub _meta_instance {
565f0cbb 81 my $self = shift;
8683db0e 82 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
565f0cbb 83}
c23184fc 84
8d2d4c67 85sub attributes {
c7e28c19 86 Carp::cluck('The attributes method has been made private.'
87 . " The public version is deprecated and will be removed in a future release.\n");
6e2f5a81 88
780f14c2 89 return shift->_attributes;
90}
91
92sub _attributes {
565f0cbb 93 my $self = shift;
2620be77 94 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
565f0cbb 95}
d90b42a6 96
97## method
98
565f0cbb 99sub initialize_body {
c7e28c19 100 Carp::cluck('The initialize_body method has been made private.'
101 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 102 shift->_initialize_body;
8f7852d8 103}
104
105sub _initialize_body {
565f0cbb 106 my $self = shift;
ecb874a0 107 my $method_name = '_generate_constructor_method';
8d2d4c67 108
565f0cbb 109 $method_name .= '_inline' if $self->is_inline;
8d2d4c67 110
8683db0e 111 $self->{'body'} = $self->$method_name;
565f0cbb 112}
113
114sub generate_constructor_method {
c7e28c19 115 Carp::cluck('The generate_constructor_method method has been made private.'
116 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 117 shift->_generate_constructor_method;
ecb874a0 118}
119
120sub _generate_constructor_method {
2a2b8458 121 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
565f0cbb 122}
123
124sub generate_constructor_method_inline {
c7e28c19 125 Carp::cluck('The generate_constructor_method_inline method has been made private.'
126 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 127 shift->_generate_constructor_method_inline;
ecb874a0 128}
129
130sub _generate_constructor_method_inline {
d90b42a6 131 my $self = shift;
565f0cbb 132
0c6f3280 133 my $close_over = {};
134
d90b42a6 135 my $source = 'sub {';
26ffbb36 136 $source .= "\n" . 'my $class = shift;';
8d2d4c67 137
26ffbb36 138 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
8d2d4c67 139 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
140
26ffbb36 141 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
142
5a49fb79 143 $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class');
8d2d4c67 144 $source .= ";\n" . (join ";\n" => map {
82cff1ae 145 $self->_generate_slot_initializer($_, $close_over)
780f14c2 146 } @{ $self->_attributes });
d90b42a6 147 $source .= ";\n" . 'return $instance';
8d2d4c67 148 $source .= ";\n" . '}';
149 warn $source if $self->options->{debug};
150
e24b19fb 151 my ( $code, $e ) = $self->_eval_closure(
ffe92c8b 152 $close_over,
153 $source
154 );
e24b19fb 155 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
a6eef5a3 156
157 return $code;
d90b42a6 158}
159
160sub _generate_slot_initializer {
161 my $self = shift;
64adcd8d 162 my $attr = shift;
82cff1ae 163 my $close = shift;
8d2d4c67 164
d90b42a6 165 my $default;
166 if ($attr->has_default) {
167 # NOTE:
168 # default values can either be CODE refs
8d2d4c67 169 # in which case we need to call them. Or
d90b42a6 170 # they can be scalars (strings/numbers)
171 # in which case we can just deal with them
172 # in the code we eval.
173 if ($attr->is_default_a_coderef) {
82cff1ae 174 my $idx = @{$close->{'@defaults'}||=[]};
175 push(@{$close->{'@defaults'}}, $attr->default);
176 $default = '$defaults[' . $idx . ']->($instance)';
d90b42a6 177 }
178 else {
179 $default = $attr->default;
180 # make sure to quote strings ...
181 unless (looks_like_number($default)) {
182 $default = "'$default'";
183 }
184 }
8d2d4c67 185 } elsif( $attr->has_builder ) {
186 $default = '$instance->'.$attr->builder;
d90b42a6 187 }
8d2d4c67 188
2f2329a4 189 if ( defined(my $init_arg = $attr->init_arg) ) {
c16c9c1b 190 return (
2f2329a4 191 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" .
5a49fb79 192 $self->_meta_instance->inline_set_slot_value(
c16c9c1b 193 '$instance',
e9a19694 194 $attr->name,
2f2329a4 195 '$params->{\'' . $init_arg . '\'}' ) . "\n" .
c16c9c1b 196 '} ' . (!defined $default ? '' : 'else {' . "\n" .
5a49fb79 197 $self->_meta_instance->inline_set_slot_value(
c16c9c1b 198 '$instance',
e9a19694 199 $attr->name,
c16c9c1b 200 $default ) . "\n" .
201 '}')
202 );
203 } elsif ( defined $default ) {
204 return (
5a49fb79 205 $self->_meta_instance->inline_set_slot_value(
c16c9c1b 206 '$instance',
e9a19694 207 $attr->name,
c16c9c1b 208 $default ) . "\n"
209 );
210 } else { return '' }
d90b42a6 211}
212
2131;
214
d90b42a6 215__END__
216
217=pod
218
8d2d4c67 219=head1 NAME
d90b42a6 220
221Class::MOP::Method::Constructor - Method Meta Object for constructors
222
223=head1 SYNOPSIS
224
96e38ba6 225 use Class::MOP::Method::Constructor;
8d2d4c67 226
96e38ba6 227 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 228 metaclass => $metaclass,
96e38ba6 229 options => {
230 debug => 1, # this is all for now
8d2d4c67 231 },
96e38ba6 232 );
8d2d4c67 233
96e38ba6 234 # calling the constructor ...
b7045e66 235 $constructor->body->execute($metaclass->name, %params);
8d2d4c67 236
d90b42a6 237=head1 DESCRIPTION
238
3fd960d9 239This is a subclass of C<Class::MOP::Method> which generates
240constructor methods.
96e38ba6 241
d90b42a6 242=head1 METHODS
243
244=over 4
245
3fd960d9 246=item B<< Class::MOP::Method::Constructor->new(%options) >>
d90b42a6 247
3fd960d9 248This creates a new constructor object. It accepts a hash reference of
249options.
96e38ba6 250
3fd960d9 251=over 8
96e38ba6 252
3fd960d9 253=item * metaclass
96e38ba6 254
3fd960d9 255This should be a L<Class::MOP::Class> object. It is required.
c23184fc 256
3fd960d9 257=item * name
d90b42a6 258
3fd960d9 259The method name (without a package name). This is required.
96e38ba6 260
3fd960d9 261=item * package_name
d90b42a6 262
3fd960d9 263The package name for the method. This is required.
c23184fc 264
3fd960d9 265=item * is_inline
96e38ba6 266
3fd960d9 267This indicates whether or not the constructor should be inlined. This
268defaults to false.
d90b42a6 269
3fd960d9 270=back
f0de47d9 271
3fd960d9 272=item B<< $metamethod->is_inline >>
f0de47d9 273
3fd960d9 274Returns a boolean indicating whether or not the constructor is
275inlined.
d90b42a6 276
3fd960d9 277=item B<< $metamethod->associated_metaclass >>
96e38ba6 278
3fd960d9 279This returns the L<Class::MOP::Class> object for the method.
d90b42a6 280
565f0cbb 281=back
282
d90b42a6 283=head1 AUTHORS
284
285Stevan Little E<lt>stevan@iinteractive.comE<gt>
286
287=head1 COPYRIGHT AND LICENSE
288
070bb6c9 289Copyright 2006-2009 by Infinity Interactive, Inc.
d90b42a6 290
291L<http://www.iinteractive.com>
292
293This library is free software; you can redistribute it and/or modify
8d2d4c67 294it under the same terms as Perl itself.
d90b42a6 295
296=cut
297