COMMUNITY

Is there a way to declare an array of primitives (for cpp extern)? Unfortunately, haxe.ds.Vector doesnt work for some reason

Hello!

Let’s say I have following C function: void log(char *line) // real native code and I couldn’t do anything about it
I need to pass – char buffer[1024] to it. How do I declare said buffer variable such that haxe would generate the right code?

I tried new Vector<Char>(1024) but it failed with casting error. Then I came up with helper:

class CharArray {
@:extern
inline public static function create(size: Int32): RawPointer<Char> {
untyped __cpp__("char buffer[{0}]", size);
return untyped __cpp__("buffer");
}
}

It is working as expected but looks ugly. I think, there should be more elegant solution.

I ended up with another funky solution :laughing:

Lets say you need to use av_make_error_string (it won’t compile with av_err2str for some reason).

Extern:

@:native("av_make_error_string")
static function makeErrorString(errbuf: RawConstPointer<Char>, errbuf_size: SizeT, errnum: Int32): RawPointer<Char>;

Usecase:

@:array(AV_ERROR_MAX_STRING_SIZE)
final error_string: RawPointer<Char> = null;

Av.makeErrorString(error_string, AV_ERROR_MAX_STRING_SIZE, error_code);

Still wondering if there may be a better way.

Yup … that’s pretty funky … :smiley: