File: README

package info (click to toggle)
libffi-c-perl 0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 484 kB
  • sloc: perl: 1,517; ansic: 57; sh: 19; makefile: 2
file content (287 lines) | stat: -rw-r--r-- 7,322 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
NAME

    FFI::C - C data types for FFI

VERSION

    version 0.15

SYNOPSIS

    In C:

     #include <stdint.h>
     
     typedef struct {
       uint8_t red;
       uint8_t green;
       uint8_t blue;
     } color_value_t;
     
     typedef struct {
       char name[22];
       color_value_t value;
     } named_color_t;
     
     typedef named_color_t array_named_color_t[4];
     
     typedef union {
       uint8_t  u8;
       uint16_t u16;
       uint32_t u32;
       uint64_t u64;
     } anyint_t;

    In Perl:

     use FFI::C;
     
     package ColorValue {
       FFI::C->struct([
         red   => 'uint8',
         green => 'uint8',
         blue  => 'uint8',
       ]);
     }
     
     package NamedColor {
       FFI::C->struct([
         name  => 'string(22)',
         value => 'color_value_t',
       ]);
     }
     
     package ArrayNamedColor {
       FFI::C->array(['named_color_t' => 4]);
     };
     
     my $array = ArrayNamedColor->new([
       { name => "red",    value => { red   => 255 } },
       { name => "green",  value => { green => 255 } },
       { name => "blue",   value => { blue  => 255 } },
       { name => "purple", value => { red   => 255,
                                      blue  => 255 } },
     ]);
     
     # dim each color by 1/2
     foreach my $color (@$array)
     {
       $color->value->red  ( $color->value->red   / 2 );
       $color->value->green( $color->value->green / 2 );
       $color->value->blue ( $color->value->blue  / 2 );
     }
     
     # print out the colors
     foreach my $color (@$array)
     {
       printf "%s [%02x %02x %02x]\n",
         $color->name,
         $color->value->red,
         $color->value->green,
         $color->value->blue;
     }
     
     package AnyInt {
       FFI::C->union([
         u8  => 'uint8',
         u16 => 'uint16',
         u32 => 'uint32',
         u64 => 'uint64',
       ]);
     }
     
     my $int = AnyInt->new({ u8 => 42 });
     print $int->u32;

DESCRIPTION

    This distribution provides tools for building classes to interface for
    common C data types. Arrays, struct, union and nested types based on
    those are supported.

    Core FFI::Platypus also provides FFI::Platypus::Record for manipulating
    and passing structured data. Typically you want to use FFI::C instead,
    the main exception is when you need to pass structured data by value
    instead of by reference.

    To work with C APIs that work with C file pointers you can use
    FFI::C::File and FFI::C::PosixFile. For C APIs that expose the POSIX
    stat structure use FFI::C::Stat.

METHODS

 ffi

     FFI::C->ffi($ffi);
     my $ffi = FFI::C->ffi;

    Get or set the FFI::Platypus instance used for the current Perl file
    (.pl or .pm).

    By default a new Platypus instance is created the on the first call to
    ffi, or when a new type is created with struct, union or array below,
    so if you want to use your own Platypus instance make sure that you set
    it as soon as possible.

    The Platypus instance is file scoped because scoping on just one
    package doesn't make sense if you are defining multiple types in one
    file since each type must be in its own package. It also doesn't make
    sense to make the Platypus instance global, because different
    distributions would conflict.

 struct

     FFI::C->struct($name, \@members);
     FFI::C->struct(\@members);

    Generate a new FFI::C::Struct class with the given @members into the
    calling package. (@members should be a list of name/type pairs). You
    may optionally give a $name which will be used for the FFI::Platypus
    type name for the generated class. If you do not specify a $name, a C
    style name will be generated from the last segment in the calling
    package name by converting to snake case and appending a _t to the end.

    As an example, given:

     package MyLibrary::FooBar {
       FFI::C->struct([
         a => 'uint8',
         b => 'float',
       ]);
     };

    You can use MyLibrary::FooBar via the file scoped FFI::Platypus
    instance using the type foo_bar_t.

     my $foobar = MyLibrary::FooBar->new({ a => 1, b => 3.14 });
     $ffi->function( my_library_func => [ 'foo_bar_t' ] => 'void' )->call($foobar);

 union

     FFI::C->union($name, \@members);
     FFI::C->union(\@members);

    This works exactly like the struct method above, except a FFI::C::Union
    class is generated instead.

 array

     FFI::C->array($name, [$type, $count]);
     FFI::C->array($name, [$type]);
     FFI::C->array([$type, $count]);
     FFI::C->array([$type]);

    This is similar to struct and union above, except FFI::C::Array is
    generated. For an array you give it the member type and the element
    count. The element count is optional for variable length arrays, but
    keep in mind that when you create such an array you do need to provide
    a size.

 enum

     FFI::C->enum($name, \@values, \%config);
     FFI::C->enum(\@values, \%config);
     FFI::C->enum(\@values, \%config);
     FFI::C->enum(\@values);

    Defines an enum. The @values and %config are passed to
    FFI::Platypus::Type::Enum, except the constants are exported to the
    calling package by default.

EXAMPLES

 unix time struct

     use FFI::Platypus 1.00;
     use FFI::C;
     
     my $ffi = FFI::Platypus->new(
       api => 1,
       lib => [undef],
     );
     FFI::C->ffi($ffi);
     
     package Unix::TimeStruct {
     
       FFI::C->struct(tm => [
         tm_sec    => 'int',
         tm_min    => 'int',
         tm_hour   => 'int',
         tm_mday   => 'int',
         tm_mon    => 'int',
         tm_year   => 'int',
         tm_wday   => 'int',
         tm_yday   => 'int',
         tm_isdst  => 'int',
         tm_gmtoff => 'long',
         _tm_zone  => 'opaque',
       ]);
     
       # For now 'string' is unsupported by FFI::C, but we
       # can cast the time zone from an opaque pointer to
       # string.
       sub tm_zone {
         my $self = shift;
         $ffi->cast('opaque', 'string', $self->_tm_zone);
       }
     
       # attach the C localtime function
       $ffi->attach( localtime => ['time_t*'] => 'tm', sub {
         my($inner, $class, $time) = @_;
         $time = time unless defined $time;
         $inner->(\$time);
       });
     }
     
     # now we can actually use our My::UnixTime class
     my $time = Unix::TimeStruct->localtime;
     printf "time is %d:%d:%d %s\n",
       $time->tm_hour,
       $time->tm_min,
       $time->tm_sec,
       $time->tm_zone;

CAVEATS

    FFI::C objects must be passed into C via FFI::Platypus by pointers.
    So-called "pass-by-value" is not and will not be supported. For
    "pass-by-value" record types, you should instead use
    FFI::Platypus::Record.

SEE ALSO

    FFI::C

    FFI::C::Array

    FFI::C::ArrayDef

    FFI::C::Def

    FFI::C::File

    FFI::C::PosixFile

    FFI::C::Struct

    FFI::C::StructDef

    FFI::C::Union

    FFI::C::UnionDef

    FFI::C::Util

    FFI::Platypus::Record

AUTHOR

    Graham Ollis <plicease@cpan.org>

COPYRIGHT AND LICENSE

    This software is copyright (c) 2020-2022 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.