Comments (6)
A workaround is to use pointers, as mentioned in #15. However, a better alternative is to use std::unique_ptr
, so that those pointers will be automatically disposed.
std::unique_ptr<nbind::cbFunction> fnPtr;
fnPtr = std::make_unique<nbind::cbFunction>(otherFnFunction);
from nbind.
This sounds like a bug. I'll look into it.
from nbind.
This should work:
nbind::cbFunction fn(otherFnFunction);
While that should be enough for basic use, I guess the move constructor and assignment operators should also be implemented...
from nbind.
Hm, the move constructor isn't really necessary, but the assignment operator is interesting because it's the only way to reassign the callbacks attributes of a class instance (otherwise, using pointers will still be required).
from nbind.
@arcanis once I declare a unique_ptr to a callback function, how do I go about callling that callback function? I seem to be having some issues with the syntax...
from nbind.
@samuelnj226 try: (*fn)();
Full working example:
#include "nbind/api.h"
/* If using C++11 instead of C++14, uncomment this:
namespace std {
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
*/
void callMe(nbind::cbFunction cb) {
std::unique_ptr<nbind::cbFunction> fn;
fn = make_unique<nbind::cbFunction>(cb);
(*fn)();
}
#include "nbind/nbind.h"
NBIND_GLOBAL() {
function(callMe);
}
JS:
var nbind = require('nbind');
var lib = nbind.init().lib;
lib.callMe(() => console.log('Hello, World!'));
from nbind.
Related Issues (20)
- Delay in handoff from C++ to Node awaiter
- nbind doesn't work with electron-packager
- Can't pass char* from JS to cpp function HOT 1
- Windows | EMCC should have a batch file too. (emcc-path)
- Is there any way to call javascript method from c++ HOT 1
- Use of `eval` violates Content Security Policy in browsers HOT 7
- Incompatible with electron 4 and later? HOT 4
- NBind not working with Node 12.0.0
- Release on Node12 HOT 9
- Fail to build with electron 5.0.8, deprecated methods
- Problem when using with proton native HOT 1
- Node 13 build errors HOT 2
- unresolved external
- unresolved external HOT 2
- sample app refuses to run
- npm i produces error "type-check is deprecated" and "Type 'string' cannot be used to index type 'ExportType'."
- Error while dooing fourth step HOT 5
- Iterator support?
- Is this project dead? HOT 2
- std::string support is broken
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 nbind.