<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Class::MOP::Instance;
our $VERSION = '2.2011';

use strict;
use warnings;

use Scalar::Util 'isweak', 'weaken', 'blessed';

use parent 'Class::MOP::Object';

# make this not a valid method name, to avoid (most) attribute conflicts
my $RESERVED_MOP_SLOT = '&lt;&lt;MOP&gt;&gt;';

sub BUILDARGS {
    my ($class, @args) = @_;

    if ( @args == 1 ) {
        unshift @args, "associated_metaclass";
    } elsif ( @args &gt;= 2 &amp;&amp; blessed($args[0]) &amp;&amp; $args[0]-&gt;isa("Class::MOP::Class") ) {
        # compat mode
        my ( $meta, @attrs ) = @args;
        @args = ( associated_metaclass =&gt; $meta, attributes =&gt; \@attrs );
    }

    my %options = @args;
    # FIXME lazy_build
    $options{slots} ||= [ map { $_-&gt;slots } @{ $options{attributes} || [] } ];
    $options{slot_hash} = { map { $_ =&gt; undef } @{ $options{slots} } }; # FIXME lazy_build

    return \%options;
}

sub new {
    my $class = shift;
    my $options = $class-&gt;BUILDARGS(@_);

    # FIXME replace with a proper constructor
    my $instance = $class-&gt;_new(%$options);

    # FIXME weak_ref =&gt; 1,
    weaken($instance-&gt;{'associated_metaclass'});

    return $instance;
}

sub _new {
    my $class = shift;
    return Class::MOP::Class-&gt;initialize($class)-&gt;new_object(@_)
      if $class ne __PACKAGE__;

    my $params = @_ == 1 ? $_[0] : {@_};
    return bless {
        # NOTE:
        # I am not sure that it makes
        # sense to pass in the meta
        # The ideal would be to just
        # pass in the class name, but
        # that is placing too much of
        # an assumption on bless(),
        # which is *probably* a safe
        # assumption,.. but you can
        # never tell &lt;:)
        'associated_metaclass' =&gt; $params-&gt;{associated_metaclass},
        'attributes'           =&gt; $params-&gt;{attributes},
        'slots'                =&gt; $params-&gt;{slots},
        'slot_hash'            =&gt; $params-&gt;{slot_hash},
    } =&gt; $class;
}

sub _class_name { $_[0]-&gt;{_class_name} ||= $_[0]-&gt;associated_metaclass-&gt;name }

sub create_instance {
    my $self = shift;
    bless {}, $self-&gt;_class_name;
}

sub clone_instance {
    my ($self, $instance) = @_;

    my $clone = $self-&gt;create_instance;
    for my $attr ($self-&gt;get_all_attributes) {
        next unless $attr-&gt;has_value($instance);
        for my $slot ($attr-&gt;slots) {
            my $val = $self-&gt;get_slot_value($instance, $slot);
            $self-&gt;set_slot_value($clone, $slot, $val);
            $self-&gt;weaken_slot_value($clone, $slot)
                if $self-&gt;slot_value_is_weak($instance, $slot);
        }
    }

    $self-&gt;_set_mop_slot($clone, $self-&gt;_get_mop_slot($instance))
        if $self-&gt;_has_mop_slot($instance);

    return $clone;
}

# operations on meta instance

sub get_all_slots {
    my $self = shift;
    return @{$self-&gt;{'slots'}};
}

sub get_all_attributes {
    my $self = shift;
    return @{$self-&gt;{attributes}};
}

sub is_valid_slot {
    my ($self, $slot_name) = @_;
    exists $self-&gt;{'slot_hash'}-&gt;{$slot_name};
}

# operations on created instances

sub get_slot_value {
    my ($self, $instance, $slot_name) = @_;
    $instance-&gt;{$slot_name};
}

sub set_slot_value {
    my ($self, $instance, $slot_name, $value) = @_;
    $instance-&gt;{$slot_name} = $value;
}

sub initialize_slot {
    my ($self, $instance, $slot_name) = @_;
    return;
}

sub deinitialize_slot {
    my ( $self, $instance, $slot_name ) = @_;
    delete $instance-&gt;{$slot_name};
}

sub initialize_all_slots {
    my ($self, $instance) = @_;
    foreach my $slot_name ($self-&gt;get_all_slots) {
        $self-&gt;initialize_slot($instance, $slot_name);
    }
}

sub deinitialize_all_slots {
    my ($self, $instance) = @_;
    foreach my $slot_name ($self-&gt;get_all_slots) {
        $self-&gt;deinitialize_slot($instance, $slot_name);
    }
}

sub is_slot_initialized {
    my ($self, $instance, $slot_name, $value) = @_;
    exists $instance-&gt;{$slot_name};
}

sub weaken_slot_value {
    my ($self, $instance, $slot_name) = @_;
    weaken $instance-&gt;{$slot_name};
}

sub slot_value_is_weak {
    my ($self, $instance, $slot_name) = @_;
    isweak $instance-&gt;{$slot_name};
}

