nbind icon indicating copy to clipboard operation
nbind copied to clipboard

Wrapping external library

Open henrikrudstrom opened this issue 8 years ago • 9 comments

Not sure if this is an issue or just a newbie question, but im testing nbind to see if i can use it for my wrapper project. I tried following the example for wrapping the 'vg' library but i get some errors, not sure if they are due to the implementation of the library im trying to wrap or something missing in nbind or my own incompetence. This is my code:

...
#include <Geom_Point.hxx>
#include <Geom_CartesianPoint.hxx>

#include "nbind/api.h"

class Point : public Geom_CartesianPoint {
  public:
    Point(Standard_Real x, Standard_Real y, Standard_Real z) : Geom_CartesianPoint(x, y, z) {}
    Point(gp_Pnt pnt) : Geom_CartesianPoint(pnt) {}
};

#include "nbind/nbind.h"

NBIND_CLASS(Point, Geom_CartesianPoint){
  inherit(Geom_CartesianPoint);
  construct<Standard_Real, Standard_Real, Standard_Real>();
  construct<gp_Pnt>();
  method(SetX);
}

when i compile i get an error when i try to wrap the inherited method SetX

make: Entering directory '/home/henrik/Development/noce/noce-nbind/build'
  CXX(target) Release/obj.target/nbind/hello.o
In file included from ../hello.cc:148:0:
../hello.cc: In instantiation of ‘BindInvokerGeom_CartesianPoint<Bound>::BindInvokerGeom_CartesianPoint() [with Bound = Point]’:
../hello.cc:156:1:   required from here
../node_modules/nbind/include/nbind/nbind.h:56:27: error: no matching function for call to ‘nbind::BindDefiner<Point>::method(const char [5], void (Geom_CartesianPoint::*)(Standard_Real))’
 #define method(name, ...) definer.method(#name, &Bound::name, ## __VA_ARGS__)
                           ^
../hello.cc:160:3: note: in expansion of macro ‘method’
   method(SetX);
   ^
In file included from ../node_modules/nbind/include/nbind/nbind.h:8:0,
                 from ../hello.cc:148:
../node_modules/nbind/include/nbind/BindDefiner.h:180:15: note: candidate: template<class ReturnType, class ... Args, class ... Policies> nbind::BindDefiner<Bound>& nbind::BindDefiner<Bound>::method(const char*, ReturnType (*)(Args ...), Policies ...) [with ReturnType = ReturnType; Args = {Args ...}; Policies = {Policies ...}; Bound = Point]
  BindDefiner &method(
               ^
../node_modules/nbind/include/nbind/BindDefiner.h:180:15: note:   template argument deduction/substitution failed:
In file included from ../hello.cc:148:0:
../node_modules/nbind/include/nbind/nbind.h:56:27: note:   mismatched types ‘ReturnType (*)(Args ...)’ and ‘void (Geom_CartesianPoint::*)(Standard_Real) {aka void (Geom_CartesianPoint::*)(double)}’
 #define method(name, ...) definer.method(#name, &Bound::name, ## __VA_ARGS__)
                           ^
../hello.cc:160:3: note: in expansion of macro ‘method’
   method(SetX);
   ^
In file included from ../node_modules/nbind/include/nbind/nbind.h:8:0,
                 from ../hello.cc:148:
../node_modules/nbind/include/nbind/BindDefiner.h:202:15: note: candidate: template<class MethodType, class ... Policies> nbind::BindDefiner<Bound>& nbind::BindDefiner<Bound>::method(const char*, MethodType Bound::*, Policies ...) [with MethodType = MethodType; Policies = {Policies ...}; Bound = Point]
  BindDefiner &method(
               ^
../node_modules/nbind/include/nbind/BindDefiner.h:202:15: note:   template argument deduction/substitution failed:
In file included from ../hello.cc:148:0:
../node_modules/nbind/include/nbind/nbind.h:56:27: note:   mismatched types ‘Point’ and ‘Geom_CartesianPoint’
 #define method(name, ...) definer.method(#name, &Bound::name, ## __VA_ARGS__)
                           ^
../hello.cc:160:3: note: in expansion of macro ‘method’
   method(SetX);

the documentation for the Geom_CartesianPoint is here: https://www.opencascade.com/doc/occt-6.9.1/refman/html/class_geom___cartesian_point.html

Anything im doing wrong here? any help would be greatly appreciated

henrikrudstrom avatar Dec 19 '16 23:12 henrikrudstrom

If I understand you correctly, then you are trying to bind the SetX method of class Geom_CartesianPoint. First of all, I would recommend that you split your class into description / definition and into body / declaration as in the example stackoverflow.com/questions/9880642/c-class-methods. Then you write your bindings into your .cpp file. I would suggest the following:

...
#include "nbind/nbind.h"

// as you are inheriting from Geom_CartesianPoint, you have to bind to it, too
NBIND_CLASS(Geom_CartesianPoint) {
  method(SetX); // I would bind SetX here
}

NBIND_CLASS(Point){ // leave out Geom_CartesianPoint here, the next line already takes care of proper inheritance
  inherit(Geom_CartesianPoint);
  construct<Standard_Real, Standard_Real, Standard_Real>();
  construct<gp_Pnt>();
}

I am not 100% sure where to bind the SetX method, but I think this could work. As I am not a C++ expert either, I am not quite sure, but maybe your inheritance is not done correctly in class Point.

subwaystation avatar Jan 02 '17 10:01 subwaystation

Thanks, i will give it a try!

henrikrudstrom avatar Jan 11 '17 02:01 henrikrudstrom

Hi,

In the above code does the Gem_Point and Gem_CartesianPoint headers files need to be bounded as well? Or can I just write a binding file like above which uses them directly?

JoyBoy-369 avatar Feb 01 '17 07:02 JoyBoy-369

Hi, I think with nbind you can not bind header files. What you can bind is classes or functions, etc. However, in the example above you should make sure that you included the necessary header files for Gem_Point and Gem_CartesianPoint. Then you can bind whatever functions and classes you want which are present in the included headers.

subwaystation avatar Feb 01 '17 11:02 subwaystation

Oh alright. Apologies for my ignorance. I'm really new to C++ and I'm trying to use ITK in electron. I was just wondering whether I have to write bindings for every source file in the library or would just writing bindings for the class, that I'm interested in would be enough? I'm really sorry if I'm not clear enough.

JoyBoy-369 avatar Feb 01 '17 11:02 JoyBoy-369

@SevenSinS02 you can give a shot at https://www.npmjs.com/package/node-itk. Never used it, but at least it could be of help...

parro-it avatar Feb 01 '17 11:02 parro-it

Ah thank you @parro-it. However, that is a private repo. I wanted to know if partial binding was possible. Like bind some classes using nbind and leaving the rest as it is.

JoyBoy-369 avatar Feb 01 '17 12:02 JoyBoy-369

Like bind some classes using nbind and leaving the rest as it is.

It should be possible, try use NBIND_CLASS just on the classes you need to use on js side...

parro-it avatar Feb 01 '17 12:02 parro-it

Ok cool. Let me try it and see if that works. Thanks a bunch @parro-it and @subwaystation.

JoyBoy-369 avatar Feb 01 '17 12:02 JoyBoy-369