Skip to content

Commit

Permalink
cpp_register(): add use_package parameter
Browse files Browse the repository at this point in the history
so that the generated wrapper code calls C++ functions as
.Call("func", ..., PACKAGE="pkg")
  • Loading branch information
alyst committed Aug 19, 2021
1 parent cd8b58a commit b7e2313
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 7 additions & 4 deletions R/register.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#' In order to use `cpp_register()` the `cli`, `decor`, `desc`, `glue`,
#' `tibble` and `vctrs` packages must also be installed.
#' @param path The path to the package root directory
#' @param use_package If `TRUE`, uses `.Call("yourfunc", ..., PACKAGE="yourpackage")`
#' to call C++ functions, otherwise (the default) uses ``.Call(`yourfunc`, ...)``.
#' @param quiet If `TRUE` suppresses output from this function
#' @return The paths to the generated R and C++ source files (in that order).
#' @export
Expand All @@ -31,7 +33,7 @@
#'
#' # cleanup
#' unlink(dir, recursive = TRUE)
cpp_register <- function(path = ".", quiet = FALSE) {
cpp_register <- function(path = ".", use_package = FALSE, quiet = FALSE) {
stop_unless_installed(get_cpp_register_needs())

r_path <- file.path(path, "R", "cpp11.R")
Expand All @@ -56,7 +58,7 @@ cpp_register <- function(path = ".", quiet = FALSE) {

init <- generate_init_functions(get_registered_functions(all_decorations, "cpp11::init", quiet))

r_functions <- generate_r_functions(funs, package, use_package = FALSE)
r_functions <- generate_r_functions(funs, package, use_package = use_package)

dir.create(dirname(r_path), recursive = TRUE, showWarnings = FALSE)

Expand Down Expand Up @@ -102,6 +104,8 @@ cpp_register <- function(path = ".", quiet = FALSE) {

extra_includes <- paste0(extra_includes, collapse = "\n")

forceSymbols = if (use_package) "" else "\n R_forceSymbols(dll, TRUE);"

brio::write_lines(path = cpp_path, glue::glue('
// Generated by cpp11: do not edit by hand
// clang-format off
Expand All @@ -118,8 +122,7 @@ cpp_register <- function(path = ".", quiet = FALSE) {
{init$declarations}
extern "C" attribute_visible void R_init_{package}(DllInfo* dll){{
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);{init$calls}
R_forceSymbols(dll, TRUE);
R_useDynamicSymbols(dll, FALSE);{init$calls}{forceSymbols}
}}
',
call_entries = glue::glue_collapse(call_entries, "\n")
Expand Down
5 changes: 4 additions & 1 deletion man/cpp_register.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b7e2313

Please sign in to comment.