Skip to content

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

Open

Description

@romainfrancois

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.

Activity

  1. krlmlr commented on May 1, 2024

    @krlmlr
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions