tweaking
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance.pm
CommitLineData
24869f62 1
2package Class::MOP::Instance;
3
4use strict;
5use warnings;
6
9fa4d0b4 7use Scalar::Util 'weaken';
24869f62 8
9our $VERSION = '0.01';
10
11sub meta {
12 require Class::MOP::Class;
13 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
14}
15
16sub new {
052c2a1a 17 my ($class, $meta, @attrs) = @_;
c57c8b10 18 my @slots = map { $_->slots } @attrs;
24869f62 19 bless {
fc3ddd1d 20 # NOTE:
21 # I am not sure that it makes
22 # sense to pass in the meta
23 # The ideal would be to just
24 # pass in the class name, but
25 # that is placing too much of
26 # an assumption on bless(),
27 # which is *probably* a safe
28 # assumption,.. but you can
29 # never tell <:)
49c93440 30 meta => $meta,
052c2a1a 31 slots => \@slots,
24869f62 32 } => $class;
33}
34
49c93440 35sub create_instance {
36 my $self = shift;
37 $self->bless_instance_structure({});
2d711cc8 38}
39
49c93440 40sub bless_instance_structure {
41 my ($self, $instance_structure) = @_;
42 bless $instance_structure, $self->{meta}->name;
2d711cc8 43}
44
45# operations on meta instance
46
eb49acde 47sub get_all_slots {
48 my $self = shift;
49c93440 49 return @{$self->{slots}};
839ea973 50}
51
2d711cc8 52# operations on created instances
53
839ea973 54sub get_slot_value {
2bab2be6 55 my ($self, $instance, $slot_name) = @_;
56 return $instance->{$slot_name};
839ea973 57}
58
2bab2be6 59sub set_slot_value {
60 my ($self, $instance, $slot_name, $value) = @_;
61 $instance->{$slot_name} = $value;
62}
63
2d711cc8 64sub initialize_slot {
49c93440 65 my ($self, $instance, $slot_name) = @_;
66 $instance->{$slot_name} = undef;
2d711cc8 67}
68
c174112e 69sub initialize_all_slots {
70 my ($self, $instance) = @_;
71 foreach my $slot_name ($self->get_all_slots) {
72 $self->initialize_slot($instance, $slot_name);
73 }
74}
75
49c93440 76sub is_slot_initialized {
77 my ($self, $instance, $slot_name, $value) = @_;
2d711cc8 78 exists $instance->{$slot_name} ? 1 : 0;
2bab2be6 79}
839ea973 80
5582521c 81sub set_slot_value_weak {
82 my ($self, $instance, $slot_name, $value) = @_;
83 $self->set_slot_value($instance, $slot_name, $value);
84 $self->weaken_slot_value($instance, $slot_name);
85}
86
87sub weaken_slot_value {
88 my ($self, $instance, $slot_name) = @_;
89 weaken $instance->{$slot_name};
90}
91
92sub strengthen_slot_value {
93 my ($self, $instance, $slot_name) = @_;
94 $self->set_slot_value($instance, $slot_name, $self->get_slot_value($instance, $slot_name));
95}
96
24869f62 971;
98
99__END__
100
101=pod
102
103=head1 NAME
104
105Class::MOP::Instance - Instance Meta Object
106
107=head1 SYNOPSIS
108
9fa4d0b4 109 # for the most part, this protocol is internal
110 # and not for public usage, but this how one
111 # might use it
112
113 package Foo;
114
115 use strict;
116 use warnings;
1becdfcc 117 use metaclass (
9fa4d0b4 118 ':instance_metaclass' => 'ArrayBasedStorage::Instance',
119 );
120
121 # now Foo->new produces blessed ARRAY ref based objects
122
24869f62 123=head1 DESCRIPTION
124
9fa4d0b4 125This is a sub-protocol which governs instance creation
126and access to the slots of the instance structure.
127
128This may seem like over-abstraction, but by abstracting
129this process into a sub-protocol we make it possible to
130easily switch the details of how an object's instance is
131stored with minimal impact. In most cases just subclassing
1becdfcc 132this class will be all you need to do (see the examples;
133F<examples/ArrayBasedStorage.pod> and
134F<examples/InsideOutClass.pod> for details).
9fa4d0b4 135
24869f62 136=head1 METHODS
137
138=over 4
139
9fa4d0b4 140=item B<new ($meta, @attrs)>
141
142Creates a new instance meta-object and gathers all the slots from
143the list of C<@attrs> given.
144
145=item B<meta>
146
147This will return a B<Class::MOP::Class> instance which is related
148to this class.
149
150=back
151
152=head2 Creation of Instances
153
154=over 4
24869f62 155
58287a97 156=item B<create_instance>
157
9fa4d0b4 158This creates the appropriate structure needed for the instance and
159then calls C<bless_instance_structure> to bless it into the class.
0e76a376 160
9fa4d0b4 161=item B<bless_instance_structure ($instance_structure)>
839ea973 162
9fa4d0b4 163This does just exactly what it says it does.
0e76a376 164
9fa4d0b4 165=back
839ea973 166
9fa4d0b4 167=head2 Instrospection
58287a97 168
9fa4d0b4 169NOTE: There might be more methods added to this part of the API,
170we will add then when we need them basically.
58287a97 171
9fa4d0b4 172=over 4
173
174=item B<get_all_slots>
175
176This will return the current list of slots based on what was
177given to this object in C<new>.
58287a97 178
24869f62 179=back
180
9fa4d0b4 181=head2 Operations on Instance Structures
182
183An important distinction of this sub-protocol is that the
184instance meta-object is a different entity from the actual
185instance it creates. For this reason, any actions on slots
186require that the C<$instance_structure> is passed into them.
24869f62 187
188=over 4
189
9fa4d0b4 190=item B<get_slot_value ($instance_structure, $slot_name)>
24869f62 191
9fa4d0b4 192=item B<set_slot_value ($instance_structure, $slot_name, $value)>
193
194=item B<initialize_slot ($instance_structure, $slot_name)>
195
196=item B<initialize_all_slots ($instance_structure)>
197
198=item B<is_slot_initialized ($instance_structure, $slot_name)>
24869f62 199
5582521c 200=item B<set_slot_value_weak ($instance_structure, $slot_name, $ref_value)>
201
202=item B<weaken_slot_value>
203
204=item B<strengthen_slot_value>
205
24869f62 206=back
207
208=head1 AUTHOR
209
9fa4d0b4 210Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
211
24869f62 212Stevan Little E<lt>stevan@iinteractive.comE<gt>
213
214=head1 COPYRIGHT AND LICENSE
215
216Copyright 2006 by Infinity Interactive, Inc.
217
218L<http://www.iinteractive.com>
219
220This library is free software; you can redistribute it and/or modify
221it under the same terms as Perl itself.
222
84ef30d1 223=cut
5582521c 224