sub strengthen_slot_value {
    my ($self, $instance, $slot_name) = @_;
    $self-&gt;set_slot_value($instance, $slot_name, $self-&gt;get_slot_value($instance, $slot_name));
}

sub rebless_instance_structure {
    my ($self, $instance, $metaclass) = @_;

    # we use $_[1] here because of t/cmop/rebless_overload.t regressions
    # on 5.8.8
    bless $_[1], $metaclass-&gt;name;
}

sub is_dependent_on_superclasses {
    return; # for meta instances that require updates on inherited slot changes
}

sub _get_mop_slot {
    my ($self, $instance) = @_;
    $self-&gt;get_slot_value($instance, $RESERVED_MOP_SLOT);
}

sub _has_mop_slot {
    my ($self, $instance) = @_;
    $self-&gt;is_slot_initialized($instance, $RESERVED_MOP_SLOT);
}

sub _set_mop_slot {
    my ($self, $instance, $value) = @_;
    $self-&gt;set_slot_value($instance, $RESERVED_MOP_SLOT, $value);
}

sub _clear_mop_slot {
    my ($self, $instance) = @_;
    $self-&gt;deinitialize_slot($instance, $RESERVED_MOP_SLOT);
}

# inlinable operation snippets

sub is_inlinable { 1 }

sub inline_create_instance {
    my ($self, $class_variable) = @_;
    'bless {} =&gt; ' . $class_variable;
}

sub inline_slot_access {
    my ($self, $instance, $slot_name) = @_;
    sprintf q[%s-&gt;{"%s"}], $instance, quotemeta($slot_name);
}

sub inline_get_is_lvalue { 1 }

sub inline_get_slot_value {
    my ($self, $instance, $slot_name) = @_;
    $self-&gt;inline_slot_access($instance, $slot_name);
}

sub inline_set_slot_value {
    my ($self, $instance, $slot_name, $value) = @_;
    $self-&gt;inline_slot_access($instance, $slot_name) . " = $value",
}

sub inline_initialize_slot {
    my ($self, $instance, $slot_name) = @_;
    return '';
}

sub inline_deinitialize_slot {
    my ($self, $instance, $slot_name) = @_;
    "delete " . $self-&gt;inline_slot_access($instance, $slot_name);
}
sub inline_is_slot_initialized {
    my ($self, $instance, $slot_name) = @_;
    "exists " . $self-&gt;inline_slot_access($instance, $slot_name);
}

sub inline_weaken_slot_value {
    my ($self, $instance, $slot_name) = @_;
    sprintf "Scalar::Util::weaken( %s )", $self-&gt;inline_slot_access($instance, $slot_name);
}

sub inline_strengthen_slot_value {
    my ($self, $instance, $slot_name) = @_;
    $self-&gt;inline_set_slot_value($instance, $slot_name, $self-&gt;inline_slot_access($instance, $slot_name));
}

sub inline_rebless_instance_structure {
    my ($self, $instance, $class_variable) = @_;
    "bless $instance =&gt; $class_variable";
}

sub _inline_get_mop_slot {
    my ($self, $instance) = @_;
    $self-&gt;inline_get_slot_value($instance, $RESERVED_MOP_SLOT);
}

sub _inline_set_mop_slot {
    my ($self, $instance, $value) = @_;
    $self-&gt;inline_set_slot_value($instance, $RESERVED_MOP_SLOT, $value);
}

sub _inline_clear_mop_slot {
    my ($self, $instance) = @_;
    $self-&gt;inline_deinitialize_slot($instance, $RESERVED_MOP_SLOT);
}

1;

# ABSTRACT: Instance Meta Object

__END__

=pod

=encoding UTF-8

=head1 NAME

Class::MOP::Instance - Instance Meta Object

=head1 VERSION

version 2.2011

=head1 DESCRIPTION

The Instance Protocol controls the creation of object instances, and
the storage of attribute values in those instances.

Using this API directly in your own code violates encapsulation, and
we recommend that you use the appropriate APIs in L&lt;Class::MOP::Class&gt;
and L&lt;Class::MOP::Attribute&gt; instead. Those APIs in turn call the
methods in this class as appropriate.

This class also participates in generating inlined code by providing
snippets of code to access an object instance.

=head1 METHODS

=head2 Object construction

=over 4

=item B&lt;&lt; Class::MOP::Instance-&gt;new(%options) &gt;&gt;

This method creates a new meta-instance object.

It accepts the following keys in C&lt;%options&gt;:

=over 8

=item * associated_metaclass

The L&lt;Class::MOP::Class&gt; object for which instances will be created.

=item * attributes

An array reference of L&lt;Class::MOP::Attribute&gt; objects. These are the
attributes which can be stored in each instance.

=back

=back

=head2 Creating and altering instances

=over 4

=item B&lt;&lt; $metainstance-&gt;create_instance &gt;&gt;

This method returns a reference blessed into the associated
metaclass's class.

The default is to use a hash reference. Subclasses can override this.

=item B&lt;&lt; $metainstance-&gt;clone_instance($instance) &gt;&gt;

