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, @attrs) = @_;
19 my @slots = map { $_->slots } @attrs;
22 # I am not sure that it makes
23 # sense to pass in the meta
24 # The ideal would be to just
25 # pass in the class name, but
26 # that is placing too much of
27 # an assumption on bless(),
28 # which is *probably* a safe
29 # assumption,.. but you can
38 $self->bless_instance_structure({});
41 sub bless_instance_structure {
42 my ($self, $instance_structure) = @_;
43 bless $instance_structure, $self->{meta}->name;
46 # operations on meta instance
50 return @{$self->{slots}};
53 # operations on created instances
55 sub initialize_all_slots {
56 my ($self, $instance) = @_;
57 foreach my $slot_name ($self->get_all_slots) {
58 $self->initialize_slot($instance, $slot_name);
63 my ($self, $instance, $slot_name) = @_;
64 return $instance->{$slot_name};
68 my ($self, $instance, $slot_name, $value) = @_;
69 $instance->{$slot_name} = $value;
73 my ($self, $instance, $slot_name) = @_;
74 $instance->{$slot_name} = undef;
77 sub is_slot_initialized {
78 my ($self, $instance, $slot_name, $value) = @_;
79 exists $instance->{$slot_name} ? 1 : 0;
82 # inlinable operation snippets
84 sub inline_get_slot_value {
85 my ($self, $instance_var_name, $slot_name) = @_;
86 return ($instance_var_name . '->{\'' . $slot_name . '\'}');
89 sub inline_set_slot_value {
90 my ($self, $instance_var_name, $slot_name, $value_name) = @_;
91 return ($self->inline_get_slot_value($instance_var_name, $slot_name) . ' = ' . $value_name);
94 sub inline_initialize_slot {
95 my ($self, $instance_var_name, $slot_name) = @_;
96 $self->inline_set_slot_value($instance_var_name, $slot_name, 'undef');
99 sub inline_is_slot_initialized {
100 my ($self, $instance_var_name, $slot_name) = @_;
101 return ('exists ' . $self->inline_get_slot_value($instance_var_name, $slot_name) . ' ? 1 : 0');
112 Class::MOP::Instance - Instance Meta Object
124 =item B<create_instance>
126 =item B<bless_instance_structure>
128 =item B<get_all_slots>
130 =item B<initialize_all_slots>
132 =item B<get_slot_value>
134 =item B<set_slot_value>
136 =item B<initialize_slot>
138 =item B<is_slot_initialized>
140 =item B<inline_get_slot_value>
142 =item B<inline_set_slot_value>
144 =item B<inline_initialize_slot>
146 =item B<inline_is_slot_initialized>
156 This will return a B<Class::MOP::Class> instance which is related
163 Stevan Little E<lt>stevan@iinteractive.comE<gt>
165 =head1 COPYRIGHT AND LICENSE
167 Copyright 2006 by Infinity Interactive, Inc.
169 L<http://www.iinteractive.com>
171 This library is free software; you can redistribute it and/or modify
172 it under the same terms as Perl itself.