Skip to content

Commit b7e2313

Browse files
committed
cpp_register(): add use_package parameter
so that the generated wrapper code calls C++ functions as .Call("func", ..., PACKAGE="pkg")
1 parent cd8b58a commit b7e2313

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

R/register.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#' In order to use `cpp_register()` the `cli`, `decor`, `desc`, `glue`,
88
#' `tibble` and `vctrs` packages must also be installed.
99
#' @param path The path to the package root directory
10+
#' @param use_package If `TRUE`, uses `.Call("yourfunc", ..., PACKAGE="yourpackage")`
11+
#' to call C++ functions, otherwise (the default) uses ``.Call(`yourfunc`, ...)``.
1012
#' @param quiet If `TRUE` suppresses output from this function
1113
#' @return The paths to the generated R and C++ source files (in that order).
1214
#' @export
@@ -31,7 +33,7 @@
3133
#'
3234
#' # cleanup
3335
#' unlink(dir, recursive = TRUE)
34-
cpp_register <- function(path = ".", quiet = FALSE) {
36+
cpp_register <- function(path = ".", use_package = FALSE, quiet = FALSE) {
3537
stop_unless_installed(get_cpp_register_needs())
3638

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

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

59-
r_functions <- generate_r_functions(funs, package, use_package = FALSE)
61+
r_functions <- generate_r_functions(funs, package, use_package = use_package)
6062

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

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

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

107+
forceSymbols = if (use_package) "" else "\n R_forceSymbols(dll, TRUE);"
108+
105109
brio::write_lines(path = cpp_path, glue::glue('
106110
// Generated by cpp11: do not edit by hand
107111
// clang-format off
@@ -118,8 +122,7 @@ cpp_register <- function(path = ".", quiet = FALSE) {
118122
{init$declarations}
119123
extern "C" attribute_visible void R_init_{package}(DllInfo* dll){{
120124
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
121-
R_useDynamicSymbols(dll, FALSE);{init$calls}
122-
R_forceSymbols(dll, TRUE);
125+
R_useDynamicSymbols(dll, FALSE);{init$calls}{forceSymbols}
123126
}}
124127
',
125128
call_entries = glue::glue_collapse(call_entries, "\n")

man/cpp_register.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)