foo
[gitmo/Class-MOP.git] / examples / InsideOutClass.pod
1
2 package # hide the package from PAUSE
3     InsideOutClass::Attribute;
4
5 use strict;
6 use warnings;
7
8 our $VERSION = '0.01';
9
10 use Carp         'confess';
11 use Scalar::Util 'refaddr';
12
13 use base 'Class::MOP::Attribute';
14
15 sub initialize_instance_slot {
16     my ($self, $meta_instance, $instance, $params) = @_;
17     my $init_arg = $self->{init_arg};
18     # try to fetch the init arg from the %params ...
19     my $val;        
20     $val = $params->{$init_arg} if exists $params->{$init_arg};
21     # if nothing was in the %params, we can use the 
22     # attribute's default value (if it has one)
23     if (!defined $val && defined $self->{default}) {
24         $val = $self->default($instance);
25     }
26     my $_meta_instance = $self->associated_class->get_meta_instance;
27     $_meta_instance->initialize_slot($instance, $self->name);
28     $_meta_instance->set_slot_value($instance, $self->name, $val);
29 }
30
31 ## Method generation helpers
32
33 sub generate_accessor_method {
34     my $self = shift;
35     my $meta_class = $self->associated_class;  
36     my $attr_name  = $self->name;
37     return sub {
38         my $meta_instance = $meta_class->get_meta_instance;
39         $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
40         $meta_instance->get_slot_value($_[0], $attr_name);
41     };
42 }
43
44 sub generate_reader_method {
45     my $self = shift;
46     my $meta_class = $self->associated_class;    
47     my $attr_name  = $self->name;
48     return sub { 
49         confess "Cannot assign a value to a read-only accessor" if @_ > 1;
50         $meta_class->get_meta_instance
51                    ->get_slot_value($_[0], $attr_name); 
52     }; 
53 }
54
55 sub generate_writer_method {
56     my $self = shift;
57     my $meta_class = $self->associated_class;    
58     my $attr_name  = $self->name;
59     return sub { 
60         $meta_class->get_meta_instance
61                    ->set_slot_value($_[0], $attr_name, $_[1]);
62     };
63 }
64
65 sub generate_predicate_method {
66     my $self = shift;
67     my $meta_class = $self->associated_class;   
68     my $attr_name  = $self->name;
69     return sub { 
70         defined $meta_class->get_meta_instance
71                            ->get_slot_value($_[0], $attr_name) ? 1 : 0;
72     };   
73 }
74
75 package # hide the package from PAUSE
76     InsideOutClass::Instance;
77
78 use strict;
79 use warnings;
80
81 our $VERSION = '0.01';
82
83 use Carp         'confess';
84 use Scalar::Util 'refaddr';
85
86 use base 'Class::MOP::Instance';
87
88 sub create_instance {
89         my ($self, $class) = @_;
90     $self->bless_instance_structure(\(my $instance));
91 }
92
93 sub get_slot_value {
94         my ($self, $instance, $slot_name) = @_;
95         $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance};
96 }
97
98 sub set_slot_value {
99         my ($self, $instance, $slot_name, $value) = @_;
100         $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = $value;
101 }
102
103 sub initialize_slot {
104     my ($self, $instance, $slot_name) = @_;
105     $self->{meta}->add_package_variable(('%' . $slot_name) => {})
106         unless $self->{meta}->has_package_variable('%' . $slot_name); 
107     $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = undef;
108 }
109
110 sub is_slot_initialized {
111         my ($self, $instance, $slot_name) = @_;
112         return 0 unless $self->{meta}->has_package_variable('%' . $slot_name);
113         return exists $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
114 }
115
116 1;
117
118 __END__
119
120 =pod
121
122 =head1 NAME
123
124 InsideOutClass - A set of example metaclasses which implement the Inside-Out technique
125
126 =head1 SYNOPSIS
127
128   package Foo;
129   
130   use metaclass (
131     ':attribute_metaclass' => 'InsideOutClass::Attribute',
132     ':instance_metaclass'  => 'InsideOutClass::Instance'
133   );
134   
135   __PACKAGE__->meta->add_attribute('foo' => (
136       reader => 'get_foo',
137       writer => 'set_foo'
138   ));    
139   
140   sub new  {
141       my $class = shift;
142       $class->meta->new_object(@_);
143   } 
144
145   # now you can just use the class as normal
146
147 =head1 DESCRIPTION
148
149 This is a set of example metaclasses which implement the Inside-Out 
150 class technique. What follows is a brief explaination of the code 
151 found in this module.
152
153 We must create a subclass of B<Class::MOP::Instance> and override 
154 the slot operations. This requires 
155 overloading C<get_slot_value>, C<set_slot_value>, C<slot_initialized>, and
156 C<initialize_slot>, as well as their inline counterparts. Additionally we
157 overload C<add_slot> in order to initialize the global hash containing the
158 actual slot values.
159
160 And that is pretty much all. Of course I am ignoring need for 
161 inside-out objects to be C<DESTROY>-ed, and some other details as 
162 well (threading, etc), but this is an example. A real implementation is left as
163 an exercise to the reader.
164
165 =head1 AUTHOR
166
167 Stevan Little E<lt>stevan@iinteractive.comE<gt>
168
169 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
170
171 =head1 COPYRIGHT AND LICENSE
172
173 Copyright 2006 by Infinity Interactive, Inc.
174
175 L<http://www.iinteractive.com>
176
177 This library is free software; you can redistribute it and/or modify
178 it under the same terms as Perl itself. 
179
180 =cut