You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to find a way to pass an array of ::Struct based objects into a newly created ::Array. Here's an example of my goal based on the synopsis from FFI::C::Array:
use FFI::C::ArrayDef;
use FFI::C::StructDef;
my$point_def = FFI::C::StructDef->new(
name=>'point_t',
class=>'Point',
members=> [
x=>'double',
y=>'double',
],
);
my$rect_def = FFI::C::ArrayDef->new(
name=>'rectangle_t',
class=>'Rectangle',
members=> [
$point_def, 2,
]
);
# create a rectangle using the def's create methodmy$square = $rect_def->create([
Point->new( {x=> 1.0, y=> 1.0} ), # Obtained somehow as full objects...
Point->new( {x=> 2.0, y=> 2.0} ) # ...rather than hash references
]);
printf"[[%d%d][%d%d]]\n",
$square->[0]->x, $square->[0]->y,
$square->[1]->x, $square->[1]->y; # [[1 1][2 2]]
In my project, I would have these Point objects returned from C by yet another attached function. My only solution to FFI::C::Util::perl_to_c(...) croaking on this is to map { ... } the objects back to plain hash references and let FFI::C::Def instantiate brand new objects with the exposed data (this would break on complex C objects). Before I start looking for a place to do this quickly and cleanly enough for a PR (maybe), am I missing something obvious?
The text was updated successfully, but these errors were encountered:
What you want to do is pass in an array of hash references to the create $rect_def->create call like so:
# create a rectangle using the def's create methodmy$square = $rect_def->create([
{x=> 1.0, y=> 1.0}, # Obtained somehow as full objects...
{x=> 2.0, y=> 2.0} # ...rather than hash references
]);
This saves you from explicitly having to call perl_to_c. Then the script works:
% perl foo.pl
[[1 1][2 2]]
You unfortunately cannot build up an C array from existing point objects because in C an array is contiguous memory, and the Point objects would have to be copied (I think doing an implicit copy in this situation would be a confusing interface). You can still get the point objects once the array has been created though:
A copy from an existing object could be a useful thing to do, but if that really is what you are trying to do I think we want to think about how to work the interface so that it is clear that is what is happening.
I'm trying to find a way to pass an array of ::Struct based objects into a newly created ::Array. Here's an example of my goal based on the synopsis from FFI::C::Array:
In my project, I would have these
Point
objects returned from C by yet another attached function. My only solution toFFI::C::Util::perl_to_c(...)
croaking on this is tomap { ... }
the objects back to plain hash references and let FFI::C::Def instantiate brand new objects with the exposed data (this would break on complex C objects). Before I start looking for a place to do this quickly and cleanly enough for a PR (maybe), am I missing something obvious?The text was updated successfully, but these errors were encountered: