Callback to haxe function from extern cpp class

Just for the record and if someone is looking for this solution too how to pass a function pointer from haxe to c++ to receive a callback from an extern c++ class/library.

//Main.hx
 public static function callback():Bool
 {
 trace("callback from extern c++ lib");
 return true;
 }
 
 public function new()
 {
 var entry = Entry.create();
 var app = entry.value.startApplication( untyped __cpp__ ("callback") );
 }


//YourExternBindings.hx

extern class Entry
{
 @:native("new Entry")
 public static function create():cpp.Pointer<Entry>;
 public function startApplication(cb:cpp.Callable<Void->Bool>):cpp.Pointer<ExternApplication>;
}


//and on the cpp side

 void Entry::startApplication(bool (*cb)())
 {
               cb(); // calling back to haxe code
 }

I had to return bool or int because on the cpp side void != Void, float != Float (cannot cast Void to void error)

UPDATE: but this works

@:void public static function callback():Void

2 Likes