working without XS
[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
0c6f4c4a 10our $VERSION = '0.04';
d90b42a6 11our $AUTHORITY = 'cpan:STEVAN';
12
565f0cbb 13use base 'Class::MOP::Method::Generated';
d90b42a6 14
15sub new {
16 my $class = shift;
17 my %options = @_;
8d2d4c67 18
ad315b75 19 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
20 || confess "You must pass a metaclass instance if you want to inline"
8d2d4c67 21 if $options{is_inline};
22
d90b42a6 23 my $self = bless {
24 # from our superclass
4c105333 25 '&!body' => undef,
26 '$!package_name' => $options{package_name},
27 '$!name' => $options{name},
d90b42a6 28 # specific to this subclass
ad315b75 29 '%!options' => $options{options} || {},
c23184fc 30 '$!associated_metaclass' => $options{metaclass},
8d2d4c67 31 '$!is_inline' => ($options{is_inline} || 0),
d90b42a6 32 } => $class;
33
8d2d4c67 34 # we don't want this creating
35 # a cycle in the code, if not
d90b42a6 36 # needed
8d2d4c67 37 weaken($self->{'$!associated_metaclass'});
d90b42a6 38
565f0cbb 39 $self->initialize_body;
d90b42a6 40
8d2d4c67 41 return $self;
d90b42a6 42}
43
8d2d4c67 44## accessors
c23184fc 45
565f0cbb 46sub options { (shift)->{'%!options'} }
47sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
c23184fc 48
565f0cbb 49## cached values ...
d90b42a6 50
8d2d4c67 51sub meta_instance {
565f0cbb 52 my $self = shift;
53 $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
54}
c23184fc 55
8d2d4c67 56sub attributes {
565f0cbb 57 my $self = shift;
58 $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ]
59}
d90b42a6 60
61## method
62
565f0cbb 63sub initialize_body {
64 my $self = shift;
65 my $method_name = 'generate_constructor_method';
8d2d4c67 66
565f0cbb 67 $method_name .= '_inline' if $self->is_inline;
8d2d4c67 68
565f0cbb 69 $self->{'&!body'} = $self->$method_name;
70}
71
72sub generate_constructor_method {
2a2b8458 73 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
565f0cbb 74}
75
76sub generate_constructor_method_inline {
d90b42a6 77 my $self = shift;
565f0cbb 78
d90b42a6 79 my $source = 'sub {';
80 $source .= "\n" . 'my ($class, %params) = @_;';
8d2d4c67 81
2a2b8458 82 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(%params)';
8d2d4c67 83 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
84
d90b42a6 85 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
8d2d4c67 86 $source .= ";\n" . (join ";\n" => map {
87 $self->_generate_slot_initializer($_)
d90b42a6 88 } 0 .. (@{$self->attributes} - 1));
89 $source .= ";\n" . 'return $instance';
8d2d4c67 90 $source .= ";\n" . '}';
91 warn $source if $self->options->{debug};
92
d90b42a6 93 my $code;
94 {
95 # NOTE:
96 # create the nessecary lexicals
8d2d4c67 97 # to be picked up in the eval
d90b42a6 98 my $attrs = $self->attributes;
8d2d4c67 99
d90b42a6 100 $code = eval $source;
101 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
102 }
565f0cbb 103 return $code;
d90b42a6 104}
105
106sub _generate_slot_initializer {
107 my $self = shift;
108 my $index = shift;
8d2d4c67 109
d90b42a6 110 my $attr = $self->attributes->[$index];
8d2d4c67 111
d90b42a6 112 my $default;
113 if ($attr->has_default) {
114 # NOTE:
115 # default values can either be CODE refs
8d2d4c67 116 # in which case we need to call them. Or
d90b42a6 117 # they can be scalars (strings/numbers)
118 # in which case we can just deal with them
119 # in the code we eval.
120 if ($attr->is_default_a_coderef) {
121 $default = '$attrs->[' . $index . ']->default($instance)';
122 }
123 else {
124 $default = $attr->default;
125 # make sure to quote strings ...
126 unless (looks_like_number($default)) {
127 $default = "'$default'";
128 }
129 }
8d2d4c67 130 } elsif( $attr->has_builder ) {
131 $default = '$instance->'.$attr->builder;
d90b42a6 132 }
8d2d4c67 133
c16c9c1b 134 if ( defined $attr->init_arg ) {
135 return (
136 'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" .
137 $self->meta_instance->inline_set_slot_value(
138 '$instance',
139 ("'" . $attr->name . "'"),
140 '$params{\'' . $attr->init_arg . '\'}' ) . "\n" .
141 '} ' . (!defined $default ? '' : 'else {' . "\n" .
142 $self->meta_instance->inline_set_slot_value(
143 '$instance',
144 ("'" . $attr->name . "'"),
145 $default ) . "\n" .
146 '}')
147 );
148 } elsif ( defined $default ) {
149 return (
150 $self->meta_instance->inline_set_slot_value(
151 '$instance',
152 ("'" . $attr->name . "'"),
153 $default ) . "\n"
154 );
155 } else { return '' }
d90b42a6 156}
157
1581;
159
1601;
161
162__END__
163
164=pod
165
8d2d4c67 166=head1 NAME
d90b42a6 167
168Class::MOP::Method::Constructor - Method Meta Object for constructors
169
170=head1 SYNOPSIS
171
96e38ba6 172 use Class::MOP::Method::Constructor;
8d2d4c67 173
96e38ba6 174 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 175 metaclass => $metaclass,
96e38ba6 176 options => {
177 debug => 1, # this is all for now
8d2d4c67 178 },
96e38ba6 179 );
8d2d4c67 180
96e38ba6 181 # calling the constructor ...
182 $constructor->body->($metaclass->name, %params);
8d2d4c67 183
d90b42a6 184=head1 DESCRIPTION
185
8d2d4c67 186This is a subclass of C<Class::MOP::Method> which deals with
127d39a7 187class constructors. This is used when making a class immutable
188to generate an optimized constructor.
96e38ba6 189
d90b42a6 190=head1 METHODS
191
192=over 4
193
96e38ba6 194=item B<new (metaclass => $meta, options => \%options)>
d90b42a6 195
96e38ba6 196=item B<options>
197
198This returns the options HASH which is passed into C<new>.
199
200=item B<associated_metaclass>
201
202This returns the metaclass which is passed into C<new>.
c23184fc 203
d90b42a6 204=item B<attributes>
205
8d2d4c67 206This returns the list of attributes which are associated with the
96e38ba6 207metaclass which is passed into C<new>.
208
d90b42a6 209=item B<meta_instance>
210
8d2d4c67 211This returns the meta instance which is associated with the
96e38ba6 212metaclass which is passed into C<new>.
c23184fc 213
96e38ba6 214=item B<is_inline>
215
8d2d4c67 216This returns a boolean, but since constructors are very rarely
96e38ba6 217not inlined, this always returns true for now.
d90b42a6 218
565f0cbb 219=item B<initialize_body>
d90b42a6 220
8d2d4c67 221This creates the code reference for the constructor itself.
96e38ba6 222
d90b42a6 223=back
224
127d39a7 225=head2 Method Generators
565f0cbb 226
227=over 4
228
229=item B<generate_constructor_method>
230
231=item B<generate_constructor_method_inline>
232
233=back
234
d90b42a6 235=head1 AUTHORS
236
237Stevan Little E<lt>stevan@iinteractive.comE<gt>
238
239=head1 COPYRIGHT AND LICENSE
240
69e3ab0a 241Copyright 2006-2008 by Infinity Interactive, Inc.
d90b42a6 242
243L<http://www.iinteractive.com>
244
245This library is free software; you can redistribute it and/or modify
8d2d4c67 246it under the same terms as Perl itself.
d90b42a6 247
248=cut
249