Add tests for inline {con,de}structor warnings and inlining. Got rid
[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
8e863a0a 11our $VERSION = '0.86';
f04900bf 12$VERSION = eval $VERSION;
13our $AUTHORITY = 'cpan:STEVAN';
14
f5d08022 15sub meta {
16 my $self = shift;
17
18 # if it is not blessed, then someone is asking
19 # for the meta of Class::MOP::Class:;Immutable::Trait
20 return Class::MOP::Class->initialize($self) unless blessed($self);
21
22 # otherwise, they are asking for the metaclass
23 # which has been made immutable, which is itself
24 # except in the cases where it is a metaclass itself
25 # that has been made immutable and for that we need
26 # to dig a bit ...
27
28 if ( $self->isa('Class::MOP::Class') ) {
6446bd59 29
f5d08022 30 # except this is a lie... oh well
31 return Class::MOP::class_of( $self->get_mutable_metaclass_name );
32 }
33 else {
34 return $self;
35 }
36}
37
38# the original class of the metaclass instance
39sub get_mutable_metaclass_name { $_[0]{__immutable}{original_class} }
40
41sub immutable_options { %{ $_[0]{__immutable}{options} } }
42
6446bd59 43sub is_mutable {0}
44sub is_immutable {1}
f5d08022 45
46sub superclasses {
47 confess "This method is read-only" if @_ > 1;
48 $_[0]->next::method;
49}
50
6446bd59 51sub _immutable_cannot_call {
52 Carp::confess "This method cannot be called on an immutable instance";
53}
f5d08022 54
55sub add_method { shift->_immutable_cannot_call }
56sub alias_method { shift->_immutable_cannot_call }
57sub remove_method { shift->_immutable_cannot_call }
58sub add_attribute { shift->_immutable_cannot_call }
59sub remove_attribute { shift->_immutable_cannot_call }
60sub remove_package_symbol { shift->_immutable_cannot_call }
61
6446bd59 62sub class_precedence_list {
63 @{ $_[0]{__immutable}{class_precedence_list}
64 ||= [ shift->next::method ] };
65}
66
67sub linearized_isa {
68 @{ $_[0]{__immutable}{linearized_isa} ||= [ shift->next::method ] };
69}
70
71sub get_all_methods {
72 @{ $_[0]{__immutable}{get_all_methods} ||= [ shift->next::method ] };
73}
74
75sub get_all_method_names {
76 @{ $_[0]{__immutable}{get_all_method_names} ||= [ shift->next::method ] };
77}
78
79sub get_all_attributes {
80 @{ $_[0]{__immutable}{get_all_attributes} ||= [ shift->next::method ] };
81}
f5d08022 82
6446bd59 83sub get_meta_instance {
84 $_[0]{__immutable}{get_meta_instance} ||= shift->next::method;
85}
86
87sub get_method_map {
88 $_[0]{__immutable}{get_method_map} ||= shift->next::method;
89}
f5d08022 90
91sub add_package_symbol {
92 confess "Cannot add package symbols to an immutable metaclass"
6446bd59 93 unless ( caller(1) )[3] eq 'Class::MOP::Package::get_package_symbol';
f5d08022 94
95 shift->next::method(@_);
96}
97
981;
15726b75 99
100__END__
101
102=pod
103
104=head1 NAME
105
106Class::MOP::Class::Immutable::Trait - Implements immutability for metaclass objects
107
108=head1 DESCRIPTION
109
110This class provides a trait that is applied to immutable metaclass
111objects. This is deep guts.
112
113=head1 AUTHOR
114
115Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
116
117=head1 COPYRIGHT AND LICENSE
118
119Copyright 2009 by Infinity Interactive, Inc.
120
121L<http://www.iinteractive.com>
122
123This library is free software; you can redistribute it and/or modify
124it under the same terms as Perl itself.
125
126=cut
127