Coder Social home page Coder Social logo

Binding C++ functions about wrenbind17 HOT 2 CLOSED

davlhd avatar davlhd commented on July 20, 2024
Binding C++ functions

from wrenbind17.

Comments (2)

matusnovak avatar matusnovak commented on July 20, 2024

Hi @davlhd

Unfortunatelly Wren does not easily support binding global/namespaced functions. However, there is a simple trick to make it work.

What you need to do is to create an empty C++ class. Let's call it Cv. Then you add your cv::treshold function via cls.funcStaticExt binding. See this example here: https://matusnovak.github.io/wrenbind17/tutorial/custom_types/#633-functions-via-external-functions

In short, the C++ part should look like this:

class Cv {
    // empty :)
};

int main(...) {
    [...]
    auto& cls = m.klass<Cv>("Cv");
    cls.funcStaticExt<&cv::threshold>("threshold");
}

Assuming that cv::threshold is a non-member function, and the InputArray and OutputArray have also been added to the Wren VM, then you can use it as the following:

import "mymodule" for Cv

var input = Cv.getMeSomeInput([...]);
var output = Cv.getMeSomeOutput([...]);
var res = Cv.threshold(input, output, [...])

One very important thing to add. I am not exactly sure how the InputArray and OutputArray are defined and used, but internally WrenBind17 will keep them as std::shared_ptr<InputArray> and std::shared_ptr<OutputArray>. It looks like the InputArray is just a typdef of const _InputArray&. You might get a compilation error using the above code because the WrenBind17 will try to dereference std::shared_ptr<InputArray> into const _InputArray& when going from Wren to C++. But doing std::shared_ptr<InputArray> which is essentially the same thing as (if I am reading OpenCV doc correctly) std::shared_ptr<const _InputArray&>, which does not make sense. I don't exactly know how the InputArray is created, but you might have to pass it to Wren via a pointer instead. Wren will not delete the memory if you do that. Otherwise it will try to copy your const _InputArray& into std::shared_ptr<_InputArray> which might cause problems. If you pass it as _InputArray* into Wren, it will only keep the pointer and the shared ptr will not deallocate your array, only keep it as a reference.

In that case, you will have to do the following:

class Cv {
    // empty :)
};

static SomeRetType cvThresholdWrapper(std::shared_ptr<_InputArray> input, [...]) {
    return cv::threshold(input, [...]);
}

int main(...) {
    [...]
    auto& cls = m.klass<Cv>("Cv");
    cls.funcStaticExt<&cvThresholdWrapper>("threshold");
}

In short, every custom type is wrapped into std::shared_ptr<T> by WrenBind17. This mechanism is explained more in https://matusnovak.github.io/wrenbind17/tutorial/custom_types/#62-passing-values-into-wren

from wrenbind17.

davlhd avatar davlhd commented on July 20, 2024

Thank you very much for your detailed reply.

Wrapping it with cls.funcStaticExt<&cv::threshold>("threshold"); worked like a charm.

I will have to look into the memory side of things as I proceed but accessing the function works perfectly for now.

Many thanks

from wrenbind17.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.