C++ class embind, passing object pointer argument

927 views
Skip to first unread message

guillaume...@gmail.com

unread,
Nov 19, 2014, 10:07:57 AM11/19/14
to emscripte...@googlegroups.com
Hi,

I try to pass object pointer argument to method of an embind class. The objet pointer argument is not a embind class.
Here a example of code :

#include <emscripten.h>
#include <bind.h>

using namespace emscripten;


 
class A {
     
public:
          A
() {
              a
=5;
         
}
         
void add() {
              a
= a+1;
         
}
         
int get() {
             
return a;
         
}
     
private:
         
int a;
 
};

 
class B {
     
public:
          B
() {
              b
=10;
         
}
         
void add() {
              b
= b+1;
         
}
         
int get() {
             
return b;
         
}

          A
* getA() {
             
return new A;
         
}

         
void addA(A* a) {
              b
+=a->get();
         
}
     
private:
         
int b;
 
};


EMSCRIPTEN_BINDINGS
(test) {
    class_
<B>("B")
         
.constructor<>()
         
.function("add",&B::add)
         
.function("get",&B::get)
         
.function("getA",&B::getA, allow_raw_pointers())
         
.function("addA",&B::addA, allow_raw_pointers())
         
;
}

My javascript code :

var B = new Module.B;
var A = B.getA();
B
.addA(A);

I get the following error : "ReferenceError: getTypeName is not defined"
I suppose it's because the class A is not embind. I work with big class I don't have to manipulate in javascript, I just have a pointer.

Is it possible to use pointer on class which are not embind ? (like cwrap which return an integer for a pointer)

Jukka Jylänki

unread,
Nov 19, 2014, 10:13:00 AM11/19/14
to emscripte...@googlegroups.com
You can register that class without anything exposed on it, which will allow you to pass pointers to it around in a black-box manner.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guillaume...@gmail.com

unread,
Nov 19, 2014, 11:02:44 AM11/19/14
to emscripte...@googlegroups.com
Thanks, I try it and it work for my simple example.

But in my project, the class has virtual methods (but no pure virtual)... and I have now this error :
incomplete type 'GuidoParser' used in type trait expression
    : public integral_constant<bool, __is_polymorphic(_Tp)> {};

Any idea ?
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

guillaume...@gmail.com

unread,
Nov 19, 2014, 11:46:04 AM11/19/14
to emscripte...@googlegroups.com
OK, it's not the virtual function. It's because this class is referenced in an other file just with "class GuidoParser;" and not with the include file definition.

Chad Austin

unread,
Jan 7, 2015, 1:02:26 PM1/7/15
to emscripte...@googlegroups.com
It sounds like embind currently does not support forward-declared classes because some information about the type definition is necessary at bind type: how to copy the value, destroy it, and so on.

It's probably possible to add support for this functionality.  I wonder if there is a way using C++ templates to determine whether a type is declared or declared-and-defined at bind time.

If not, perhaps an API something like...

opaque_handle<C>("C");

... would work.  You wouldn't be able to bind member functions or constructors or destructors or anything, but you could pass C* pointers around as opaque integers.


To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Chad Austin
Technical Director, IMVU

Reply all
Reply to author
Forward
0 new messages