Comments (2)
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.
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)
- binding operator method HOT 2
- Binding multiple constructors from C++ class HOT 1
- Base class construction in Wren script leaks destructor calls from derived class HOT 4
- Add mode without exceptions HOT 1
- CMake: Allow to skip install HOT 1
- Support std::string_view HOT 1
- How do I put functions from a library into wren class without defining wrapper class on C++ side? HOT 7
- Unable to assign values to binded classes which has binded class members. HOT 2
- Passing a map to C++ HOT 2
- Derived class returned as a base class from C++ cannot use derived methods HOT 3
- Non-copyable types cannot have a constructor HOT 3
- calling method with std::list throws assertion error HOT 1
- random HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wrenbind17.