File: Enum.pm

package info (click to toggle)
libffi-platypus-type-enum-perl 0.06-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 188 kB
  • sloc: perl: 111; sh: 3; makefile: 2
file content (424 lines) | stat: -rw-r--r-- 9,941 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package FFI::Platypus::Type::Enum;

use strict;
use warnings;
use constant 1.32 ();
use 5.008001;
use Ref::Util qw( is_plain_arrayref is_plain_hashref is_ref );
use Scalar::Util qw( dualvar );
use Carp qw( croak );

# ABSTRACT: Custom platypus type for dealing with C enumerated types
our $VERSION = '0.06'; # VERSION


our @CARP_NOT = qw( FFI::Platypus );

sub ffi_custom_type_api_1
{
  my %config = defined $_[2] && is_plain_hashref $_[2]
    ? %{ splice(@_, 2, 1) }
    : ();
  my(undef, undef, @values) = @_;

  my $index = 0;
  my %str_lookup;
  my %int_lookup;
  my $prefix = defined $config{prefix} ? $config{prefix} : '';
  $config{rev} ||= 'str';
  ($config{rev} =~ /^(int|str|dualvar)$/) or croak("rev must be either 'int', 'str', or 'dualvar'");

  $config{casing} ||= 'upper';
  ($config{casing} =~ /^(upper|keep)$/) or croak("casing must be either 'upper' or 'keep'");

  foreach my $value (@values)
  {
    my $name;
    my @aliases;

    if(is_plain_arrayref $value)
    {
      my %opt;
      if(@$value % 2)
      {
        ($name,%opt) = @$value;
      }
      else
      {
        ($name,$index,%opt) = @$value;
      }
      @aliases = @{ delete $opt{alias} || [] };
      croak("unrecognized options: @{[ sort keys %opt ]}") if %opt;
    }
    elsif(!is_ref $value)
    {
      $name = $value;
    }
    else
    {
      croak("not a array ref or scalar: $value");
    }

    if($index < 0)
    {
      $config{type} ||= 'senum';
    }

    if(my $packages = $config{package})
    {
      foreach my $package (is_plain_arrayref $packages ? @$packages : $packages)
      {
        foreach my $name ($name,@aliases)
        {
          my $full = join '::', $package, $prefix . ( $config{casing} eq 'upper' ? uc($name) : $name );
          constant->import($full, $index);
        }
      }
    }

    croak("$name declared twice") if exists $str_lookup{$name};

    $int_lookup{$index} = $name unless exists $int_lookup{$index};
    $str_lookup{$_}     = $index for @aliases;
    $str_lookup{$name}  = $index++;
  }

  $config{type} ||= 'enum';

  if(defined $config{maps})
  {
    if(is_plain_arrayref $config{maps})
    {
      @{ $config{maps} } = (\%str_lookup, \%int_lookup, $config{type});
    }
    else
    {
      croak("maps is not an array reference");
    }
  }

  my %type = (
    native_type    => $config{type},
    perl_to_native => sub {
      exists $str_lookup{$_[0]}
        ? $str_lookup{$_[0]}
        : exists $int_lookup{$_[0]}
          ? $_[0]
          : croak("illegal enum value $_[0]");
    },
  );

  if($config{rev} eq 'str')
  {
    $type{native_to_perl} = sub {
      exists $int_lookup{$_[0]}
        ? $int_lookup{$_[0]}
        : $_[0];
    }
  }
  elsif($config{rev} eq 'dualvar')
  {
    $type{native_to_perl} = sub {
      exists $int_lookup{$_[0]}
        ? dualvar( $_[0], $int_lookup{$_[0]} )
        : $_[0];
    };
  }

  \%type;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

FFI::Platypus::Type::Enum - Custom platypus type for dealing with C enumerated types

=head1 VERSION

version 0.06

=head1 SYNOPSIS

C:

 enum {
   DEFAULT,
   BETTER,
   BEST = 12
 } foo_t;
 
 foo_t
 f(foo_t arg)
 {
   return foo_t;
 }

Perl with strings:

 use FFI::Platypus 1.00;
 my $ffi = FFI::Platypus->new( api => 1 );
 
 $ffi->load_custom_type('::Enum', 'foo_t',
   'default',
   'better',
   ['best' => 12],
 );
 
 $ffi->attach( f => ['foo_t'] => 'foo_t' );
 
 f("default") eq 'default';  # true
 f("default") eq 'better';   # false
 
 print f("default"), "\n";   # default
 print f("better"),  "\n";   # better
 print f("best"),    "\n";   # best

Perl with constants:

 use FFI::Platypus 1.00;
 my $ffi = FFI::Platypus->new( api => 1 );
 
 $ffi->load_custom_type('::Enum', 'foo_t',
   { rev => 'int', package => 'Foo', prefix => 'FOO_' },
   'default',
   'better',
   ['best' => 12],
 );
 
 $ffi->attach( f => ['foo_t'] => 'foo_t' );
 
 f(Foo::FOO_DEFAULT) == Foo::FOO_DEFAULT;   # true
 f(Foo::FOO_DEFAULT) == Foo::FOO_BETTER;    # false

=head1 DESCRIPTION

This type plugin is a helper for making enumerated types.  It makes the most sense
to use this when you have an enumerated type with a small number of possible values.
For a large set of enumerated values or constants, see L<FFI::Platypus::Constant>.

This type plugin has two modes:

=over 4

=item string

In string mode, string representations of the enum values are converted into
the integer enum values when passed into C, and the enums are converted back
into strings when coming from C back into Perl.  You can also pass in the
integer values.

=item constant

In constant mode, constants are defined in the specified package, and with
the optional prefix.  The string representation or integer constants can
be passed into C, but the integer constants are returned from C back into
Perl.

=back

In both modes, if you attempt to pass in a value that isn't one of the possible
enum values, an exception will be thrown.

=head1 OPTIONS

The general form of the custom type load is:

 $ffi->load_custom_type('::Enum', $name, \%options, @values);
 $ffi->load_custom_type('::Enum', $name, @values);

The enumerated values are specified as a list of strings and array references.

=over 4

=item string

 $ffi->load_custom_type('::Enum', $name, $string1, $string2, ... );

For strings the constant value starts at zero (0) and increases by one for each
possible value.

=item array reference

 $ffi->load_custom_type('::Enum', $name, [ $value_name, $value, %options ]);
 $ffi->load_custom_type('::Enum', $name, [ $value_name, %options ]);

You can use an array reference to include an explicit integer value, rather
than using the implicit incremented value.  You can also use the array
reference for value options.  If the value isn't included (that is if
there are an odd number of values in the array reference), then the
implicit incremented value will be used.

