.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "FFI::Platypus::Record 3pm" .TH FFI::Platypus::Record 3pm 2024-01-10 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME FFI::Platypus::Record \- FFI support for structured records data .SH VERSION .IX Header "VERSION" version 2.08 .SH SYNOPSIS .IX Header "SYNOPSIS" C: .PP .Vb 5 \& struct my_person { \& int age; \& const char title[3]; \& const char *name \& }; \& \& void process_person(struct my_person *person) \& { \& /* ... */ \& } .Ve .PP Perl: .PP .Vb 1 \& package MyPerson; \& \& use FFI::Platypus::Record; \& \& record_layout_1( \& \*(Aqint\*(Aq => \*(Aqage\*(Aq, \& \*(Aqstring(3)\*(Aq => \*(Aqtitle\*(Aq, \& \*(Aqstring rw\*(Aq => \*(Aqname\*(Aq, \& ); \& \& package main; \& \& use FFI::Platypus 2.00; \& \& my $ffi = FFI::Platypus\->new( api => 2 ); \& $ffi\->lib("myperson.so"); \& $ffi\->type("record(MyPerson)" => \*(AqMyPerson\*(Aq); \& \& my $person = MyPerson\->new( \& age => 40, \& title => "Mr.", \& name => "John Smith", \& ); \& \& $ffi\->attach( process_person => [ \*(AqMyPerson*\*(Aq ] => \*(Aqvoid\*(Aq ); \& \& process_person($person); \& \& $person\->age($person\->age + 1); # another year older \& \& process_person($person); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" [version 0.21] .PP This module provides a mechanism for building classes that can be used to mange structured data records (known as C as "structs" and in some languages as "records"). A structured record is a series of bytes that have structure understood by the C or other foreign language library that you are interfacing with. It is designed for use with FFI and FFI::Platypus, though it may have other applications. .PP Before you get to deep into using this class you should also consider the FFI::C, which provides some overlapping functionality. Briefly, it comes down to this: .PP (The tl;dr is: use this class when you need to pass by value (since FFI::C does not support pass by value) and use FFI::C in all other circumstances). .IP FFI::Platypus::Record 4 .IX Item "FFI::Platypus::Record" Supports: .RS 4 .ie n .IP "C pointers to ""struct"" types" 4 .el .IP "C pointers to \f(CWstruct\fR types" 4 .IX Item "C pointers to struct types" .PD 0 .ie n .IP "Passing C ""struct""s by-value." 4 .el .IP "Passing C \f(CWstruct\fRs by-value." 4 .IX Item "Passing C structs by-value." .RE .RS 4 .PD .Sp Does not support: .ie n .IP "C ""union"" types." 4 .el .IP "C \f(CWunion\fR types." 4 .IX Item "C union types." .PD 0 .ie n .IP "C arrays of ""struct"" and ""union"" types." 4 .el .IP "C arrays of \f(CWstruct\fR and \f(CWunion\fR types." 4 .IX Item "C arrays of struct and union types." .RE .RS 4 .RE .IP FFI::C 4 .IX Item "FFI::C" .PD Supports: .RS 4 .ie n .IP "C ""struct"" and""union"" types" 4 .el .IP "C \f(CWstruct\fR and\f(CWunion\fR types" 4 .IX Item "C struct andunion types" .PD 0 .ie n .IP "C arrays of ""struct"" and ""union"" types." 4 .el .IP "C arrays of \f(CWstruct\fR and \f(CWunion\fR types." 4 .IX Item "C arrays of struct and union types." .RE .RS 4 .PD .Sp Does not support: .ie n .IP "Passing C ""struct""s by-value." 4 .el .IP "Passing C \f(CWstruct\fRs by-value." 4 .IX Item "Passing C structs by-value." .RE .RS 4 .Sp String members are as of this writing a TODO for FFI::C, but should be coming soon! .RE .SH FUNCTIONS .IX Header "FUNCTIONS" .SS record_layout_1 .IX Subsection "record_layout_1" .Vb 3 \& record_layout_1($ffi, $type => $name, ... ); \& record_layout_1(\e@ffi_args, $type => $name, ... ); \& record_layout_1($type => $name, ... ); .Ve .PP Define the layout of the record. You may optionally provide an instance of FFI::Platypus as the first argument in order to use its type aliases. Alternatively you may provide constructor arguments that will be passed to the internal platypus instance. Thus this is the same: .PP .Vb 4 \& my $ffi = FFI::Platypus\->new( lang => \*(AqRust\*(Aq, api => 2 ); \& record_layout_1( $ffi, ... ); \& # same as: \& record_layout_1( [ lang => \*(AqRust\*(Aq ], ... ); .Ve .PP and this is the same: .PP .Vb 4 \& my $ffi = FFI::Platypus\->new( api => 2 ); \& record_layout_1( $ffi, ... ); \& # same as: \& record_layout_1( ... ); .Ve .PP Then you provide members as type/name pairs. .PP For each member you declare, \f(CW\*(C`record_layout_1\*(C'\fR will create an accessor which can be used to read and write its value. For example imagine a class \f(CW\*(C`Foo\*(C'\fR: .PP .Vb 1 \& package Foo; \& \& use FFI::Platypus::Record; \& \& record_layout_1( \& int => \*(Aqbar\*(Aq, # int bar; \& \*(Aqstring(10)\*(Aq => \*(Aqbaz\*(Aq, # char baz[10]; \& ); .Ve .PP You can get and set its fields with like named \f(CW\*(C`bar\*(C'\fR and \f(CW\*(C`baz\*(C'\fR accessors: .PP .Vb 1 \& my $foo = Foo\->new; \& \& $foo\->bar(22); \& my $value = $foo\->bar; \& \& $foo\->baz("grimlock\e0\e0"); # should be 10 characters long \& my $string_value = $foo\->baz; # includes the trailing \e0\e0 .Ve .PP You can also pass initial values in to the constructor, either passing as a list of key value pairs or by passing a hash reference: .PP .Vb 4 \& $foo = Foo\->new( \& bar => 22, \& baz => "grimlock\e0\e0", \& ); \& \& # same as: \& \& $foo = Foo\->new( { \& bar => 22, \& baz => "grimlock\e0\e0", \& } ); .Ve .PP If there are members of a record that you need to account for in terms of size and alignment, but do not want to have an accessor for, you can use \f(CW\*(C`:\*(C'\fR as a place holder for its name: .PP .Vb 4 \& record_layout_1( \& \*(Aqint\*(Aq => \*(Aq:\*(Aq, \& \*(Aqstring(10)\*(Aq => \*(Aqbaz\*(Aq, \& ); .Ve .PP \fIstrings\fR .IX Subsection "strings" .PP So far I've shown fixed length strings. These are declared with the word \f(CW\*(C`string\*(C'\fR followed by the length of the string in parentheticals. Fixed length strings are included inside the record itself and do not need to be allocated or deallocated separately from the record. Variable length strings must be allocated on the heap, and thus require a sense of "ownership", that is whomever allocates variable length strings should be responsible for also free'ing them. To handle this, you can add a \f(CW\*(C`ro\*(C'\fR or \f(CW\*(C`rw\*(C'\fR trait to a string field. The default is \&\f(CW\*(C`ro\*(C'\fR, means that you can get, but not set its value: .PP .Vb 1 \& package Foo; \& \& record_layout_1( \& \*(Aqstring ro\*(Aq => \*(Aqbar\*(Aq, # same type as \*(Aqstring\*(Aq and \*(Aqstring_ro\*(Aq \& ); \& \& package main; \& \& my $foo = Foo\->new; \& \& my $string = $foo\->bar; # GOOD \& $foo\->bar("starscream"); # BAD .Ve .PP If you specify a field is \f(CW\*(C`rw\*(C'\fR, then you can set its value: .PP .Vb 1 \& package Foo; \& \& record_layout_1( \& \*(Aqstring rw\*(Aq => \*(Aqbar\*(Aq, # same type as \*(Aqstring_rw\*(Aq \& ); \& \& package main; \& \& my $foo = Foo\->new; \& \& my $string = $foo\->bar; # GOOD \& $foo\->bar("starscream"); # GOOD .Ve .PP Any string value that is pointed to by the record will be free'd when it falls out of scope, so you must be very careful that any \f(CW\*(C`string rw\*(C'\fR fields are not set or modified by C code. You should also take care not to copy any record that has a \f(CW\*(C`rw\*(C'\fR string in it because its values will be free'd twice! .PP .Vb 1 \& use Clone qw( clone ); \& \& my $foo2 = clone $foo; # BAD bar will be free\*(Aqd twice .Ve .PP \fIarrays\fR .IX Subsection "arrays" .PP Arrays of integer, floating points and opaque pointers are supported. .PP .Vb 1 \& package Foo; \& \& record_layout_1( \& \*(Aqint[10]\*(Aq => \*(Aqbar\*(Aq, \& ); \& \& my $foo = Foo\->new; \& \& $foo\->bar([1,2,3,4,5,6,7,8,9,10]); # sets the values for the array \& my $list = $foo\->bar; # returns a list reference \& \& $foo\->bar(5, \-6); # sets the 5th element in the array to \-6 \& my $item = $foo\->bar(5); gets the 5th element in the array .Ve .SS record_layout .IX Subsection "record_layout" .Vb 3 \& record_layout($ffi, $type => $name, ... ); \& record_layout(\e@ffi_args, $type => $name, ... ); \& record_layout($type => $name, ... ); .Ve .PP This function works like \f(CW\*(C`record_layout\*(C'\fR except that \&\f(CW\*(C`api => 0\*(C'\fR is used instead of \f(CW\*(C`api => 1\*(C'\fR. All new code should use \f(CW\*(C`record_layout_1\*(C'\fR instead. .SH CAVEATS .IX Header "CAVEATS" These useful features (and probably more) are missing, and unlikely to be added. .IP Unions 4 .IX Item "Unions" .PD 0 .IP "Nested records" 4 .IX Item "Nested records" .PD .PP If you need these features, consider using FFI::C instead. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP FFI::Platypus 4 .IX Item "FFI::Platypus" The main platypus documentation. .IP FFI::C 4 .IX Item "FFI::C" Another interface for constructing structured data. It includes support for \&\f(CW\*(C`union\*(C'\fR and array types (which this module does not), but lacks support for passing records by-value. .IP FFI::Platypus::Record::TieArray 4 .IX Item "FFI::Platypus::Record::TieArray" Tied array interface for record array members. .IP Convert::Binary::C 4 .IX Item "Convert::Binary::C" Another method for constructing and dissecting structured data records. .IP "pack and unpack" 4 .IX Item "pack and unpack" Built-in Perl functions for constructing and dissecting structured data records. .SH AUTHOR .IX Header "AUTHOR" Author: Graham Ollis .PP Contributors: .PP Bakkiaraj Murugesan (bakkiaraj) .PP Dylan Cali (calid) .PP pipcet .PP Zaki Mughal (zmughal) .PP Fitz Elliott (felliott) .PP Vickenty Fesunov (vyf) .PP Gregor Herrmann (gregoa) .PP Shlomi Fish (shlomif) .PP Damyan Ivanov .PP Ilya Pavlov (Ilya33) .PP Petr Písař (ppisar) .PP Mohammad S Anwar (MANWAR) .PP Håkon Hægland (hakonhagland, HAKONH) .PP Meredith (merrilymeredith, MHOWARD) .PP Diab Jerius (DJERIUS) .PP Eric Brine (IKEGAMI) .PP szTheory .PP José Joaquín Atria (JJATRIA) .PP Pete Houston (openstrike, HOUSTON) .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2015\-2022 by Graham Ollis. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.