FFI-Platypus-Declare

 view release on metacpan or  search on metacpan

lib/FFI/Platypus/Declare.pm  view on Meta::CPAN

like this:

 foo(closure { ... });         # BAD

Perl will not see any references to it and try to free it immediately.
(this has to do with the way Perl and C handle responsibilities for
memory allocation differently).  One fix for this is to make sure the
closure remains in scope using either C<my> or C<our>.  If you know the
closure will need to remain in existence for the life of the process (or
if you do not care about leaking memory), then you can add the sticky
keyword to tell L<FFI::Platypus> to keep the thing in memory.

 foo(sticky closure { ... });  # OKAY

=head2 cast

 my $converted_value = cast $original_type, $converted_type, $original_value;

The C<cast> function converts an existing I<$original_value> of type
I<$original_type> into one of type I<$converted_type>.  Not all types
are supported, so care must be taken.  For example, to get the address
of a string, you can do this:

 my $address = cast 'string' => 'opaque', $string_value;

=head2 attach_cast

 attach_cast "cast_name", $original_type, $converted_type;
 my $converted_value = cast_name($original_value);

This function creates a subroutine which can be used to convert
variables just like the L<cast|FFI::Platypus::Declare#cast> function
above.  The above synopsis is roughly equivalent to this:

 sub cast_name { cast($original_type, $converted_type, $_[0]) }
 my $converted_value = cast_name($original_value);

Except that the L<attach_cast|FFI::Platypus::Declare#attach_cast>
variant will be much faster if called multiple times since the cast does
not need to be dynamically allocated on each instance.

=head2 sizeof

 my $size = sizeof $type;

Returns the total size of the given type.  For example to get the size
of an integer:

 my $intsize = sizeof 'int'; # usually 4 or 8 depending on platform

You can also get the size of arrays

 my $intarraysize = sizeof 'int[64]';

Keep in mind that "pointer" types will always be the pointer / word size
for the platform that you are using.  This includes strings, opaque and
pointers to other types.

This function is not very fast, so you might want to save this value as
a constant, particularly if you need the size in a loop with many
iterations.

=head2 lang

 lang $language;

Specifies the foreign language that you will be interfacing with. The
default is C.  The foreign language specified with this attribute
changes the default native types (for example, if you specify
L<Rust|FFI::Platypus::Lang::Rust>, you will get C<i32> as an alias for
C<sint32> instead of C<int> as you do with L<C|FFI::Platypus::Lang::C>).

In the future this may attribute may offer hints when doing demangling
of languages that require it like L<C++|FFI::Platypus::Lang::CPP>.

=head2 abi

 abi $abi;

Set the ABI or calling convention for use in subsequent calls
to L</attach>.  May be either a string name or integer value
from L<FFI::Platypus#abis>.

=head1 SEE ALSO

=over 4

=item L<FFI::Platypus>

Object oriented interface to Platypus.

=item L<FFI::Platypus::Type>

Type definitions for Platypus.

=item L<FFI::Platypus::API>

Custom types API for Platypus.

=item L<FFI::Platypus::Memory>

memory functions for FFI.

=item L<FFI::CheckLib>

Find dynamic libraries in a portable way.

=item L<FFI::TinyCC>

JIT compiler for FFI.

=back

=head1 AUTHOR

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

Contributors:

Carlos D. Álvaro (cdalvaro)



( run in 1.426 second using v1.01-cache-2.11-cpan-8dfa8b56332 )