From: Yuval Kogman Date: Sun, 30 Apr 2006 16:49:45 +0000 (+0000) Subject: Add the Inlinable class, salvaged from my blib X-Git-Tag: 0_29_02~25 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=46c9857c04564f42760d725fbd4627ed1b5a3a58;p=gitmo%2FClass-MOP.git Add the Inlinable class, salvaged from my blib --- diff --git a/lib/Class/MOP/Instance/Inlinable.pm b/lib/Class/MOP/Instance/Inlinable.pm new file mode 100644 index 0000000..ac0729e --- /dev/null +++ b/lib/Class/MOP/Instance/Inlinable.pm @@ -0,0 +1,88 @@ +#!/usr/bin/perl + +package Class::MOP::Instance::Inlinable; + +use strict; +use warnings; + +# cheap ass pseudo-mixin + +# inlinable operation snippets + +sub inline_get_slot_value { + my ($self, $instance, $slot_name) = @_; + sprintf "%s->{%s}", $instance, $slot_name; +} + +sub inline_set_slot_value { + my ($self, $instance, $slot_name, $value) = @_; + $self->_inline_slot_lvalue( $instance, $slot_name ) . " = $value", +} + +sub inline_set_slot_value_with_init { + my ($self, $instance, $slot_name, $value) = @_; + + $self->_join_statements( + $self->inline_initialize_slot( $instance, $slot_name ), + $self->inline_set_slot_value( $instance, $slot_name, $value ), + ); +} + +sub inline_set_slot_value_weak { + my ($self, $instance, $slot_name, $value) = @_; + + $self->_join_statements( + $self->inline_set_slot_value( $instance, $slot_name, $value ), + $self->inline_weaken_slot_value( $instance, $slot_name ), + ); +} + +sub inline_weaken_slot_value { + my ($self, $instance, $slot_name) = @_; + sprintf "Scalar::Util::weaken( %s )", $self->_inline_slot_lvalue( $instance, $slot_name ); +} + +sub inline_initialize_slot { + return ""; +} + +sub inline_slot_initialized { + my ($self, $instance, $slot_name) = @_; + "exists " . $self->inline_get_slot_value; +} + +sub _join_statements { + my ( $self, @statements ) = @_; + my @filtered = grep { length } @statements; + return $filtered[0] if @filtered == 1; + return join("; ", @filtered); +} + +sub _inline_slot_lvalue { + my ($self, $instance, $slot_name) = @_; + $self->inline_get_slot_value( $instance, $slot_name ); +} + +__PACKAGE__; + +__END__ + +=pod + +=head1 NAME + +Class::MOP::Instance::Inlinable - Generate inline slot operations. + +=head1 SYNOPSIS + + # see Moose::Meta::Attribute for an example + +=head1 DESCRIPTION + +This pseudo-mixin class provides additional methods to work along side +L, which can be used to generate accessors with inlined +slot operations. + +=cut + +