I want to be able to use a double pointer in a class with REPR CStruct/CPointer:
typedef struct CipherContext {
void *cipher;
const uint8_t *key;
size_t key_len;
const uint8_t *path;
size_t path_len;
size_t block_size;
void *handle;
int (*cipher_init)(void **, const uint8_t *, size_t);
int (*cipher_encode)(void *, const uint8_t *, uint8_t *, size_t);
int (*cipher_decode)(void *, const uint8_t *, uint8_t *, size_t);
void (*cipher_free)(void *);
const uint8_t *(*cipher_strerror)(int);
} CipherContext;
int cipher_context_init(CipherContext **, const uint8_t *, size_t, const uint8_t *, size_t, size_t);
int cipher_context_encode(CipherContext *, const uint8_t *, uint8_t *, size_t);
int cipher_context_decode(CipherContext *, const uint8_t *, uint8_t *, size_t);
void cipher_context_free(CipherContext *);
const uint8_t *cipher_context_strerror(int);
Perl 6 code:
method new(Blob :$key!, Str :$path!, Int :$block-size!) {
my Pointer[::?CLASS] $ptr .= new;
my Int $err = cipher_context_init($ptr, $key, $key.elems, $path, $path.codes, $block-size);
return $ptr.deref unless $err;
my Str $errstr = cipher_context_strerror($err) || do {
my &cipher-strerror = nativecast(:(int32 --> Str), $!cipher-strerror);
cipher-strerror($err)
};
die "Failed to initialize cipher context: $errstr";
}
submethod DESTROY() {
cipher_context_free(self)
}
Short golf:
use v6.d;
use Nativecall;
class Foo is repr('CPointer') {
my Pointer[::?CLASS] $foo .= nw;
}
Only I can't figure out how to do it due to a bug in Rakudo. Is there a better way I could be handling errors in the C portion of the code (which is why I'm writing it like this)?