2 package Class::MOP::Instance;
8 use Scalar::Util 'blessed', 'reftype', 'weaken';
10 our $VERSION = '0.01';
13 require Class::MOP::Class;
14 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
18 my ( $class, $meta ) = @_;
26 my ( $self, $class ) = @_;
28 # rely on autovivification
29 $self->bless_instance_structure( {}, $class );
32 sub bless_instance_structure {
33 my ( $self, $instance_structure, $class ) = @_;
34 $class ||= $self->{meta}->name;
35 bless $instance_structure, $class;
40 my @parents = $self->{meta}->class_precedence_list;
41 shift @parents; # shift off ourselves
42 return map { $_->get_meta_instance } map { $_->meta || () } @parents;
45 # operations on meta instance
48 my ($self, $slot_name ) = @_;
49 confess "The slot '$slot_name' already exists"
50 if 0 && $self->has_slot_recursively( $slot_name ); # FIXME
51 $self->{instance_layout}->{$slot_name} = undef;
56 keys %{ $self->{instance_layout} };
59 sub get_all_slots_recursively {
63 map { $_->get_all_slots } $self->get_all_parents,
68 my ($self, $slot_name) = @_;
69 exists $self->{instance_layout}->{$slot_name} ? 1 : 0;
72 sub has_slot_recursively {
73 my ( $self, $slot_name ) = @_;
74 return 1 if $self->has_slot($slot_name);
75 $_->has_slot_recursively($slot_name) && return 1 for $self->get_all_parents;
80 my ( $self, $slot_name ) = @_;
82 # this does not search recursively cause
83 # that is not the domain of this meta-instance
84 # it is specific to this class ...
85 confess "The slot '$slot_name' does not exist (maybe it's inherited?)"
86 if 0 && $self->has_slot( $slot_name ); # FIXME
87 delete $self->{instance_layout}->{$slot_name};
91 # operations on created instances
94 my ($self, $instance, $slot_name) = @_;
95 return $instance->{$slot_name};
98 # can be called only after initialize_slot_value
100 my ($self, $instance, $slot_name, $value) = @_;
101 $instance->{$slot_name} = $value;
105 # non autovivifying stores will have this as { initialize_slot unless slot_initlized; set_slot_value }
106 sub set_slot_value_with_init {
107 my ( $self, $instance, $slot_name, $value ) = @_;
108 $self->set_slot_value( $instance, $slot_name, $value );
111 sub initialize_slot {
112 my ( $self, $instance, $slot_name ) = @_;
115 sub slot_initialized {
116 my ($self, $instance, $slot_name) = @_;
117 exists $instance->{$slot_name} ? 1 : 0;
121 # inlinable operation snippets
123 sub inline_get_slot_value {
124 my ($self, $instance, $slot_name) = @_;
125 sprintf "%s->{%s}", $instance, $slot_name;
128 sub inline_set_slot_value {
129 my ($self, $instance, $slot_name, $value) = @_;
130 $self->_inline_slot_lvalue . " = $value",
133 sub inline_set_slot_value_with_init {
134 my ( $self, $instance, $slot_name, $value) = @_;
135 $self->inline_set_slot_value( $instance, $slot_name, $value ) . ";";
138 sub inline_initialize_slot {
142 sub inline_slot_initialized {
143 my ($self, $instance, $slot_name) = @_;
144 "exists " . $self->inline_get_slot_value;
147 sub _inline_slot_lvalue {
148 my ($self, $instance, $slot_name) = @_;
149 $self->inline_slot_value;
160 Class::MOP::Instance - Instance Meta Object
174 =item B<bless_instance_structure>
176 =item B<create_instance>
178 =item B<get_all_parents>
180 =item B<get_slot_value>
184 =item B<has_slot_recursively>
186 =item B<initialize_slot>
188 =item B<inline_get_slot_value>
190 =item B<inline_initialize_slot>
192 =item B<inline_set_slot_value>
194 =item B<inline_set_slot_value_with_init>
196 =item B<inline_slot_initialized>
200 =item B<set_slot_value>
202 =item B<set_slot_value_with_init>
204 =item B<slot_initialized>
206 =item B<get_all_slots>
208 =item B<get_all_slots_recursively>
218 This will return a B<Class::MOP::Class> instance which is related
225 Stevan Little E<lt>stevan@iinteractive.comE<gt>
227 =head1 COPYRIGHT AND LICENSE
229 Copyright 2006 by Infinity Interactive, Inc.
231 L<http://www.iinteractive.com>
233 This library is free software; you can redistribute it and/or modify
234 it under the same terms as Perl itself.