Hello,

I'm trying to encapsulate a C struct, one member of which is an array of
pointers to structs, and I'm having some problems to figure out how to do
it.

I tried to follow the hint in the documentation
https://docs.perl6.org/language/nativecall#Structs

use NativeCall;

class A is repr('CStruct') {
  has uint8 $.u8;
}

class B is repr('CStruct') {
  has CArray[A] $.a;
  submethod TWEAK {
    my $arr := CArray[A].new;
    for ^5 { $arr[$_] = A.new(u8 => $_) }
    $!a := $arr;
  }
}

sub MAIN
{
  my B $b .= new;
  say $b.a[2];
  say 'Size of $b: ' ~ nativesizeof($b);
}

but this way the resulting size is of one pointer, 8 bytes (on my
computer), while what I'm trying to get is a size of 5 pointers, 40 bytes.

Obviously I can do this:

use NativeCall;

class A is repr('CStruct') {
  has uint8 $.u8;
}

class C is repr('CStruct') {
  has A $.a1;
  has A $.a2;
  has A $.a3;
  has A $.a4;
  has A $.a5;
}

sub MAIN
{
  my C $c .= new;
  say nativesizeof($c);
}

which returns the size I need. Yet this solution hurts my feelings :-)

Is there any other way to do it, without resorting to this:

https://stackoverflow.com/questions/43544931/passing-an-array-of-structures-to-a-perl-6-nativecall-function

(Which is presented as "enough rope to -hang yourself- *build a workaround*
")

-- 
Fernando Santagata

Reply via email to