New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
HASH.pm in vendors/lib/FCM/Class – NEMO

source: vendors/lib/FCM/Class/HASH.pm @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

File size: 10.7 KB
Line 
1#-------------------------------------------------------------------------------
2# (C) British Crown Copyright 2006-17 Met Office.
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#-------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22#-------------------------------------------------------------------------------
23package FCM::Class::HASH;
24use FCM::Class::Exception;
25use Scalar::Util qw{reftype};
26
27# Methods for working out the default value of an attribute.
28my %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.
46my $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.
69sub 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#-------------------------------------------------------------------------------
1781;
179__END__
180
181=head1 NAME
182
183FCM::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
204Provides a simple method to create HASH-based classes.
205
206The class() method creates the new() method for initiating a new instance. It
207also provides a get_$attrib() and set_$attrib() accessors for each attribute.
208Basic type checkings are performed on writing to the attributes to ensure
209correct usage.
210
211=head1 METHODS
212
213=over 4
214
215=item $class->class(\%attrib_opt,\%class_opt)
216
217Creates the class, using the attribute options in %attrib_opt and %class_opt.
218
219The %attrib_opt is used to configure the attributes of an instance of the class.
220The key of each element is the name of the attribute, and the value is a HASH
221containing the options of the attribute, or a SCALAR. (If a SCALAR is specified,
222it 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
237be the name of a class (e.g. $attrib_class). The HASH attribute will receive an
238extra 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
245If this option is defined, the attribute will be initialised to the specified
246value when the new() method is called. In the special case where the value of
247this option is a CODE reference, it will be invoked as $code->(\%attrib), and
248the default value will be the returned value of the CODE reference. This is
249useful, for example, if the default value needs to be a new instance of a class.
250If a genuine CODE reference is required as the default, this option should be
251set to a CODE reference that returns the required CODE reference itself.
252
253For 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
265If the default options is not defined, and if the attribute "isa" is ARRAY, HASH
266or 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
271as $type, a new $value of the attribute is only accepted if $value is undef,
272UNIVERSAL::isa($value,$type) returns true or if $type is C<SCALAR> and the new
273value is not a reference.
274
275The attribute accepts $, @, %, & and * as aliases to SCALAR, ARRAY, HASH, CODE
276and GLOB.
277
278=back
279
280The argument %class_opt can have the following elements:
281
282=over 4
283
284=item init
285
286If $class_opt{init} is defined, it should be a CODE reference. If specified, it
287will be called just after the instance is blessed in the $class->new() method,
288with an interface $f->($instance) where $instance is the new instance.
289
290=item init_attrib
291
292The value of this option must be a CODE. The $class->new() normally expects a
293single HASH reference argument. If an alternate interface to the $class->new()
294is required, this CODE can be used to turn the input argument list to the
295expected HASH reference.
296
297=back
298
299=item $class->new(\%attrib)
300
301Creates a new instance with %attrib. Initial values of the attributes can be
302specified using %attrib. Otherwise, the method will attempt to assign the
303default values, as specified in the class() method, to the newly created
304instance.
305
306=item $instance->get_$attrib()
307
308Returns a readable attribute.
309
310=item $instance->get_$attrib($key)
311
312These are available for HASH attributes only. Returns the value of an element in
313a readable attribute.
314
315=item $instance->set_$attrib($value)
316
317Sets the value of a writable attribute. Returns $instance.
318
319=item $instance->add_$attrib($key,@args)
320
321These are available for HASH attributes (with the C<add> attribute option
322defined) only. Adds a new $key element to the HASH attribute. Returns the newly
323added element.
324
325=back
326
327=head1 DIAGNOSTICS
328
329L<FCM::Class::Exception|FCM::Class::Exception> is thrown on error.
330
331=head1 SEE ALSO
332
333Inspired by the standard module L<Class::Struct|Class::Struct> and CPAN modules
334such as L<Class::Accessor|Class::Accessor>.
335
336=head1 COPYRIGHT
337
338(C) Crown copyright Met Office. All rights reserved.
339
340=cut
Note: See TracBrowser for help on using the repository browser.