Commit | Line | Data |
46c9857c |
1 | #!/usr/bin/perl |
2 | |
3 | package Class::MOP::Instance::Inlinable; |
4 | |
5 | use strict; |
6 | use warnings; |
7 | |
8 | # cheap ass pseudo-mixin |
9 | |
10 | # inlinable operation snippets |
11 | |
12 | sub inline_get_slot_value { |
13 | my ($self, $instance, $slot_name) = @_; |
14 | sprintf "%s->{%s}", $instance, $slot_name; |
15 | } |
16 | |
17 | sub inline_set_slot_value { |
18 | my ($self, $instance, $slot_name, $value) = @_; |
19 | $self->_inline_slot_lvalue( $instance, $slot_name ) . " = $value", |
20 | } |
21 | |
22 | sub 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 | |
31 | sub 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 | |
40 | sub 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 | |
45 | sub inline_initialize_slot { |
46 | return ""; |
47 | } |
48 | |
49 | sub inline_slot_initialized { |
50 | my ($self, $instance, $slot_name) = @_; |
51 | "exists " . $self->inline_get_slot_value; |
52 | } |
53 | |
54 | sub _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 | |
61 | sub _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 | |
74 | Class::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 | |
82 | This pseudo-mixin class provides additional methods to work along side |
83 | L<Class::MOP::Instance>, which can be used to generate accessors with inlined |
84 | slot operations. |
85 | |
86 | =cut |
87 | |
88 | |