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

BEGIN_CPP11 + END_CPP11 -> using Rf_errorcall(call, ...) #354

Open
romainfrancois opened this issue Apr 19, 2024 · 1 comment
Open

BEGIN_CPP11 + END_CPP11 -> using Rf_errorcall(call, ...) #354

romainfrancois opened this issue Apr 19, 2024 · 1 comment

Comments

@romainfrancois
Copy link
Collaborator

I believe we could expand on END_CPP11 (or add a sibling).

#define END_CPP11 \
} \
catch (cpp11::unwind_exception & e) { \
err = e.token; \
} \
catch (std::exception & e) { \
strncpy(buf, e.what(), sizeof(buf) - 1); \
} \
catch (...) { \
strncpy(buf, "C++ error (unknown cause)", sizeof(buf) - 1); \
} \
if (buf[0] != '\0') { \
Rf_errorcall(R_NilValue, "%s", buf); \
} else if (err != R_NilValue) { \
CPP11_UNWIND \
} \
return R_NilValue;

so that it would take parameters, easily generated in generate_cpp_functions(), i.e. package and funs$name

cpp11/R/register.R

Lines 165 to 185 in 51f4cd5

generate_cpp_functions <- function(funs, package = "cpp11") {
funs <- funs[c("name", "return_type", "args", "file", "line", "decoration")]
funs$real_params <- vcapply(funs$args, glue_collapse_data, "{type} {name}")
funs$sexp_params <- vcapply(funs$args, glue_collapse_data, "SEXP {name}")
funs$calls <- mapply(wrap_call, funs$name, funs$return_type, funs$args, SIMPLIFY = TRUE)
funs$package <- package
out <- glue::glue_data(funs,
'
// {basename(file)}
{return_type} {name}({real_params});
extern "C" SEXP _{package}_{name}({sexp_params}) {{
BEGIN_CPP11
{calls}
END_CPP11
}}
'
)
out <- glue::glue_collapse(out, sep = "\n")
unclass(out)
}

so that we could construct a call to use in Rf_errorcall() instead of using R_NilValue.

@krlmlr
Copy link
Member

krlmlr commented May 1, 2024

I did something similar in duckdb, it has cpp11 vendored.

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

No branches or pull requests

2 participants