方法`extend`和`implement`在使用`allow_subclass`时不会生成。

huangapple go评论92阅读模式
英文:

The methods `extend` and `implement` are not generated when using `allow_subclass`

问题

I have a C++ class with a number of pure virtual methods. To allow that a JS class can implement those I need to bind the constructor(s) of that class. Unfortunately, embind won't allow that, because of the pure virtual methods.

As an attempt to solve this problem I created a helper C++ class, which implements all those pure virtual methods and returns empty values for them. This leaves me with this code:

class Test {
public:
    Test(int i) {}
    ~Test() {}

    virtual std::string get() const = 0;
}

class TestHelper extends Test {
public:
    virtual std::string get() const override { return ""; }
}

class TestWrapper : public wrapper<TestHelper> {
public:
  EMSCRIPTEN_WRAPPER(TestWrapper);

  virtual ~TestWrapper() noexcept override {
  }
}

EMSCRIPTEN_BINDINGS(main) {
    class_<TestHelper, base<Test>>("Test")

    .allow_subclass<TestWrapper>("TestWrapper");
}

This compiles fine and both Test and TestWrapper exist in the generated wasm module. However, neither of the two contain the methods extend and implement. Other classes, which don't use an intermediate class to implement the pure virtual methods contain those methods.

What is the correct way to generate the missing methods also in the case of the Test class?

英文:

I have a C++ class with a number of pure virtual methods. To allow that a JS class can implement those I need to bind the constructor(s) of that class. Unfortunately, embind won't allow that, because of the pure virtual methods.

As an attempt to solve this problem I created a helper C++ class, which implements all those pure virtual methods and returns empty values for them. This leaves me with this code:

class Test {
public:
    Test(int i) {}
    ~Test() {}

    virtual std::string get() const = 0;
}

class TestHelper extends Test {
public:
    virtual std::string get() const override { return &quot;&quot;; }
}

class TestWrapper : public wrapper&lt;TestHelper&gt; {
public:
  EMSCRIPTEN_WRAPPER(TestWrapper);

  virtual ~TestWrapper() noexcept override {
  }
}


EMSCRIPTEN_BINDINGS(main) {
    class_&lt;TestHelper, base&lt;Test&gt;&gt;(&quot;Test&quot;)

    .allow_subclass&lt;TestWrapper&gt;(&quot;TestWrapper&quot;);
}

This compiles fine and both Test and TestWrapper exist in the generated wasm module. However, neither of the two contain the methods extend and implement. Other classes, which don't use an intermediate class to implement the pure virtual methods contain those methods.

What is the correct way to generate the missing methods also in the case of the Test class?

答案1

得分: 0

“extend”和“implement”方法只有在整个继承链被绑定时才会被添加。在上述情况下,Test类没有绑定。一旦它被添加,缺失的方法也会出现。

英文:

The extend and implement methods are only added, if the entire inheritance chain is bound. In the scenario above no binding for the Test class exists. Once it is added, also the missing methods appear.

huangapple
  • 本文由 发表于 2023年7月3日 22:39:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605798.html
  • embind
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定