tidy benchmark code
[gitmo/Class-MOP.git] / lib / Class / MOP / Class / Immutable / Trait.pm
CommitLineData
f5d08022 1package Class::MOP::Class::Immutable::Trait;
2
3use strict;
4use warnings;
5
6use MRO::Compat;
7
6446bd59 8use Carp 'confess';
f5d08022 9use Scalar::Util 'blessed', 'weaken';
10
d499b013 11our $VERSION = '0.92_01';
f04900bf 12$VERSION = eval $VERSION;
13our $AUTHORITY = 'cpan:STEVAN';
14
f5d08022 15# the original class of the metaclass instance
34233474 16sub _get_mutable_metaclass_name { $_[0]{__immutable}{original_class} }
f5d08022 17
78f6e9c6 18sub is_mutable { 0 }
19sub is_immutable { 1 }
f5d08022 20
a986e165 21sub _immutable_metaclass { ref $_[1] }
22
f5d08022 23sub superclasses {
78f6e9c6 24 my $orig = shift;
25 my $self = shift;
26 confess "This method is read-only" if @_;
27 $self->$orig;
f5d08022 28}
29
6446bd59 30sub _immutable_cannot_call {
31 Carp::confess "This method cannot be called on an immutable instance";
32}
f5d08022 33
78f6e9c6 34sub add_method { _immutable_cannot_call() }
35sub alias_method { _immutable_cannot_call() }
36sub remove_method { _immutable_cannot_call() }
37sub add_attribute { _immutable_cannot_call() }
38sub remove_attribute { _immutable_cannot_call() }
39sub remove_package_symbol { _immutable_cannot_call() }
f5d08022 40
6446bd59 41sub class_precedence_list {
78f6e9c6 42 my $orig = shift;
43 my $self = shift;
44 @{ $self->{__immutable}{class_precedence_list}
45 ||= [ $self->$orig ] };
6446bd59 46}
47
48sub linearized_isa {
78f6e9c6 49 my $orig = shift;
50 my $self = shift;
51 @{ $self->{__immutable}{linearized_isa} ||= [ $self->$orig ] };
6446bd59 52}
53
54sub get_all_methods {
78f6e9c6 55 my $orig = shift;
56 my $self = shift;
57 @{ $self->{__immutable}{get_all_methods} ||= [ $self->$orig ] };
6446bd59 58}
59
60sub get_all_method_names {
78f6e9c6 61 my $orig = shift;
62 my $self = shift;
63 @{ $self->{__immutable}{get_all_method_names} ||= [ $self->$orig ] };
6446bd59 64}
65
66sub get_all_attributes {
78f6e9c6 67 my $orig = shift;
68 my $self = shift;
69 @{ $self->{__immutable}{get_all_attributes} ||= [ $self->$orig ] };
6446bd59 70}
f5d08022 71
6446bd59 72sub get_meta_instance {
78f6e9c6 73 my $orig = shift;
74 my $self = shift;
75 $self->{__immutable}{get_meta_instance} ||= $self->$orig;
6446bd59 76}
77
78sub get_method_map {
78f6e9c6 79 my $orig = shift;
80 my $self = shift;
81 $self->{__immutable}{get_method_map} ||= $self->$orig;
6446bd59 82}
f5d08022 83
86e1c8d8 84sub add_package_symbol {
85 my $orig = shift;
86 my $self = shift;
87 confess "Cannot add package symbols to an immutable metaclass"
88 unless ( caller(3) )[3] eq 'Class::MOP::Package::get_package_symbol';
89
90 $self->$orig(@_);
91}
92
f5d08022 931;
15726b75 94
95__END__
96
97=pod
98
99=head1 NAME
100
101Class::MOP::Class::Immutable::Trait - Implements immutability for metaclass objects
102
103=head1 DESCRIPTION
104
9e25e01f 105This class provides a pseudo-trait that is applied to immutable metaclass
106objects. In reality, it is simply a parent class.
107
108It implements caching and read-only-ness for various metaclass methods.
15726b75 109
110=head1 AUTHOR
111
112Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
113
114=head1 COPYRIGHT AND LICENSE
115
116Copyright 2009 by Infinity Interactive, Inc.
117
118L<http://www.iinteractive.com>
119
120This library is free software; you can redistribute it and/or modify
121it under the same terms as Perl itself.
122
123=cut
124