1package FFI::Platypus::Type::PointerSizeBuffer;
2
3use strict;
4use warnings;
5use 5.008004;
6use FFI::Platypus;
7use FFI::Platypus::API qw(
8  arguments_set_pointer
9  arguments_set_uint32
10  arguments_set_uint64
11);
12use FFI::Platypus::Buffer qw( scalar_to_buffer );
13use FFI::Platypus::Buffer qw( buffer_to_scalar );
14
15# ABSTRACT: Convert string scalar to a buffer as a pointer / size_t combination
16our $VERSION = '1.56'; # VERSION
17
18
19my @stack;
20
21*arguments_set_size_t
22  = FFI::Platypus->new( api => 1 )->sizeof('size_t') == 4
23  ? \&arguments_set_uint32
24  : \&arguments_set_uint64;
25
26sub perl_to_native
27{
28  my($pointer, $size) = scalar_to_buffer($_[0]);
29  push @stack, [ $pointer, $size ];
30  arguments_set_pointer $_[1], $pointer;
31  arguments_set_size_t($_[1]+1, $size);
32}
33
34sub perl_to_native_post
35{
36  my($pointer, $size) = @{ pop @stack };
37  $_[0] = buffer_to_scalar($pointer, $size);
38}
39
40sub ffi_custom_type_api_1
41{
42  {
43    native_type         => 'opaque',
44    perl_to_native      => \&perl_to_native,
45    perl_to_native_post => \&perl_to_native_post,
46    argument_count      => 2,
47  }
48}
49
501;
51
52__END__
53
54=pod
55
56=encoding UTF-8
57
58=head1 NAME
59
60FFI::Platypus::Type::PointerSizeBuffer - Convert string scalar to a buffer as a pointer / size_t combination
61
62=head1 VERSION
63
64version 1.56
65
66=head1 SYNOPSIS
67
68In your C code:
69
70 void
71 function_with_buffer(void *pointer, size_t size)
72 {
73   ...
74 }
75
76In your Platypus::FFI code:
77
78 use FFI::Platypus;
79
80 my $ffi = FFI::Platypus->new( api => 1 );
81 $ffi->load_custom_type('::PointerSizeBuffer' => 'buffer');
82
83 $ffi->attach(function_with_buffer => ['buffer'] => 'void');
84 my $string = "content of buffer";
85 function_with_buffer($string);
86
87=head1 DESCRIPTION
88
89A common pattern in C code is to pass in a region of memory as a buffer,
90consisting of a pointer and a size of the memory region.  In Perl,
91string scalars also point to a contiguous series of bytes that has a
92size, so when interfacing with C libraries it is handy to be able to
93pass in a string scalar as a pointer / size buffer pair.
94
95=head1 SEE ALSO
96
97=over 4
98
99=item L<FFI::Platypus>
100
101Main Platypus documentation.
102
103=item L<FFI::Platypus::Type>
104
105Platypus types documentation.
106
107=back
108
109=head1 AUTHOR
110
111Author: Graham Ollis E<lt>plicease@cpan.orgE<gt>
112
113Contributors:
114
115Bakkiaraj Murugesan (bakkiaraj)
116
117Dylan Cali (calid)
118
119pipcet
120
121Zaki Mughal (zmughal)
122
123Fitz Elliott (felliott)
124
125Vickenty Fesunov (vyf)
126
127Gregor Herrmann (gregoa)
128
129Shlomi Fish (shlomif)
130
131Damyan Ivanov
132
133Ilya Pavlov (Ilya33)
134
135Petr Písař (ppisar)
136
137Mohammad S Anwar (MANWAR)
138
139Håkon Hægland (hakonhagland, HAKONH)
140
141Meredith (merrilymeredith, MHOWARD)
142
143Diab Jerius (DJERIUS)
144
145Eric Brine (IKEGAMI)
146
147szTheory
148
149José Joaquín Atria (JJATRIA)
150
151Pete Houston (openstrike, HOUSTON)
152
153=head1 COPYRIGHT AND LICENSE
154
155This software is copyright (c) 2015,2016,2017,2018,2019,2020 by Graham Ollis.
156
157This is free software; you can redistribute it and/or modify it under
158the same terms as the Perl 5 programming language system itself.
159
160=cut
161