summaryrefslogtreecommitdiff
path: root/lib/Moose/Meta/Method/Accessor/Native/Hash
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Moose/Meta/Method/Accessor/Native/Hash')
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm31
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm61
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm28
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm22
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm31
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm40
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm23
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm31
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm35
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm22
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm22
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm23
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm103
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/shallow_clone.pm26
-rw-r--r--lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm22
15 files changed, 520 insertions, 0 deletions
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm
new file mode 100644
index 0000000..ccc3e1f
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/Writer.pm
@@ -0,0 +1,31 @@
+package Moose::Meta::Method::Accessor::Native::Hash::Writer;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Class::MOP::MiniTrait;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Writer',
+ 'Moose::Meta::Method::Accessor::Native::Hash',
+ 'Moose::Meta::Method::Accessor::Native::Collection';
+
+sub _inline_coerce_new_values {
+ my $self = shift;
+ $self->Moose::Meta::Method::Accessor::Native::Collection::_inline_coerce_new_values(@_);
+}
+
+sub _new_values { '@values' }
+
+sub _copy_old_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return '{ %{ (' . $slot_access . ') } }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm
new file mode 100644
index 0000000..f4f978e
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/accessor.pm
@@ -0,0 +1,61 @@
+package Moose::Meta::Method::Accessor::Native::Hash::accessor;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Hash::set',
+ 'Moose::Meta::Method::Accessor::Native::Hash::get';
+
+sub _inline_process_arguments {
+ my $self = shift;
+ $self->Moose::Meta::Method::Accessor::Native::Hash::set::_inline_process_arguments(@_);
+}
+
+sub _inline_check_argument_count {
+ my $self = shift;
+ $self->Moose::Meta::Method::Accessor::Native::Hash::set::_inline_check_argument_count(@_);
+}
+
+sub _inline_check_arguments {
+ my $self = shift;
+ $self->Moose::Meta::Method::Accessor::Native::Hash::set::_inline_check_arguments(@_);
+}
+
+sub _return_value {
+ my $self = shift;
+ $self->Moose::Meta::Method::Accessor::Native::Hash::set::_return_value(@_);
+}
+
+sub _generate_method {
+ my $self = shift;
+
+ my $inv = '$self';
+ my $slot_access = $self->_get_value($inv);
+
+ return (
+ 'sub {',
+ 'my ' . $inv . ' = shift;',
+ $self->_inline_curried_arguments,
+ $self->_inline_check_lazy($inv, '$type_constraint', '$type_coercion', '$type_message'),
+ # get
+ 'if (@_ == 1) {',
+ $self->_inline_check_var_is_valid_key('$_[0]'),
+ $slot_access . '->{$_[0]}',
+ '}',
+ # set
+ 'else {',
+ $self->_inline_writer_core($inv, $slot_access),
+ '}',
+ '}',
+ );
+}
+
+sub _minimum_arguments { 1 }
+sub _maximum_arguments { 2 }
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm
new file mode 100644
index 0000000..751a443
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/clear.pm
@@ -0,0 +1,28 @@
+package Moose::Meta::Method::Accessor::Native::Hash::clear;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Hash::Writer';
+
+sub _maximum_arguments { 0 }
+
+sub _adds_members { 0 }
+
+sub _potential_value { '{}' }
+
+sub _inline_optimized_set_new_value {
+ my $self = shift;
+ my ($inv, $new, $slot_access) = @_;
+
+ return $slot_access . ' = {};';
+}
+
+sub _return_value { '' }
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm
new file mode 100644
index 0000000..aca9116
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/count.pm
@@ -0,0 +1,22 @@
+package Moose::Meta::Method::Accessor::Native::Hash::count;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader';
+
+sub _maximum_arguments { 0 }
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'scalar keys %{ (' . $slot_access . ') }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm
new file mode 100644
index 0000000..0062918
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/defined.pm
@@ -0,0 +1,31 @@
+package Moose::Meta::Method::Accessor::Native::Hash::defined;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader',
+ 'Moose::Meta::Method::Accessor::Native::Hash';
+
+sub _minimum_arguments { 1 }
+
+sub _maximum_arguments { 1 }
+
+sub _inline_check_arguments {
+ my $self = shift;
+
+ return $self->_inline_check_var_is_valid_key('$_[0]');
+}
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'defined ' . $slot_access . '->{ $_[0] }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm
new file mode 100644
index 0000000..1a6d706
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/delete.pm
@@ -0,0 +1,40 @@
+package Moose::Meta::Method::Accessor::Native::Hash::delete;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Hash::Writer';
+
+sub _adds_members { 0 }
+
+sub _potential_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return '(do { '
+ . 'my %potential = %{ (' . $slot_access . ') }; '
+ . '@return = delete @potential{@_}; '
+ . '\%potential; '
+ . '})';
+}
+
+sub _inline_optimized_set_new_value {
+ my $self = shift;
+ my ($inv, $new, $slot_access) = @_;
+
+ return '@return = delete @{ (' . $slot_access . ') }{@_};';
+}
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'wantarray ? @return : $return[-1]';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm
new file mode 100644
index 0000000..d1ba09d
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/elements.pm
@@ -0,0 +1,23 @@
+package Moose::Meta::Method::Accessor::Native::Hash::elements;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader';
+
+sub _maximum_arguments { 0 }
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'map { $_, ' . $slot_access . '->{$_} } '
+ . 'keys %{ (' . $slot_access . ') }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm
new file mode 100644
index 0000000..7ab09cc
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/exists.pm
@@ -0,0 +1,31 @@
+package Moose::Meta::Method::Accessor::Native::Hash::exists;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader',
+ 'Moose::Meta::Method::Accessor::Native::Hash';
+
+sub _minimum_arguments { 1 }
+
+sub _maximum_arguments { 1 }
+
+sub _inline_check_arguments {
+ my $self = shift;
+
+ return $self->_inline_check_var_is_valid_key('$_[0]');
+}
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = shift;
+
+ return 'exists ' . $slot_access . '->{ $_[0] }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm
new file mode 100644
index 0000000..a91c8f8
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/get.pm
@@ -0,0 +1,35 @@
+package Moose::Meta::Method::Accessor::Native::Hash::get;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader',
+ 'Moose::Meta::Method::Accessor::Native::Hash';
+
+sub _minimum_arguments { 1 }
+
+sub _inline_check_arguments {
+ my $self = shift;
+
+ return (
+ 'for (@_) {',
+ $self->_inline_check_var_is_valid_key('$_'),
+ '}',
+ );
+}
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return '@_ > 1 '
+ . '? @{ (' . $slot_access . ') }{@_} '
+ . ': ' . $slot_access . '->{$_[0]}';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm
new file mode 100644
index 0000000..7948927
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/is_empty.pm
@@ -0,0 +1,22 @@
+package Moose::Meta::Method::Accessor::Native::Hash::is_empty;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader';
+
+sub _maximum_arguments { 0 }
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'scalar keys %{ (' . $slot_access . ') } ? 0 : 1';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm
new file mode 100644
index 0000000..439be94
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/keys.pm
@@ -0,0 +1,22 @@
+package Moose::Meta::Method::Accessor::Native::Hash::keys;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader';
+
+sub _maximum_arguments { 0 }
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'keys %{ (' . $slot_access . ') }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm
new file mode 100644
index 0000000..4bbb325
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/kv.pm
@@ -0,0 +1,23 @@
+package Moose::Meta::Method::Accessor::Native::Hash::kv;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader';
+
+sub _maximum_arguments { 0 }
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'map { [ $_, ' . $slot_access . '->{$_} ] } '
+ . 'keys %{ (' . $slot_access . ') }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm
new file mode 100644
index 0000000..7d7a1fa
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm
@@ -0,0 +1,103 @@
+package Moose::Meta::Method::Accessor::Native::Hash::set;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use List::MoreUtils ();
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Hash::Writer';
+
+sub _minimum_arguments { 2 }
+
+sub _maximum_arguments { undef }
+
+around _inline_check_argument_count => sub {
+ my $orig = shift;
+ my $self = shift;
+
+ return (
+ $self->$orig(@_),
+ 'if (@_ % 2) {',
+ $self->_inline_throw_exception( MustPassEvenNumberOfArguments =>
+ "method_name => '".$self->delegate_to_method."',".
+ 'args => \@_',
+ ) . ';',
+ '}',
+ );
+};
+
+sub _inline_process_arguments {
+ my $self = shift;
+
+ return (
+ 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;',
+ 'my @values_idx = grep { $_ % 2 } 0..$#_;',
+ );
+}
+
+sub _inline_check_arguments {
+ my $self = shift;
+
+ return (
+ 'for (@keys_idx) {',
+ 'if (!defined($_[$_])) {',
+ $self->_inline_throw_exception( UndefinedHashKeysPassedToMethod =>
+ 'hash_keys => \@keys_idx,'.
+ "method_name => '".$self->delegate_to_method."'",
+ ) . ';',
+ '}',
+ '}',
+ );
+}
+
+sub _adds_members { 1 }
+
+# We need to override this because while @_ can be written to, we cannot write
+# directly to $_[1].
+sub _inline_coerce_new_values {
+ my $self = shift;
+
+ return unless $self->associated_attribute->should_coerce;
+
+ return unless $self->_tc_member_type_can_coerce;
+
+ # Is there a simpler way to do this?
+ return (
+ 'my $iter = List::MoreUtils::natatime(2, @_);',
+ '@_ = ();',
+ 'while (my ($key, $val) = $iter->()) {',
+ 'push @_, $key, $member_coercion->($val);',
+ '}',
+ );
+};
+
+sub _potential_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return '{ %{ (' . $slot_access . ') }, @_ }';
+}
+
+sub _new_members { '@_[ @values_idx ]' }
+
+sub _inline_optimized_set_new_value {
+ my $self = shift;
+ my ($inv, $new, $slot_access) = @_;
+
+ return '@{ (' . $slot_access . ') }{ @_[@keys_idx] } = @_[@values_idx];';
+}
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'wantarray '
+ . '? @{ (' . $slot_access . ') }{ @_[@keys_idx] } '
+ . ': ' . $slot_access . '->{ $_[$keys_idx[0]] }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/shallow_clone.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/shallow_clone.pm
new file mode 100644
index 0000000..62b09cb
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/shallow_clone.pm
@@ -0,0 +1,26 @@
+package Moose::Meta::Method::Accessor::Native::Hash::shallow_clone;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Params::Util ();
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader';
+
+sub _minimum_arguments { 0 }
+
+sub _maximum_arguments { 0 }
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return '{ %{ (' . $slot_access . ') } }';
+}
+
+no Moose::Role;
+
+1;
diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm
new file mode 100644
index 0000000..750ce76
--- /dev/null
+++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/values.pm
@@ -0,0 +1,22 @@
+package Moose::Meta::Method::Accessor::Native::Hash::values;
+our $VERSION = '2.1405';
+
+use strict;
+use warnings;
+
+use Moose::Role;
+
+with 'Moose::Meta::Method::Accessor::Native::Reader';
+
+sub _maximum_arguments { 0 }
+
+sub _return_value {
+ my $self = shift;
+ my ($slot_access) = @_;
+
+ return 'values %{ (' . $slot_access . ') }';
+}
+
+no Moose::Role;
+
+1;