Add inlining methods for Number native trait
Dave Rolsky [Wed, 22 Sep 2010 22:21:29 +0000 (17:21 -0500)]
lib/Moose/Meta/Attribute/Native/Trait/Number.pm
lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm [new file with mode: 0644]
lib/Moose/Meta/Method/Accessor/Native/Number/add.pm [new file with mode: 0644]
lib/Moose/Meta/Method/Accessor/Native/Number/div.pm [new file with mode: 0644]
lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm [new file with mode: 0644]
lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm [new file with mode: 0644]
lib/Moose/Meta/Method/Accessor/Native/Number/set.pm [new file with mode: 0644]
lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm [new file with mode: 0644]

index fa305af..20e9359 100644 (file)
@@ -5,51 +5,18 @@ our $VERSION   = '1.14';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
+use Moose::Meta::Method::Accessor::Native::Number::abs;
+use Moose::Meta::Method::Accessor::Native::Number::add;
+use Moose::Meta::Method::Accessor::Native::Number::div;
+use Moose::Meta::Method::Accessor::Native::Number::mod;
+use Moose::Meta::Method::Accessor::Native::Number::mul;
+use Moose::Meta::Method::Accessor::Native::Number::set;
+use Moose::Meta::Method::Accessor::Native::Number::sub;
+
 with 'Moose::Meta::Attribute::Native::Trait';
 
 sub _helper_type { 'Num' }
 
-# NOTE: we don't use the method provider for this module since many of
-# the names of the provided methods would conflict with keywords - SL
-
-has 'method_constructors' => (
-    is      => 'ro',
-    isa     => 'HashRef',
-    lazy    => 1,
-    default => sub {
-        return +{
-            set => sub {
-                my ( $attr, $reader, $writer ) = @_;
-                return sub { $writer->( $_[0], $_[1] ) };
-            },
-            add => sub {
-                my ( $attr, $reader, $writer ) = @_;
-                return sub { $writer->( $_[0], $reader->( $_[0] ) + $_[1] ) };
-            },
-            sub => sub {
-                my ( $attr, $reader, $writer ) = @_;
-                return sub { $writer->( $_[0], $reader->( $_[0] ) - $_[1] ) };
-            },
-            mul => sub {
-                my ( $attr, $reader, $writer ) = @_;
-                return sub { $writer->( $_[0], $reader->( $_[0] ) * $_[1] ) };
-            },
-            div => sub {
-                my ( $attr, $reader, $writer ) = @_;
-                return sub { $writer->( $_[0], $reader->( $_[0] ) / $_[1] ) };
-            },
-            mod => sub {
-                my ( $attr, $reader, $writer ) = @_;
-                return sub { $writer->( $_[0], $reader->( $_[0] ) % $_[1] ) };
-            },
-            abs => sub {
-                my ( $attr, $reader, $writer ) = @_;
-                return sub { $writer->( $_[0], abs( $reader->( $_[0] ) ) ) };
-            },
-        };
-    }
-);
-
 no Moose::Role;
 
 1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/abs.pm