Value options:

=over 4

=item alias

 $ffi->load_custom_type('::Enum, $name, [ $value_name, $value, alias => \@aliases ]);
 $ffi->load_custom_type('::Enum, $name, [ $value_name, alias => \@aliases ]);

The C<alias> option lets you specify value aliases.  For example, suppose you have
an enum definition like:

 enum {
   FOO,
   BAR,
   BAZ=BAR,
   ABC,
   XYZ
 } foo_t;

The Perl definition would be:

 $ffi->load_custom_type('::Enum', 'foo_t',
   'foo',
   ['bar', alias => ['baz']],
   'abc',
   'xyz',
 );

=back

=back

Type options may be passed in as a hash reference after the type name.

Type options:

=over 4

=item maps

 my @maps;
 $ffi->load_custom_type('::Enum', $name, { maps => \@maps }, ... );
 my($str,$int,$type) = @maps;

If set to an empty array reference, this will be filled with the string, integer
and native type for the enum.

=item package

 $ffi->load_custom_type('::Enum', $name, { package => $package  }, ... );
 $ffi->load_custom_type('::Enum', $name, { package => \@package }, ... );  # version 0.05

This option specifies the Perl package where constants will be defined.
If not specified, then no constants will be generated.  Unless otherwise
specified (see 'casing' below), the constants will be the upper case of
the value names as per the usual convention.

[version 0.05]

As of version 0.05, you can specify multiple packages to create the constants via
an array reference.

=item prefix

 $ffi->load_custom_type('::Enum', $name, { prefix => $prefix }, ... );

This specifies an optional prefix to give each constant.  If not specified,
then no prefix will be used.

=item rev

 $ffi->load_custom_type('::Enum', $name, { rev => 'int'     }, ... );
 $ffi->load_custom_type('::Enum', $name, { rev => 'str'     }, ... );
 $ffi->load_custom_type('::Enum', $name, { rev => 'dualvar' }, ... );  # version 0.05

This specifies what should be returned for C functions that return the
enumerated type.  For strings, use C<str>, and for integer constants use
C<int>.

(C<rev> is short for "reverse")

[version 0.05]

As of version 0.05, dualvar can be specified to return a string/integer
dualvar.

=item type

 $ffi->load_custom_type('::Enum', $name, { type => $type }, ... );

This specifies the integer type that should be used for the enumerated
type.  The default is to use C<enum> for types that only have positive
possible values and C<senum> for types that have possible negative values.
(Note that on some platforms these two types may actually be the same).

You can also use other integer types, which is useful if the enum is
only used to define constants, and the values are stored in a type
smaller than the default for C<enum> or C<senum>.  For example:

C:

 enum {
   DEFAULT,
   BETTER,
   BEST = 12
 } foo_enum;
 typedef uint8_t foo_t;
 
 /*
  * you are expected to use the constants from foo_enum,
  * but the signature actually uses a uint8_t
  */
 void f(foo_t);

Perl:

 $ffi->load_custom_type('::Enum', 'foo_t',
   { type => 'uint8' },
   'default',
   'better',
   [best => 12],
 );

 $ffi->attach( f => [ 'foo_t' ] => 'void' );

=item casing

[version 0.06]

 $ffi->load_custom_type('::Enum', $name, { casing => 'upper' }, ... );
 $ffi->load_custom_type('::Enum', $name, { casing => 'keep'  }, ... );

When in constant mode, all constant names are by default generated in
uppercase as is conventional.  However, some libraries will on occasion
define constant names in mixed case.  For these cases, the C<casing> option,
added in version 0.06, can be set to C<keep> to prevent the names from being
modified.  The only other allowed value is C<upper>, which is the default.

=back

=head1 SEE ALSO

=over 4

=item L<FFI::Platypus>

=item L<FFI::C>

=back

=head1 AUTHOR

Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>

Contributors:

José Joaquín Atria (JJATRIA)

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by Graham Ollis.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut