scapix icon indicating copy to clipboard operation
scapix copied to clipboard

Support interfaces as callbacks from C++ (in addition to std::function support)

Open mathiasi opened this issue 5 years ago • 6 comments

First for all: very interesting project! I was testing it for a new project but I need the ability to implement a C++ interface in the "client" languages. I could see from https://github.com/scapix-com/example1/blob/a0a0b5a27e4c68c1e2f6bb210901206ff378b213/source/chat/contact.h there is some traces of something along those lines (scapix::bridge::interface). What is the current status of this? Was it scratched or is it coming in the pipelines?

mathiasi avatar Sep 12 '19 14:09 mathiasi

Thanks! Implementing interfaces is definitely in the pipeline, but right now you can use already supported std::function<> parameters to implement callbacks to client languages.

Boris-Rasin avatar Sep 12 '19 16:09 Boris-Rasin

Good to hear! Yea I found the way to do callbacks but that doesn't fully meet my needs. I need entire interfaces to be implemented in the client language and have it passed to C++. I'll keep an eye out for updates :)

mathiasi avatar Sep 13 '19 07:09 mathiasi

@Boris-Rasin do I understand correctly that using c++ interfaces in client languages is impossible now?

For instance, I have an interface Foo, it's implementation and the factory Bar that produces Foo. I get a SIGABRT on creating Foo object in JNI.

Bar.h

#include <memory>

#include <scapix/bridge/object.h>
#include "Foo.h"

class Bar : public scapix::bridge::object<Bar> {
public:
    std::shared_ptr<Foo> getFoo() {
        return std::static_pointer_cast<Foo>(std::make_shared<FooImpl>());
    }
};

Foo.h

#include <scapix/bridge/object.h>

class Foo : public scapix::bridge::object<Foo> {
public:
    virtual ~Foo() = default;
    virtual void baz() = 0;
};

class FooImpl : public Foo {
public:
    void baz() override {}
};

client's code

val bar = Bar()
val foo = bar.getFoo()   //  SIGABRT here
foo.baz()

dmalukov avatar Dec 03 '19 12:12 dmalukov

dmalukov, this issue is about support for interfaces as callbacks from C++ (in addition to std::function support). Your example is supposed to work. I will take a look. Can you open a different issue for it?

Boris-Rasin avatar Dec 04 '19 10:12 Boris-Rasin

Interfaces as callbacks from C++ (overriding C++ virtual functions in bridged languages) are supported in the latest version of Scapix, currently for Python bridge only.

Support for other languages coming soon, stay tuned!

Boris-Rasin avatar Oct 13 '21 12:10 Boris-Rasin

Added support for overriding C++ virtual functions in JavaScript bridge (now supported in Python and JavaScript bridges).

Boris-Rasin avatar Nov 18 '21 10:11 Boris-Rasin