From b7e23132f1198e9d14a6a72187d60353c4d824e2 Mon Sep 17 00:00:00 2001 From: Alexey Stukalov Date: Thu, 12 Aug 2021 00:49:19 +0200 Subject: [PATCH] cpp_register(): add use_package parameter so that the generated wrapper code calls C++ functions as .Call("func", ..., PACKAGE="pkg") --- R/register.R | 11 +++++++---- man/cpp_register.Rd | 5 ++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/R/register.R b/R/register.R index efe78a2e..b188a9ad 100644 --- a/R/register.R +++ b/R/register.R @@ -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 @@ -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") @@ -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) @@ -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 @@ -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") diff --git a/man/cpp_register.Rd b/man/cpp_register.Rd index ae6bacb3..6a1c9dc0 100644 --- a/man/cpp_register.Rd +++ b/man/cpp_register.Rd @@ -4,11 +4,14 @@ \alias{cpp_register} \title{Generates wrappers for registered C++ functions} \usage{ -cpp_register(path = ".", quiet = FALSE) +cpp_register(path = ".", use_package = FALSE, quiet = FALSE) } \arguments{ \item{path}{The path to the package root directory} +\item{use_package}{If \code{TRUE}, uses \code{.Call("yourfunc", ..., PACKAGE="yourpackage")} +to call C++ functions, otherwise (the default) uses \code{.Call(`yourfunc`, ...)}.} + \item{quiet}{If \code{TRUE} suppresses output from this function} } \value{