new file mode 100644 (file)
index 0000000..5021ee9
--- /dev/null
@@ -0,0 +1,27 @@
+package Moose::Meta::Method::Accessor::Native::Number::abs;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.13';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
+use base 'Moose::Meta::Method::Accessor::Native::Writer';
+
+sub _minimum_arguments {0}
+sub _maximum_arguments {0}
+
+sub _potential_value {
+    my ( $self, $slot_access ) = @_;
+
+    return "abs($slot_access);";
+}
+
+sub _inline_optimized_set_new_value {
+    my ( $self, $inv, $new, $slot_access ) = @_;
+
+    return "$slot_access = abs($slot_access)";
+}
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/add.pm
new file mode 100644 (file)
index 0000000..c24052b
--- /dev/null
@@ -0,0 +1,27 @@
+package Moose::Meta::Method::Accessor::Native::Number::add;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.13';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
+use base 'Moose::Meta::Method::Accessor::Native::Writer';
+
+sub _minimum_arguments {1}
+sub _maximum_arguments {1}
+
+sub _potential_value {
+    my ( $self, $slot_access ) = @_;
+
+    return "$slot_access + \$_[0];";
+}
+
+sub _inline_optimized_set_new_value {
+    my ( $self, $inv, $new, $slot_access ) = @_;
+
+    return "$slot_access += \$_[0];";
+}
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/div.pm
new file mode 100644 (file)
index 0000000..eddc608
--- /dev/null
@@ -0,0 +1,27 @@
+package Moose::Meta::Method::Accessor::Native::Number::div;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.13';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
+use base 'Moose::Meta::Method::Accessor::Native::Writer';
+
+sub _minimum_arguments {1}
+sub _maximum_arguments {1}
+
+sub _potential_value {
+    my ( $self, $slot_access ) = @_;
+
+    return "$slot_access / \$_[0];";
+}
+
+sub _inline_optimized_set_new_value {
+    my ( $self, $inv, $new, $slot_access ) = @_;
+
+    return "$slot_access /= \$_[0];";
+}
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/mod.pm
new file mode 100644 (file)
index 0000000..7f57570
--- /dev/null
@@ -0,0 +1,27 @@
+package Moose::Meta::Method::Accessor::Native::Number::mod;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.13';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
+use base 'Moose::Meta::Method::Accessor::Native::Writer';
+
+sub _minimum_arguments {1}
+sub _maximum_arguments {1}
+
+sub _potential_value {
+    my ( $self, $slot_access ) = @_;
+
+    return "$slot_access % \$_[0];";
+}
+
+sub _inline_optimized_set_new_value {
+    my ( $self, $inv, $new, $slot_access ) = @_;
+
+    return "$slot_access %= \$_[0];";
+}
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/mul.pm
new file mode 100644 (file)
index 0000000..1ed88c8
--- /dev/null
@@ -0,0 +1,27 @@
+package Moose::Meta::Method::Accessor::Native::Number::mul;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.13';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
+use base 'Moose::Meta::Method::Accessor::Native::Writer';
+
+sub _minimum_arguments {1}
+sub _maximum_arguments {1}
+
+sub _potential_value {
+    my ( $self, $slot_access ) = @_;
+
+    return "$slot_access * \$_[0];";
+}
+
+sub _inline_optimized_set_new_value {
+    my ( $self, $inv, $new, $slot_access ) = @_;
+
+    return "$slot_access *= \$_[0];";
+}
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/set.pm
new file mode 100644 (file)
index 0000000..ca797bf
--- /dev/null
@@ -0,0 +1,23 @@
+package Moose::Meta::Method::Accessor::Native::Number::set;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.13';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
+use base 'Moose::Meta::Method::Accessor::Native::Writer';
+
+sub _minimum_arguments {1}
+sub _maximum_arguments {1}
+
+sub _potential_value {'$_[0]'}
+
+sub _inline_optimized_set_new_value {
+    my ( $self, $inv, $new, $slot_access ) = @_;
+
+    return "$slot_access = \$_[0];";
+}
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm b/lib/Moose/Meta/Method/Accessor/Native/Number/sub.pm
new file mode 100644 (file)
index 0000000..563464a
--- /dev/null
@@ -0,0 +1,27 @@
+package Moose::Meta::Method::Accessor::Native::Number::sub;
+
+use strict;
+use warnings;
+
+our $VERSION = '1.13';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
+use base 'Moose::Meta::Method::Accessor::Native::Writer';
+
+sub _minimum_arguments {1}
+sub _maximum_arguments {1}
+
+sub _potential_value {
+    my ( $self, $slot_access ) = @_;
+
+    return "$slot_access - \$_[0];";
+}
+
+sub _inline_optimized_set_new_value {
+    my ( $self, $inv, $new, $slot_access ) = @_;
+
+    return "$slot_access -= \$_[0];";
+}
+
+1;