1 package MooseX::InsideOut::Role::Meta::Instance;
5 use Hash::Util::FieldHash::Compat qw(fieldhash);
6 use Scalar::Util qw(refaddr weaken);
7 use namespace::clean -except => 'meta';
11 around create_instance => sub {
13 my $instance = shift->$next(@_);
14 $attr{refaddr $instance} = {};
19 my ($self, $instance, $slot_name) = @_;
21 return $attr{refaddr $instance}->{$slot_name};
25 my ($self, $instance, $slot_name, $value) = @_;
27 return $attr{refaddr $instance}->{$slot_name} = $value;
30 sub deinitialize_slot {
31 my ($self, $instance, $slot_name) = @_;
32 return delete $attr{refaddr $instance}->{$slot_name};
35 sub deinitialize_all_slots {
36 my ($self, $instance) = @_;
37 $attr{refaddr $instance} = {};
40 sub is_slot_initialized {
41 my ($self, $instance, $slot_name) = @_;
43 return exists $attr{refaddr $instance}->{$slot_name};
46 sub weaken_slot_value {
47 my ($self, $instance, $slot_name) = @_;
48 weaken $attr{refaddr $instance}->{$slot_name};
51 around inline_create_instance => sub {
53 my ($self, $class_variable) = @_;
54 my $code = $self->$next($class_variable);
55 $code = "do { {my \$instance = ($code);";
57 '$%s::attr{Scalar::Util::refaddr($instance)} = {};',
60 $code .= '$instance }';
64 sub inline_slot_access {
65 my ($self, $instance, $slot_name) = @_;
66 return sprintf '$%s::attr{Scalar::Util::refaddr(%s)}->{%s}',
67 __PACKAGE__, $instance, $slot_name;
76 Meta-instance role implementing inside-out storage.
78 =method create_instance
80 =method get_slot_value
82 =method set_slot_value
84 =method deinitialize_slot
86 =method deinitialize_all_slots
88 =method is_slot_initialized
90 =method weaken_slot_value
92 =method inline_create_instance
94 =method inline_slot_access
96 See L<Class::MOP::Instance>.