TODO-list
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance / Inlinable.pm
CommitLineData
46c9857c 1#!/usr/bin/perl
2
3package Class::MOP::Instance::Inlinable;
4
5use strict;
6use warnings;
7
8# cheap ass pseudo-mixin
9
10# inlinable operation snippets
11
12sub inline_get_slot_value {
13 my ($self, $instance, $slot_name) = @_;
14 sprintf "%s->{%s}", $instance, $slot_name;
15}
16
17sub inline_set_slot_value {
18 my ($self, $instance, $slot_name, $value) = @_;
19 $self->_inline_slot_lvalue( $instance, $slot_name ) . " = $value",
20}
21
22sub inline_set_slot_value_with_init {
23 my ($self, $instance, $slot_name, $value) = @_;
24
25 $self->_join_statements(
26 $self->inline_initialize_slot( $instance, $slot_name ),
27 $self->inline_set_slot_value( $instance, $slot_name, $value ),
28 );
29}
30
31sub inline_set_slot_value_weak {
32 my ($self, $instance, $slot_name, $value) = @_;
33
34 $self->_join_statements(
35 $self->inline_set_slot_value( $instance, $slot_name, $value ),
36 $self->inline_weaken_slot_value( $instance, $slot_name ),
37 );
38}
39
40sub inline_weaken_slot_value {
41 my ($self, $instance, $slot_name) = @_;
42 sprintf "Scalar::Util::weaken( %s )", $self->_inline_slot_lvalue( $instance, $slot_name );
43}
44
45sub inline_initialize_slot {
46 return "";
47}
48
49sub inline_slot_initialized {
50 my ($self, $instance, $slot_name) = @_;
51 "exists " . $self->inline_get_slot_value;
52}
53
54sub _join_statements {
55 my ( $self, @statements ) = @_;
56 my @filtered = grep { length } @statements;
57 return $filtered[0] if @filtered == 1;
58 return join("; ", @filtered);
59}
60
61sub _inline_slot_lvalue {
62 my ($self, $instance, $slot_name) = @_;
63 $self->inline_get_slot_value( $instance, $slot_name );
64}
65
66__PACKAGE__;
67
68__END__
69
70=pod
71
72=head1 NAME
73
74Class::MOP::Instance::Inlinable - Generate inline slot operations.
75
76=head1 SYNOPSIS
77
78 # see Moose::Meta::Attribute for an example
79
80=head1 DESCRIPTION
81
82This pseudo-mixin class provides additional methods to work along side
83L<Class::MOP::Instance>, which can be used to generate accessors with inlined
84slot operations.
85
86=cut
87
88