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 );
51 $self->{instance_layout}->{$slot_name} = undef;
55 my ($self, $slot_name) = @_;
56 exists $self->{instance_layout}->{$slot_name} ? 1 : 0;
59 sub has_slot_recursively {
60 my ( $self, $slot_name ) = @_;
61 return 1 if $self->has_slot($slot_name);
62 $_->has_slot_recursively($slot_name) && return 1 for $self->get_all_parents;
67 my ( $self, $slot_name ) = @_;
69 # this does not search recursively cause
70 # that is not the domain of this meta-instance
71 # it is specific to this class ...
72 confess "The slot '$slot_name' does not exist (maybe it's inherited?)"
73 if 0 && $self->has_slot( $slot_name );
74 delete $self->{instance_layout}->{$slot_name};
78 # operations on created instances
81 my ($self, $instance, $slot_name) = @_;
82 return $instance->{$slot_name};
85 # can be called only after initialize_slot_value
87 my ($self, $instance, $slot_name, $value) = @_;
88 $slot_name or confess "must provide slot name";
89 $instance->{$slot_name} = $value;
93 # non autovivifying stores will have this as { initialize_slot unless slot_initlized; set_slot_value }
94 sub set_slot_value_with_init {
95 my ( $self, $instance, $slot_name, $value ) = @_;
96 $self->set_slot_value( $instance, $slot_name, $value );
100 my ( $self, $instance, $slot_name ) = @_;
103 sub slot_initialized {
104 my ($self, $instance, $slot_name) = @_;
105 exists $instance->{$slot_name} ? 1 : 0;
109 # inlinable operation snippets
111 sub inline_get_slot_value {
112 my ($self, $instance, $slot_name) = @_;
113 sprintf "%s->{%s}", $instance, $slot_name;
116 sub inline_set_slot_value {
117 my ($self, $instance, $slot_name, $value) = @_;
118 $self->_inline_slot_lvalue . " = $value",
121 sub inline_set_slot_value_with_init {
122 my ( $self, $instance, $slot_name, $value) = @_;
123 $self->inline_set_slot_value( $instance, $slot_name, $value ) . ";";
126 sub inline_initialize_slot {
130 sub inline_slot_initialized {
131 my ($self, $instance, $slot_name) = @_;
132 "exists " . $self->inline_get_slot_value;
135 sub _inline_slot_lvalue {
136 my ($self, $instance, $slot_name) = @_;
137 $self->inline_slot_value;
148 Class::MOP::Instance - Instance Meta Object
162 =item B<bless_instance_structure>
164 =item B<create_instance>
166 =item B<get_all_parents>
168 =item B<get_slot_value>
172 =item B<has_slot_recursively>
174 =item B<initialize_slot>
176 =item B<inline_get_slot_value>
178 =item B<inline_initialize_slot>
180 =item B<inline_set_slot_value>
182 =item B<inline_set_slot_value_with_init>
184 =item B<inline_slot_initialized>
188 =item B<set_slot_value>
190 =item B<set_slot_value_with_init>
192 =item B<slot_initialized>
202 This will return a B<Class::MOP::Class> instance which is related
209 Stevan Little E<lt>stevan@iinteractive.comE<gt>
211 =head1 COPYRIGHT AND LICENSE
213 Copyright 2006 by Infinity Interactive, Inc.
215 L<http://www.iinteractive.com>
217 This library is free software; you can redistribute it and/or modify
218 it under the same terms as Perl itself.