Given an instance, this method creates a new object by making
I&lt;shallow&gt; clone of the original.

=back

=head2 Introspection

=over 4

=item B&lt;&lt; $metainstance-&gt;associated_metaclass &gt;&gt;

This returns the L&lt;Class::MOP::Class&gt; object associated with the
meta-instance object.

=item B&lt;&lt; $metainstance-&gt;get_all_slots &gt;&gt;

This returns a list of slot names stored in object instances. In
almost all cases, slot names correspond directly attribute names.

=item B&lt;&lt; $metainstance-&gt;is_valid_slot($slot_name) &gt;&gt;

This will return true if C&lt;$slot_name&gt; is a valid slot name.

=item B&lt;&lt; $metainstance-&gt;get_all_attributes &gt;&gt;

This returns a list of attributes corresponding to the attributes
passed to the constructor.

=back

=head2 Operations on Instance Structures

It's important to understand that the meta-instance object is a
different entity from the actual instances it creates. For this
reason, any operations on the C&lt;$instance_structure&gt; always require
that the object instance be passed to the method.

=over 4

=item B&lt;&lt; $metainstance-&gt;get_slot_value($instance_structure, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;set_slot_value($instance_structure, $slot_name, $value) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;initialize_slot($instance_structure, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;deinitialize_slot($instance_structure, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;initialize_all_slots($instance_structure) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;deinitialize_all_slots($instance_structure) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;is_slot_initialized($instance_structure, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;weaken_slot_value($instance_structure, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;slot_value_is_weak($instance_structure, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;strengthen_slot_value($instance_structure, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;rebless_instance_structure($instance_structure, $new_metaclass) &gt;&gt;

The exact details of what each method does should be fairly obvious
from the method name.

=back

=head2 Inlinable Instance Operations

=over 4

=item B&lt;&lt; $metainstance-&gt;is_inlinable &gt;&gt;

This is a boolean that indicates whether or not slot access operations
can be inlined. By default it is true, but subclasses can override
this.

=item B&lt;&lt; $metainstance-&gt;inline_create_instance($class_variable) &gt;&gt;

This method expects a string that, I&lt;when inlined&gt;, will become a
class name. This would literally be something like C&lt;'$class'&gt;, not an
actual class name.

It returns a snippet of code that creates a new object for the
class. This is something like C&lt; bless {}, $class_name &gt;.

=item B&lt;&lt; $metainstance-&gt;inline_get_is_lvalue &gt;&gt;

Returns whether or not C&lt;inline_get_slot_value&gt; is a valid lvalue. This can be
used to do extra optimizations when generating inlined methods.

=item B&lt;&lt; $metainstance-&gt;inline_slot_access($instance_variable, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;inline_get_slot_value($instance_variable, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;inline_set_slot_value($instance_variable, $slot_name, $value) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;inline_initialize_slot($instance_variable, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;inline_deinitialize_slot($instance_variable, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;inline_is_slot_initialized($instance_variable, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;inline_weaken_slot_value($instance_variable, $slot_name) &gt;&gt;

=item B&lt;&lt; $metainstance-&gt;inline_strengthen_slot_value($instance_variable, $slot_name) &gt;&gt;

These methods all expect two arguments. The first is the name of a
variable, than when inlined, will represent the object
instance. Typically this will be a literal string like C&lt;'$_[0]'&gt;.

The second argument is a slot name.

The method returns a snippet of code that, when inlined, performs some
operation on the instance.

=item B&lt;&lt; $metainstance-&gt;inline_rebless_instance_structure($instance_variable, $class_variable) &gt;&gt;

This takes the name of a variable that will, when inlined, represent the object
instance, and the name of a variable that will represent the class to rebless
into, and returns code to rebless an instance into a class.

=back

=head2 Introspection

=over 4

=item B&lt;&lt; Class::MOP::Instance-&gt;meta &gt;&gt;

This will return a L&lt;Class::MOP::Class&gt; instance for this class.

It should also be noted that L&lt;Class::MOP&gt; will actually bootstrap
this module by installing a number of attribute meta-objects into its
metaclass.

=back

=head1 AUTHORS

=over 4

=item *

Stevan Little &lt;stevan.little@iinteractive.com&gt;

=item *

Dave Rolsky &lt;autarch@urth.org&gt;

=item *

Jesse Luehrs &lt;doy@tozt.net&gt;

=item *

Shawn M Moore &lt;code@sartak.org&gt;

=item *

×™×•×‘×œ ×§×•×’'×ž×Ÿ (Yuval Kogman) &lt;nothingmuch@woobling.org&gt;

=item *

Karen Etheridge &lt;ether@cpan.org&gt;

=item *

Florian Ragwitz &lt;rafl@debian.org&gt;

=item *

Hans Dieter Pearcey &lt;hdp@weftsoar.net&gt;

=item *

Chris Prather &lt;chris@prather.org&gt;

=item *

Matt S Trout &lt;mst@shadowcat.co.uk&gt;

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2006 by Infinity Interactive, Inc.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
</pre></body></html>