| 1 |
package FFI::Platypus::Constant; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use constant 1.32 (); |
| 7 |
use FFI::Platypus; |
| 8 |
|
| 9 |
# ABSTRACT: Define constants in C space for Perl |
| 10 |
our $VERSION = '2.12'; # VERSION |
| 11 |
|
| 12 |
|
| 13 |
{ |
| 14 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 15 |
$ffi->bundle; |
| 16 |
|
| 17 |
$ffi->type( 'opaque' => 'ffi_platypus_constant_t' ); |
| 18 |
$ffi->type( '(string,string)->void' => 'set_str_t' ); |
| 19 |
$ffi->type( '(string,sint64)->void' => 'set_sint_t' ); |
| 20 |
$ffi->type( '(string,uint64)->void' => 'set_uint_t' ); |
| 21 |
$ffi->type( '(string,double)->void' => 'set_double_t' ); |
| 22 |
|
| 23 |
$ffi->mangler(sub { |
| 24 |
my($name) = @_; |
| 25 |
$name =~ s/^/ffi_platypus_constant__/; |
| 26 |
$name; |
| 27 |
}); |
| 28 |
|
| 29 |
$ffi->attach( new => [ 'set_str_t', 'set_sint_t', 'set_uint_t', 'set_double_t' ] => 'ffi_platypus_constant_t' => sub { |
| 30 |
my($xsub, $class, $default_package) = @_; |
| 31 |
my $f = $ffi->closure(sub { |
| 32 |
my($name, $value) = @_; |
| 33 |
if($name !~ /::/) |
| 34 |
{ |
| 35 |
$name = join('::', $default_package, $name); |
| 36 |
} |
| 37 |
constant->import($name, $value); |
| 38 |
}); |
| 39 |
|
| 40 |
bless { |
| 41 |
ptr => $xsub->($f, $f, $f, $f), |
| 42 |
f => $f, |
| 43 |
}, $class; |
| 44 |
}); |
| 45 |
|
| 46 |
$ffi->attach( DESTROY => ['ffi_platypus_constant_t'] => 'void' => sub { |
| 47 |
my($xsub, $self) = @_; |
| 48 |
$xsub->($self->ptr); |
| 49 |
}); |
| 50 |
|
| 51 |
sub ptr { shift->{ptr} } |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
1; |
| 56 |
|
| 57 |
__END__ |
| 58 |
|
| 59 |
=pod |
| 60 |
|
| 61 |
=encoding UTF-8 |
| 62 |
|
| 63 |
=head1 NAME |
| 64 |
|
| 65 |
FFI::Platypus::Constant - Define constants in C space for Perl |
| 66 |
|
| 67 |
=head1 VERSION |
| 68 |
|
| 69 |
version 2.12 |
| 70 |
|
| 71 |
=head1 SYNOPSIS |
| 72 |
|
| 73 |
C<ffi/foo.c>: |
| 74 |
|
| 75 |
#include <ffi_platypus_bundle.h> |
| 76 |
|
| 77 |
void |
| 78 |
ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c) |
| 79 |
{ |
| 80 |
c->set_str("FOO", "BAR"); /* sets $package::FOO to "BAR" */ |
| 81 |
c->set_str("ABC::DEF", "GHI"); /* sets ABC::DEF to GHI */ |
| 82 |
} |
| 83 |
|
| 84 |
C<lib/Foo.pm>: |
| 85 |
|
| 86 |
package Foo; |
| 87 |
|
| 88 |
use strict; |
| 89 |
use warnings; |
| 90 |
use FFI::Platypus 2.00; |
| 91 |
use Exporter qw( import ); |
| 92 |
|
| 93 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 94 |
# sets constants Foo::FOO and ABC::DEF from C |
| 95 |
$ffi->bundle; |
| 96 |
|
| 97 |
1; |
| 98 |
|
| 99 |
=head1 DESCRIPTION |
| 100 |
|
| 101 |
The Platypus bundle interface (see L<FFI::Platypus::Bundle>) has an entry point |
| 102 |
C<ffi_pl_bundle_constant> that lets you define constants in Perl space from C. |
| 103 |
|
| 104 |
void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c); |
| 105 |
|
| 106 |
The first argument C<package> is the name of the Perl package. The second argument |
| 107 |
C<c> is a struct with function pointers that lets you define constants of different |
| 108 |
types. The first argument for each function is the name of the constant and the |
| 109 |
second is the value. If C<::> is included in the constant name then it will be |
| 110 |
defined in that package space. If it isn't then the constant will be defined in |
| 111 |
whichever package called C<bundle>. |
| 112 |
|
| 113 |
=over 4 |
| 114 |
|
| 115 |
=item set_str |
| 116 |
|
| 117 |
c->set_str(name, value); |
| 118 |
|
| 119 |
Sets a string constant. |
| 120 |
|
| 121 |
=item set_sint |
| 122 |
|
| 123 |
c->set_sint(name, value); |
| 124 |
|
| 125 |
Sets a 64-bit signed integer constant. |
| 126 |
|
| 127 |
=item set_uint |
| 128 |
|
| 129 |
c->set_uint(name, value); |
| 130 |
|
| 131 |
Sets a 64-bit unsigned integer constant. |
| 132 |
|
| 133 |
=item set_double |
| 134 |
|
| 135 |
c->set_double(name, value); |
| 136 |
|
| 137 |
Sets a double precision floating point constant. |
| 138 |
|
| 139 |
=back |
| 140 |
|
| 141 |
=head2 Example |
| 142 |
|
| 143 |
Suppose you have a header file C<myheader.h>: |
| 144 |
|
| 145 |
#ifndef MYHEADER_H |
| 146 |
#define MYHEADER_H |
| 147 |
|
| 148 |
#define MYVERSION_STRING "1.2.3" |
| 149 |
#define MYVERSION_MAJOR 1 |
| 150 |
#define MYVERSION_MINOR 2 |
| 151 |
#define MYVERSION_PATCH 3 |
| 152 |
|
| 153 |
enum { |
| 154 |
MYBAD = -1, |
| 155 |
MYOK = 1 |
| 156 |
}; |
| 157 |
|
| 158 |
#define MYPI 3.14 |
| 159 |
|
| 160 |
#endif |
| 161 |
|
| 162 |
You can define these constants from C: |
| 163 |
|
| 164 |
#include <ffi_platypus_bundle.h> |
| 165 |
#include "myheader.h" |
| 166 |
|
| 167 |
void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c) |
| 168 |
{ |
| 169 |
c->set_str("MYVERSION_STRING", MYVERSION_STRING); |
| 170 |
c->set_uint("MYVERSION_MAJOR", MYVERSION_MAJOR); |
| 171 |
c->set_uint("MYVERSION_MINOR", MYVERSION_MINOR); |
| 172 |
c->set_uint("MYVERSION_PATCH", MYVERSION_PATCH); |
| 173 |
c->set_sint("MYBAD", MYBAD); |
| 174 |
c->set_sint("MYOK", MYOK); |
| 175 |
c->set_double("MYPI", MYPI); |
| 176 |
} |
| 177 |
|
| 178 |
Your Perl code doesn't have to do anything when calling bundle: |
| 179 |
|
| 180 |
package Const; |
| 181 |
|
| 182 |
use strict; |
| 183 |
use warnings; |
| 184 |
use FFI::Platypus 2.00; |
| 185 |
|
| 186 |
{ |
| 187 |
my $ffi = FFI::Platypus->new( api => 2 ); |
| 188 |
$ffi->bundle; |
| 189 |
} |
| 190 |
|
| 191 |
1; |
| 192 |
|
| 193 |
=head1 AUTHOR |
| 194 |
|
| 195 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 196 |
|
| 197 |
Contributors: |
| 198 |
|
| 199 |
Bakkiaraj Murugesan (bakkiaraj) |
| 200 |
|
| 201 |
Dylan Cali (calid) |
| 202 |
|
| 203 |
pipcet |
| 204 |
|
| 205 |
Zaki Mughal (zmughal) |
| 206 |
|
| 207 |
Fitz Elliott (felliott) |
| 208 |
|
| 209 |
Vickenty Fesunov (vyf) |
| 210 |
|
| 211 |
Gregor Herrmann (gregoa) |
| 212 |
|
| 213 |
Shlomi Fish (shlomif) |
| 214 |
|
| 215 |
Damyan Ivanov |
| 216 |
|
| 217 |
Ilya Pavlov (Ilya33) |
| 218 |
|
| 219 |
Petr Písař (ppisar) |
| 220 |
|
| 221 |
Mohammad S Anwar (MANWAR) |
| 222 |
|
| 223 |
Håkon Hægland (hakonhagland, HAKONH) |
| 224 |
|
| 225 |
Meredith (merrilymeredith, MHOWARD) |
| 226 |
|
| 227 |
Diab Jerius (DJERIUS) |
| 228 |
|
| 229 |
Eric Brine (IKEGAMI) |
| 230 |
|
| 231 |
szTheory |
| 232 |
|
| 233 |
José Joaquín Atria (JJATRIA) |
| 234 |
|
| 235 |
Pete Houston (openstrike, HOUSTON) |
| 236 |
|
| 237 |
Lukas Mai (MAUKE) |
| 238 |
|
| 239 |
=head1 COPYRIGHT AND LICENSE |
| 240 |
|
| 241 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 242 |
|
| 243 |
This is free software; you can redistribute it and/or modify it under |
| 244 |
the same terms as the Perl 5 programming language system itself. |
| 245 |
|
| 246 |
=cut |