Commit | Line | Data |
9ec169fe |
1 | |
9ec169fe |
2 | package # hide the package from PAUSE |
43715282 |
3 | InsideOutClass::Attribute; |
4 | |
5 | use strict; |
6 | use warnings; |
7 | |
ba38bf08 |
8 | our $VERSION = '0.02'; |
43715282 |
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) = @_; |
c23184fc |
17 | my $init_arg = $self->init_arg; |
43715282 |
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) |
c23184fc |
23 | if (!defined $val && defined $self->default) { |
43715282 |
24 | $val = $self->default($instance); |
25 | } |
6d5355c3 |
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); |
43715282 |
29 | } |
30 | |
ba38bf08 |
31 | sub accessor_metaclass { 'InsideOutClass::Method::Accessor' } |
32 | |
33 | package # hide the package from PAUSE |
34 | InsideOutClass::Method::Accessor; |
35 | |
36 | use strict; |
37 | use warnings; |
38 | |
39 | our $VERSION = '0.01'; |
40 | |
41 | use Carp 'confess'; |
42 | use Scalar::Util 'refaddr'; |
43 | |
44 | use base 'Class::MOP::Method::Accessor'; |
45 | |
43715282 |
46 | ## Method generation helpers |
47 | |
afc92ac6 |
48 | sub _generate_accessor_method { |
ba38bf08 |
49 | my $attr = (shift)->associated_attribute; |
50 | my $meta_class = $attr->associated_class; |
51 | my $attr_name = $attr->name; |
43715282 |
52 | return sub { |
53 | my $meta_instance = $meta_class->get_meta_instance; |
54 | $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2; |
55 | $meta_instance->get_slot_value($_[0], $attr_name); |
56 | }; |
57 | } |
58 | |
afc92ac6 |
59 | sub _generate_reader_method { |
ba38bf08 |
60 | my $attr = (shift)->associated_attribute; |
61 | my $meta_class = $attr->associated_class; |
62 | my $attr_name = $attr->name; |
43715282 |
63 | return sub { |
64 | confess "Cannot assign a value to a read-only accessor" if @_ > 1; |
65 | $meta_class->get_meta_instance |
66 | ->get_slot_value($_[0], $attr_name); |
67 | }; |
68 | } |
69 | |
afc92ac6 |
70 | sub _generate_writer_method { |
ba38bf08 |
71 | my $attr = (shift)->associated_attribute; |
72 | my $meta_class = $attr->associated_class; |
73 | my $attr_name = $attr->name; |
43715282 |
74 | return sub { |
75 | $meta_class->get_meta_instance |
76 | ->set_slot_value($_[0], $attr_name, $_[1]); |
77 | }; |
78 | } |
79 | |
afc92ac6 |
80 | sub _generate_predicate_method { |
ba38bf08 |
81 | my $attr = (shift)->associated_attribute; |
82 | my $meta_class = $attr->associated_class; |
83 | my $attr_name = $attr->name; |
43715282 |
84 | return sub { |
85 | defined $meta_class->get_meta_instance |
86 | ->get_slot_value($_[0], $attr_name) ? 1 : 0; |
87 | }; |
88 | } |
89 | |
90 | package # hide the package from PAUSE |
2d711cc8 |
91 | InsideOutClass::Instance; |
9ec169fe |
92 | |
93 | use strict; |
94 | use warnings; |
95 | |
b880e0de |
96 | our $VERSION = '0.01'; |
9ec169fe |
97 | |
b9dfbf78 |
98 | use Carp 'confess'; |
9ec169fe |
99 | use Scalar::Util 'refaddr'; |
100 | |
2d711cc8 |
101 | use base 'Class::MOP::Instance'; |
102 | |
103 | sub create_instance { |
49c93440 |
104 | my ($self, $class) = @_; |
cf11901e |
105 | bless \(my $instance), $self->_class_name; |
9ec169fe |
106 | } |
107 | |
2d711cc8 |
108 | sub get_slot_value { |
49c93440 |
109 | my ($self, $instance, $slot_name) = @_; |
c23184fc |
110 | $self->associated_metaclass->get_package_symbol('%' . $slot_name)->{refaddr $instance}; |
9ec169fe |
111 | } |
112 | |
2d711cc8 |
113 | sub set_slot_value { |
49c93440 |
114 | my ($self, $instance, $slot_name, $value) = @_; |
c23184fc |
115 | $self->associated_metaclass->get_package_symbol('%' . $slot_name)->{refaddr $instance} = $value; |
9ec169fe |
116 | } |
117 | |
49c93440 |
118 | sub initialize_slot { |
119 | my ($self, $instance, $slot_name) = @_; |
c23184fc |
120 | $self->associated_metaclass->add_package_symbol(('%' . $slot_name) => {}) |
121 | unless $self->associated_metaclass->has_package_symbol('%' . $slot_name); |
122 | $self->associated_metaclass->get_package_symbol('%' . $slot_name)->{refaddr $instance} = undef; |
49c93440 |
123 | } |
2d711cc8 |
124 | |
49c93440 |
125 | sub is_slot_initialized { |
126 | my ($self, $instance, $slot_name) = @_; |
c23184fc |
127 | return 0 unless $self->associated_metaclass->has_package_symbol('%' . $slot_name); |
128 | return exists $self->associated_metaclass->get_package_symbol('%' . $slot_name)->{refaddr $instance} ? 1 : 0; |
9ec169fe |
129 | } |
130 | |
9ec169fe |
131 | 1; |
132 | |
133 | __END__ |
134 | |
135 | =pod |
136 | |
137 | =head1 NAME |
138 | |
139 | InsideOutClass - A set of example metaclasses which implement the Inside-Out technique |
140 | |
141 | =head1 SYNOPSIS |
142 | |
143 | package Foo; |
144 | |
1becdfcc |
145 | use metaclass ( |
43715282 |
146 | ':attribute_metaclass' => 'InsideOutClass::Attribute', |
147 | ':instance_metaclass' => 'InsideOutClass::Instance' |
677eb158 |
148 | ); |
9ec169fe |
149 | |
2e41896e |
150 | __PACKAGE__->meta->add_attribute('foo' => ( |
151 | reader => 'get_foo', |
152 | writer => 'set_foo' |
153 | )); |
9ec169fe |
154 | |
155 | sub new { |
156 | my $class = shift; |
5659d76e |
157 | $class->meta->new_object(@_); |
158 | } |
9ec169fe |
159 | |
160 | # now you can just use the class as normal |
161 | |
162 | =head1 DESCRIPTION |
163 | |
164 | This is a set of example metaclasses which implement the Inside-Out |
165 | class technique. What follows is a brief explaination of the code |
166 | found in this module. |
167 | |
2d711cc8 |
168 | We must create a subclass of B<Class::MOP::Instance> and override |
169 | the slot operations. This requires |
170 | overloading C<get_slot_value>, C<set_slot_value>, C<slot_initialized>, and |
171 | C<initialize_slot>, as well as their inline counterparts. Additionally we |
172 | overload C<add_slot> in order to initialize the global hash containing the |
173 | actual slot values. |
9ec169fe |
174 | |
175 | And that is pretty much all. Of course I am ignoring need for |
176 | inside-out objects to be C<DESTROY>-ed, and some other details as |
2d711cc8 |
177 | well (threading, etc), but this is an example. A real implementation is left as |
178 | an exercise to the reader. |
9ec169fe |
179 | |
1a09d9cc |
180 | =head1 AUTHORS |
9ec169fe |
181 | |
182 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
183 | |
005adf8f |
184 | Yuval Kogman E<lt>nothingmuch@woobling.comE<gt> |
2d711cc8 |
185 | |
9ec169fe |
186 | =head1 COPYRIGHT AND LICENSE |
187 | |
69e3ab0a |
188 | Copyright 2006-2008 by Infinity Interactive, Inc. |
9ec169fe |
189 | |
190 | L<http://www.iinteractive.com> |
191 | |
192 | This library is free software; you can redistribute it and/or modify |
193 | it under the same terms as Perl itself. |
194 | |
195 | =cut |