Dyn

 view release on metacpan or  search on metacpan

lib/Dyn/Callback.pm  view on Meta::CPAN

package Dyn::Callback 0.05 {
    use strict;
    use warnings;
    use 5.030;
    use XSLoader;
    XSLoader::load( __PACKAGE__, our $VERSION );
    use parent 'Exporter';
    our %EXPORT_TAGS;
    @{ $EXPORT_TAGS{all} } = our @EXPORT_OK = map { @{ $EXPORT_TAGS{$_} } } keys %EXPORT_TAGS;
};
1;
__END__

=encoding utf-8

=head1 NAME

Dyn::Callback - Perl Code as FFI Callbacks

=head1 SYNOPSIS

    use Dyn::Callback qw[:all];
    use Dyn::Load;
    use Dyn::Call qw[DC_CALL_C_DEFAULT];
    my $lib = Dyn::Load::dlLoadLibrary('path/to/lib.so');
    my $ptr = Dyn::Load::dlFindSymbol( $lib, 'timer' );
    my $cvm = dcNewCallVM(1024);
    Dyn::Call::dcMode( $cvm, DC_CALL_C_DEFAULT );
    Dyn::Call::dcReset($cvm);
    my $cb = dcbNewCallback(       # Accepts an int and returns an int
        'i)i',
        sub {
            my ($cb, $args, $result, $userdata) = @_;
            ...;                   # do something
            return 'i';
        },
        5
    );
    Dyn::Call::dcArgPointer( $cvm, $cb );    # pass callbacks as pointers
    Dyn::Call::dcCallVoid( $cvm, $ptr );     # your timer() function returns void

=head1 DESCRIPTION

Dyn::Callback is an interface to create callback objects that can be passed to
functions as callback arguments. In other words, a pointer to the callback
object can be "called" directly from the foreign library.

=head1 Functions

These may be imported by name or called directly.

=head2 C<dcbNewCallback( $signature, $coderef, $userdata )>

Creates a new callback object, where C<$signature> is a signature string
describing the function.

    my $pcb = dcbNewCallback(
        'i)i',
        sub {
            my ($cb, $args, $result, $userdata) = @_;
            ...;
            return 'i';
        },
        5
    );

Expected parameters include:

=over

=item C<signature> - string describing any parameters and return value

This is needed for dyncallback dyncallback to correctly prepare the arguments
passed in by the function that calls the callback handler.

=item C<code> - a code reference

Note that the code reference doesn't return the value specified in the
signature, directly, but a signature character, specifying the return value's
type. The return value itself is stored where the callback's 3rd parameter
points to (see below).

=item C<userdata> - optional, arbitrary data

This data, if defined, is passed back to the given coderef as the 4th
parameter.

=back

=head2 C<dcbInitCallback( ... )>

Initialize (or reinitialize) the callback object.

    dcbInitCallback( $pcb, 'i)Z', sub { ...; }, undef );

Expected parameters include:

=over



( run in 0.553 second using v1.01-cache-2.11-cpan-39bf76dae61 )