1 | #------------------------------------------------------------------------------- |
---|
2 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
3 | # |
---|
4 | # This file is part of FCM, tools for managing and building source code. |
---|
5 | # |
---|
6 | # FCM is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # FCM is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | #------------------------------------------------------------------------------- |
---|
19 | use strict; |
---|
20 | use warnings; |
---|
21 | |
---|
22 | #------------------------------------------------------------------------------- |
---|
23 | package FCM::Class::HASH; |
---|
24 | use FCM::Class::Exception; |
---|
25 | use Scalar::Util qw{reftype}; |
---|
26 | |
---|
27 | # Methods for working out the default value of an attribute. |
---|
28 | my %ATTRIB_DEFAULT_BY = ( |
---|
29 | default => sub { |
---|
30 | my $opt_ref = shift(); |
---|
31 | my $ret = $opt_ref->{default}; |
---|
32 | return (ref($ret) && reftype($ret) eq 'CODE' ? $ret->() : $ret); |
---|
33 | }, |
---|
34 | isa => sub { |
---|
35 | my $opt_ref = shift(); |
---|
36 | return |
---|
37 | $opt_ref->{isa} eq 'ARRAY' ? [] |
---|
38 | : $opt_ref->{isa} eq 'HASH' ? {} |
---|
39 | : $opt_ref->{isa} eq 'CODE' ? sub {} |
---|
40 | : undef |
---|
41 | ; |
---|
42 | }, |
---|
43 | ); |
---|
44 | |
---|
45 | # Checks the value of an attribute. |
---|
46 | my $ATTRIB_CHECK = sub { |
---|
47 | my ($class, $opt_ref, $key, $value, $caller_ref) = @_; |
---|
48 | # Note: undef is always OK? |
---|
49 | if (!defined($value)) { |
---|
50 | return; |
---|
51 | } |
---|
52 | my $expected_isa = $opt_ref->{isa}; |
---|
53 | if (!$expected_isa || $expected_isa eq 'SCALAR' && !ref($value)) { |
---|
54 | return; |
---|
55 | } |
---|
56 | if (!UNIVERSAL::isa($value, $expected_isa)) { |
---|
57 | return FCM::Class::Exception->throw({ |
---|
58 | 'code' => FCM::Class::Exception->CODE_TYPE, |
---|
59 | 'caller' => $caller_ref, |
---|
60 | 'package' => $class, |
---|
61 | 'key' => $key, |
---|
62 | 'type' => $expected_isa, |
---|
63 | 'value' => $value, |
---|
64 | }); |
---|
65 | } |
---|
66 | }; |
---|
67 | |
---|
68 | # Creates the methods of the class. |
---|
69 | sub class { |
---|
70 | my ($class, $attrib_opt_ref, $class_opt_ref) = @_; |
---|
71 | my %class_opt = ( |
---|
72 | init => sub {}, |
---|
73 | init_attrib => sub {@_}, |
---|
74 | (defined($class_opt_ref) ? %{$class_opt_ref} : ()), |
---|
75 | ); |
---|
76 | if (!defined($attrib_opt_ref)) { |
---|
77 | $attrib_opt_ref = {}; |
---|
78 | } |
---|
79 | my %attrib_opt; |
---|
80 | while (my ($key, $item) = each(%{$attrib_opt_ref})) { |
---|
81 | my %option = ( |
---|
82 | r => 1, # readable? |
---|
83 | w => 1, # writable? |
---|
84 | add => undef, # isa eq 'HASH' only, class of HASH element |
---|
85 | default => undef, # default value or CODE to return it |
---|
86 | isa => undef, # attribute type |
---|
87 | ( defined($item) && ref($item) ? %{$item} |
---|
88 | : defined($item) ? (isa => $item) |
---|
89 | : () |
---|
90 | ), |
---|
91 | ); |
---|
92 | if (defined($option{isa})) { |
---|
93 | $option{isa} |
---|
94 | = $option{isa} eq '$' ? 'SCALAR' |
---|
95 | : $option{isa} eq '@' ? 'ARRAY' |
---|
96 | : $option{isa} eq '%' ? 'HASH' |
---|
97 | : $option{isa} eq '&' ? 'CODE' |
---|
98 | : $option{isa} eq '*' ? 'GLOB' |
---|
99 | : $option{isa} |
---|
100 | ; |
---|
101 | } |
---|
102 | $attrib_opt{$key} = \%option; |
---|
103 | } |
---|
104 | no strict qw{refs}; |
---|
105 | # $class->new(\%attrib) |
---|
106 | *{$class . '::new'} = sub { |
---|
107 | my $class = shift(); |
---|
108 | my ($attrib_ref) = $class_opt{init_attrib}->(@_); |
---|
109 | my $caller_ref = [caller()]; |
---|
110 | my %attrib = (defined($attrib_ref) ? %{$attrib_ref} : ()); |
---|
111 | while (my ($key, $value) = each(%attrib)) { |
---|
112 | $ATTRIB_CHECK->($class, $attrib_opt{$key}, $key, $value, $caller_ref); |
---|
113 | } |
---|
114 | my $self = bless(\%attrib, $class); |
---|
115 | KEY: |
---|
116 | while (my ($key, $opt_ref) = each(%attrib_opt)) { |
---|
117 | if (exists($self->{$key})) { |
---|
118 | next KEY; |
---|
119 | } |
---|
120 | for my $opt_name (qw{default isa}) { |
---|
121 | if (defined($opt_ref->{$opt_name})) { |
---|
122 | $self->{$key} = $ATTRIB_DEFAULT_BY{$opt_name}->($opt_ref); |
---|
123 | next KEY; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | $class_opt{init}->($self); |
---|
128 | return $self; |
---|
129 | }; |
---|
130 | # $instance->$methods() |
---|
131 | while (my ($key, $opt_ref) = each(%attrib_opt)) { |
---|
132 | # $instance->get_$attrib() |
---|
133 | # $instance->get_$attrib($name) |
---|
134 | if ($opt_ref->{r}) { |
---|
135 | *{$class . '::get_' . $key} |
---|
136 | = defined($opt_ref->{isa}) && $opt_ref->{isa} eq 'HASH' |
---|
137 | ? sub { |
---|
138 | my ($self, $name) = @_; |
---|
139 | if (!defined($name)) { |
---|
140 | return $self->{$key}; |
---|
141 | } |
---|
142 | if (exists($self->{$key}{$name})) { |
---|
143 | return $self->{$key}{$name}; |
---|
144 | } |
---|
145 | return; |
---|
146 | } |
---|
147 | : sub {$_[0]->{$key}} |
---|
148 | ; |
---|
149 | } |
---|
150 | # $instance->set_$attrib($value) |
---|
151 | if ($opt_ref->{w}) { |
---|
152 | *{$class . '::set_' . $key} = sub { |
---|
153 | my ($self, $value) = @_; |
---|
154 | $ATTRIB_CHECK->( |
---|
155 | $class, $attrib_opt{$key}, $key, $value, [caller()], |
---|
156 | ); |
---|
157 | $self->{$key} = $value; |
---|
158 | return $self; |
---|
159 | }; |
---|
160 | } |
---|
161 | # $instance->add_$attrib($name,\%option) |
---|
162 | if ( defined($opt_ref->{isa}) && $opt_ref->{isa} eq 'HASH' |
---|
163 | && defined($opt_ref->{add}) |
---|
164 | ) { |
---|
165 | *{$class . '::add_' . $key} = sub { |
---|
166 | my ($self, $name, @args) = @_; |
---|
167 | if (defined($self->{$key}{$name})) { |
---|
168 | return $self->{$key}{$name}; |
---|
169 | } |
---|
170 | $self->{$key}{$name} = $opt_ref->{add}->new(@args); |
---|
171 | }; |
---|
172 | } |
---|
173 | } |
---|
174 | return 1; |
---|
175 | } |
---|
176 | |
---|
177 | #------------------------------------------------------------------------------- |
---|
178 | 1; |
---|
179 | __END__ |
---|
180 | |
---|
181 | =head1 NAME |
---|
182 | |
---|
183 | FCM::Class::HASH |
---|
184 | |
---|
185 | =head1 SYNOPSIS |
---|
186 | |
---|
187 | package Breakfast; |
---|
188 | use base qw{FCM::Class::HASH}; |
---|
189 | __PACKAGE__->class( |
---|
190 | { |
---|
191 | eggs => {isa => '@'}, |
---|
192 | ham => {isa => '%'}, |
---|
193 | bacon => '$', |
---|
194 | # ... |
---|
195 | }, |
---|
196 | ); |
---|
197 | # Some time later... |
---|
198 | $breakfast = Breakfast->new(\%attrib); |
---|
199 | @eggs = @{$breakfast->get_eggs()}; |
---|
200 | $breakfast->set_ham(\%ham); |
---|
201 | |
---|
202 | =head1 DESCRIPTION |
---|
203 | |
---|
204 | Provides a simple method to create HASH-based classes. |
---|
205 | |
---|
206 | The class() method creates the new() method for initiating a new instance. It |
---|
207 | also provides a get_$attrib() and set_$attrib() accessors for each attribute. |
---|
208 | Basic type checkings are performed on writing to the attributes to ensure |
---|
209 | correct usage. |
---|
210 | |
---|
211 | =head1 METHODS |
---|
212 | |
---|
213 | =over 4 |
---|
214 | |
---|
215 | =item $class->class(\%attrib_opt,\%class_opt) |
---|
216 | |
---|
217 | Creates the class, using the attribute options in %attrib_opt and %class_opt. |
---|
218 | |
---|
219 | The %attrib_opt is used to configure the attributes of an instance of the class. |
---|
220 | The key of each element is the name of the attribute, and the value is a HASH |
---|
221 | containing the options of the attribute, or a SCALAR. (If a SCALAR is specified, |
---|
222 | it is equivalent to {isa => value}.). The options may contain: |
---|
223 | |
---|
224 | =over 4 |
---|
225 | |
---|
226 | =item r |
---|
227 | |
---|
228 | (Default=true) If true, the attribute is readable. |
---|
229 | |
---|
230 | =item w |
---|
231 | |
---|
232 | (Default=true) If true, the attribute is writable. |
---|
233 | |
---|
234 | =item add |
---|
235 | |
---|
236 | (Default=undef) This is only useful for a HASH attribute. If defined, it should |
---|
237 | be the name of a class (e.g. $attrib_class). The HASH attribute will receive an |
---|
238 | extra method $instance->add_$attrib($key,@args). The method will assign the |
---|
239 | $name element of the HASH attribute to the result of $attrib_class->new(@args). |
---|
240 | |
---|
241 | =item default |
---|
242 | |
---|
243 | (Default=undef) The default value of the attribute. |
---|
244 | |
---|
245 | If this option is defined, the attribute will be initialised to the specified |
---|
246 | value when the new() method is called. In the special case where the value of |
---|
247 | this option is a CODE reference, it will be invoked as $code->(\%attrib), and |
---|
248 | the default value will be the returned value of the CODE reference. This is |
---|
249 | useful, for example, if the default value needs to be a new instance of a class. |
---|
250 | If a genuine CODE reference is required as the default, this option should be |
---|
251 | set to a CODE reference that returns the required CODE reference itself. |
---|
252 | |
---|
253 | For example: |
---|
254 | |
---|
255 | Foo->class({ |
---|
256 | foo => {default => 'foo'}, # 'foo' |
---|
257 | bar => {default => sub {get_id()}}, # the next id |
---|
258 | baz => {default => sub {\&code}}, # &code |
---|
259 | }); |
---|
260 | { |
---|
261 | my $id = 0; |
---|
262 | sub get_id {$id++} |
---|
263 | } |
---|
264 | |
---|
265 | If the default options is not defined, and if the attribute "isa" is ARRAY, HASH |
---|
266 | or CODE, then the default value is [], {} and sub {} respectively. |
---|
267 | |
---|
268 | =item isa |
---|
269 | |
---|
270 | (Default=undef) The expected type of the attribute. If this optioin is defined |
---|
271 | as $type, a new $value of the attribute is only accepted if $value is undef, |
---|
272 | UNIVERSAL::isa($value,$type) returns true or if $type is C<SCALAR> and the new |
---|
273 | value is not a reference. |
---|
274 | |
---|
275 | The attribute accepts $, @, %, & and * as aliases to SCALAR, ARRAY, HASH, CODE |
---|
276 | and GLOB. |
---|
277 | |
---|
278 | =back |
---|
279 | |
---|
280 | The argument %class_opt can have the following elements: |
---|
281 | |
---|
282 | =over 4 |
---|
283 | |
---|
284 | =item init |
---|
285 | |
---|
286 | If $class_opt{init} is defined, it should be a CODE reference. If specified, it |
---|
287 | will be called just after the instance is blessed in the $class->new() method, |
---|
288 | with an interface $f->($instance) where $instance is the new instance. |
---|
289 | |
---|
290 | =item init_attrib |
---|
291 | |
---|
292 | The value of this option must be a CODE. The $class->new() normally expects a |
---|
293 | single HASH reference argument. If an alternate interface to the $class->new() |
---|
294 | is required, this CODE can be used to turn the input argument list to the |
---|
295 | expected HASH reference. |
---|
296 | |
---|
297 | =back |
---|
298 | |
---|
299 | =item $class->new(\%attrib) |
---|
300 | |
---|
301 | Creates a new instance with %attrib. Initial values of the attributes can be |
---|
302 | specified using %attrib. Otherwise, the method will attempt to assign the |
---|
303 | default values, as specified in the class() method, to the newly created |
---|
304 | instance. |
---|
305 | |
---|
306 | =item $instance->get_$attrib() |
---|
307 | |
---|
308 | Returns a readable attribute. |
---|
309 | |
---|
310 | =item $instance->get_$attrib($key) |
---|
311 | |
---|
312 | These are available for HASH attributes only. Returns the value of an element in |
---|
313 | a readable attribute. |
---|
314 | |
---|
315 | =item $instance->set_$attrib($value) |
---|
316 | |
---|
317 | Sets the value of a writable attribute. Returns $instance. |
---|
318 | |
---|
319 | =item $instance->add_$attrib($key,@args) |
---|
320 | |
---|
321 | These are available for HASH attributes (with the C<add> attribute option |
---|
322 | defined) only. Adds a new $key element to the HASH attribute. Returns the newly |
---|
323 | added element. |
---|
324 | |
---|
325 | =back |
---|
326 | |
---|
327 | =head1 DIAGNOSTICS |
---|
328 | |
---|
329 | L<FCM::Class::Exception|FCM::Class::Exception> is thrown on error. |
---|
330 | |
---|
331 | =head1 SEE ALSO |
---|
332 | |
---|
333 | Inspired by the standard module L<Class::Struct|Class::Struct> and CPAN modules |
---|
334 | such as L<Class::Accessor|Class::Accessor>. |
---|
335 | |
---|
336 | =head1 COPYRIGHT |
---|
337 | |
---|
338 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
339 | |
---|
340 | =cut |
---|