Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping external library #56

Open
henrikrudstrom opened this issue Dec 19, 2016 · 9 comments
Open

Wrapping external library #56

henrikrudstrom opened this issue Dec 19, 2016 · 9 comments

Comments

@henrikrudstrom
Copy link

henrikrudstrom commented Dec 19, 2016

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

@subwaystation
Copy link

subwaystation commented Jan 2, 2017

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.

@henrikrudstrom
Copy link
Author

Thanks, i will give it a try!

@JoyBoy-369
Copy link

JoyBoy-369 commented Feb 1, 2017

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?

@subwaystation
Copy link

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.

@JoyBoy-369
Copy link

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.

@parro-it
Copy link
Contributor

parro-it commented Feb 1, 2017

@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...

@JoyBoy-369
Copy link

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.

@parro-it
Copy link
Contributor

parro-it commented Feb 1, 2017

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...

@JoyBoy-369
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants