2 package Class::MOP::Instance;
7 use Scalar::Util 'weaken';
12 require Class::MOP::Class;
13 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
17 my ($class, $meta, @attrs) = @_;
18 my @slots = map { $_->slots } @attrs;
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
37 $self->bless_instance_structure({});
40 sub bless_instance_structure {
41 my ($self, $instance_structure) = @_;
42 bless $instance_structure, $self->{meta}->name;
45 # operations on meta instance
49 return @{$self->{slots}};
52 # operations on created instances
55 my ($self, $instance, $slot_name) = @_;
56 return $instance->{$slot_name};
60 my ($self, $instance, $slot_name, $value) = @_;
61 $instance->{$slot_name} = $value;
65 my ($self, $instance, $slot_name) = @_;
66 $instance->{$slot_name} = undef;
69 sub initialize_all_slots {
70 my ($self, $instance) = @_;
71 foreach my $slot_name ($self->get_all_slots) {
72 $self->initialize_slot($instance, $slot_name);
76 sub is_slot_initialized {
77 my ($self, $instance, $slot_name, $value) = @_;
78 exists $instance->{$slot_name} ? 1 : 0;
81 sub 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);
87 sub weaken_slot_value {
88 my ($self, $instance, $slot_name) = @_;
89 weaken $instance->{$slot_name};
92 sub strengthen_slot_value {
93 my ($self, $instance, $slot_name) = @_;
94 $self->set_slot_value($instance, $slot_name, $self->get_slot_value($instance, $slot_name));
105 Class::MOP::Instance - Instance Meta Object
109 # for the most part, this protocol is internal
110 # and not for public usage, but this how one
117 use metaclass 'Class::MOP::Class' => (
118 ':instance_metaclass' => 'ArrayBasedStorage::Instance',
121 # now Foo->new produces blessed ARRAY ref based objects
125 This is a sub-protocol which governs instance creation
126 and access to the slots of the instance structure.
128 This may seem like over-abstraction, but by abstracting
129 this process into a sub-protocol we make it possible to
130 easily switch the details of how an object's instance is
131 stored with minimal impact. In most cases just subclassing
132 this class will be all you need to do (occasionally it
133 requires that you also subclass Class::MOP::Attribute if
134 you require some kind of specific attribute initializations).
140 =item B<new ($meta, @attrs)>
142 Creates a new instance meta-object and gathers all the slots from
143 the list of C<@attrs> given.
147 This will return a B<Class::MOP::Class> instance which is related
152 =head2 Creation of Instances
156 =item B<create_instance>
158 This creates the appropriate structure needed for the instance and
159 then calls C<bless_instance_structure> to bless it into the class.
161 =item B<bless_instance_structure ($instance_structure)>
163 This does just exactly what it says it does.
167 =head2 Instrospection
169 NOTE: There might be more methods added to this part of the API,
170 we will add then when we need them basically.
174 =item B<get_all_slots>
176 This will return the current list of slots based on what was
177 given to this object in C<new>.
181 =head2 Operations on Instance Structures
183 An important distinction of this sub-protocol is that the
184 instance meta-object is a different entity from the actual
185 instance it creates. For this reason, any actions on slots
186 require that the C<$instance_structure> is passed into them.
190 =item B<get_slot_value ($instance_structure, $slot_name)>
192 =item B<set_slot_value ($instance_structure, $slot_name, $value)>
194 =item B<initialize_slot ($instance_structure, $slot_name)>
196 =item B<initialize_all_slots ($instance_structure)>
198 =item B<is_slot_initialized ($instance_structure, $slot_name)>
200 =item B<set_slot_value_weak ($instance_structure, $slot_name, $ref_value)>
202 =item B<weaken_slot_value>
204 =item B<strengthen_slot_value>
210 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
212 Stevan Little E<lt>stevan@iinteractive.comE<gt>
214 =head1 COPYRIGHT AND LICENSE
216 Copyright 2006 by Infinity Interactive, Inc.
218 L<http://www.iinteractive.com>
220 This library is free software; you can redistribute it and/or modify
221 it under the same terms as Perl itself.