Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to 'return' the bound argument via pass-by-reference. #867

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dandeto
Copy link
Collaborator

@dandeto dandeto commented Dec 2, 2022

This will resolve #730 and also maintains backwards-compatibility.

void webview::unbind(const std::string &name) works as before.
void webview::unbind(const std::string &name, void *&arg) implements the new desired behavior of "returning" the bound argument via pass-by-reference.
void webview_unbind_return(webview_t w, const char *name, void *&arg); is the new C function. I had to change the name since overloading does not exist in C.

Unbind without returning the arg:

context_t context = {.w = w, .count = 0};
webview_bind(w, "increment", increment, &context);
...
webview_unbind(w, "increment");

Unbind, use the arg, and free:

context_t *context = (context_t*) malloc (sizeof(context_t));
context->w = w;
context->count = 0;
webview_bind(w, "increment", increment, context);
...
void* ptr;
webview_unbind_return(w, "increment", ptr);
context_t *context2 = (context_t*) ptr;
printf("Count: %d\n", context2->count)
free(ptr);

The correct way to do this in C++ would be to use a template like so: template<typename T> void unbind(const std::string &name, T *& arg). Unfortunately, I could not figure out how to get C to call a C++ template function. Maybe you guys know more about that. This continues the pattern of using void* to accept an argument of any type, but using void*& is a little unsafe.

@dandeto dandeto mentioned this pull request Dec 2, 2022
@dandeto
Copy link
Collaborator Author

dandeto commented Dec 2, 2022

Built just fine on my machine. I'll look into it later.

@SteffenL
Copy link
Collaborator

SteffenL commented Dec 3, 2022

You'll need void **arg in the C API.

Since this PR adds a new function, why not just add a get_bound_arg function instead?

@dandeto
Copy link
Collaborator Author

dandeto commented Dec 4, 2022

C++ could default the void *& arg = nullptr, but that didn't work when I added C99 compatibility, so yeah I might as well add get_bound_arg instead. Thank you for the tip about the C api, my C++ is much stronger than C :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: have webview_unbind return the arg
2 participants