How to set the CB from Haxe to CPP.

44 views
Skip to first unread message

Amit Bagga

unread,
Oct 17, 2016, 11:27:40 AM10/17/16
to Haxe
#include "common.h"
#include <hx/CFFI.h>

#ifndef CLASS_H
#define CLASS_H
class MY_LIBRARY_PUBLIC  Point
{
   int x,y;
   public:
   MY_LIBRARY_PUBLIC void set_points(int,int);
   MY_LIBRARY_PUBLIC int getX();
   MY_LIBRARY_PUBLIC int getY();     
};

class MY_LIBRARY_PUBLIC   Rectangle {
                int width, height;
                value *cb;
            public:
                MY_LIBRARY_PUBLIC  void set_values (int,int);
        MY_LIBRARY_PUBLIC  void set_point_values (Point *p);
                MY_LIBRARY_PUBLIC  int area(void);
                MY_LIBRARY_PUBLIC  void callBack(value);
};
#endif



CODE FROM CPP


void Rectangle::callBack(value f)
{
        cb = alloc_root();
        *cb=f;
 }




//@:structAccess
@:include("Rectangle.h")
@:native("Rectangle*")
@:keep
@:native("cpp::Reference")
extern class Rectangle
{

     public function set_values(w : Int, h : Int) : Void;
     public function set_point_values(p:Point) : Void;
     @navtive("area")public function area() : Int;
     public function callBack(f:Point->Void):Void;
     @:native("new Rectangle") public static function create() : Rectangle;
}


@:include("Rectangle.h")
@:native("Point*")
@:keep
@:native("cpp::Reference")
extern class Point
{

     public function set_points(w : Int, h : Int) : Void;
     public function getX() : Int;
     public function getY() : Int;
     @:native("new Point") public static function create() : Point;
}


  public function Callback(p:Int):Void {

        }

        public function new () {

                super ();

                //Math_extension.init();
                var rec = Rectangle.create();
                rec.callBack(Callback);

Error


./src/Main.cpp: In member function 'void Main_obj::__construct()':
./src/Main.cpp:27:49: error: conversion from 'Dynamic' to 'value {aka _value*}' is ambiguous
./src/Main.cpp:27:49: note: candidates are:
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:72:11: note: Dynamic::operator cpp::UInt64() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:72:11: note:   no known conversion for implicit 'this' parameter from 'cpp::UInt64 {aka long long unsigned int}' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:71:11: note: Dynamic::operator cpp::Int64() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:71:11: note:   no known conversion for implicit 'this' parameter from 'cpp::Int64 {aka long long int}' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:69:11: note: Dynamic::operator signed char() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:69:11: note:   no known conversion for implicit 'this' parameter from 'signed char' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:68:11: note: Dynamic::operator char() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:68:11: note:   no known conversion for implicit 'this' parameter from 'char' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:67:11: note: Dynamic::operator unsigned char() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:67:11: note:   no known conversion for implicit 'this' parameter from 'unsigned char' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:66:11: note: Dynamic::operator short unsigned int() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:66:11: note:   no known conversion for implicit 'this' parameter from 'short unsigned int' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:65:11: note: Dynamic::operator short int() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:65:11: note:   no known conversion for implicit 'this' parameter from 'short int' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:64:11: note: Dynamic::operator unsigned int() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:64:11: note:   no known conversion for implicit 'this' parameter from 'unsigned int' to 'value {aka _value*}'
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:63:11: note: Dynamic::operator int() const <near match>
/Users/3psoft/lib/haxelib/hxcpp/3,3,49/include/Dynamic.h:63:11: note:   no known conversion for implicit 'this' parameter from 'int' to 'value {aka _value*}'
/Users/3psoft/codebase/rnd/math_extension/project/include/Rectangle.h:22:41: error:   initializing argument 1 of 'void Rectangle::callBack(value)'

Hugh

unread,
Oct 18, 2016, 1:22:19 AM10/18/16
to Haxe
Hi
The problem here is that you are mixing CFFI and haxe together in the same file by indirectly including the hx/CFFI.h in haxe-gen code.
This was not really designed for this - CFFI was for code that did not rely on the hxcpp implementation (indeed, it could run on neko).  Although now I look at it, I might be able to alias 'value' to 'hx::Object *' and your example might work.

Other options depend on how much you want to your Rectangle class to "see" the hxcpp code.  If you are going to compile them together, at the same time, into a single exe, then  you could skip CFFI and include <hxcpp.h> directly.

So I have added an option - if you #defein HXCPP_NATIVE_CFFI_VALUE before #include<hx/CFFI.h>, it will provide stronger typing for 'value'.
If you use your externs to defined the callback  type as 'cpp.Object' then you will be able to pass your callback to it.
Not sure what the minimum version of haxe required for this is though.
Reply all
Reply to author
Forward
0 new messages