| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use FFI::Platypus 2.00; |
| 4 |
|
| 5 |
my $ffi = FFI::Platypus->new( |
| 6 |
api => 2, |
| 7 |
lib => './color.so' |
| 8 |
); |
| 9 |
|
| 10 |
package Color { |
| 11 |
|
| 12 |
use FFI::Platypus::Record; |
| 13 |
use overload |
| 14 |
'""' => sub { shift->as_string }, |
| 15 |
bool => sub { 1 }, fallback => 1; |
| 16 |
|
| 17 |
record_layout_1($ffi, |
| 18 |
'string(8)' => 'name', qw( |
| 19 |
uint8 red |
| 20 |
uint8 green |
| 21 |
uint8 blue |
| 22 |
)); |
| 23 |
|
| 24 |
sub as_string { |
| 25 |
my($self) = @_; |
| 26 |
sprintf "%s: [red:%02x green:%02x blue:%02x]", |
| 27 |
$self->name, $self->red, $self->green, $self->blue; |
| 28 |
} |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
$ffi->type('record(Color)' => 'color_t'); |
| 33 |
$ffi->attach( color_increase_red => ['color_t','uint8'] => 'color_t' ); |
| 34 |
|
| 35 |
my $gray = Color->new( |
| 36 |
name => 'gray', |
| 37 |
red => 0xDC, |
| 38 |
green => 0xDC, |
| 39 |
blue => 0xDC, |
| 40 |
); |
| 41 |
|
| 42 |
my $slightly_red = color_increase_red($gray, 20); |
| 43 |
|
| 44 |
print "$gray\n"; |
| 45 |
print "$slightly_red\n"; |