Skip to content

Commit b938247

Browse files
committed
Add support for cpp11::linking_to
Fixes #48
1 parent e8f793b commit b938247

File tree

6 files changed

+84
-6
lines changed

6 files changed

+84
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Suggests:
3636
knitr,
3737
lobstr,
3838
mockery,
39+
progress,
3940
rmarkdown,
4041
scales,
4142
testthat,

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cpp11 (development version)
22

3+
* `cpp_source()`, `cpp_function()` and `cpp_eval()` now support `[[cpp11::linking_to()]]` syntax to link to third party packages with C++ headers. (#48)
4+
35
* `safe[]` can now work with functions that return any type (#70, @bkietz)
46

57
* cpp11 is now able to compile on gcc 4.8.5 (#69, @bkietz)

R/source.R

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#' [cpp_function()] compiles and loads a single function for use in R.
55
#' [cpp_eval()] evaluates a single C++ expression and returns the result.
66
#'
7+
#' Within C++ code you can use `[[cpp11::linking_to(pkgxyz)]]` to link to
8+
#' external packages. This is equivalent to putting those packages in the
9+
#' `LinkingTo` field in a package DESCRIPTION.
10+
#'
711
#' @param file A file containing C++ code to compile
812
#' @param code If non-null, the C++ code to compile
913
#' @param env The R environment where the R wrapping functions should be defined.
@@ -30,6 +34,30 @@
3034
#' ')
3135
#'
3236
#' num_odd(as.integer(c(1:10, 15, 23)))
37+
#'
38+
#' if (require("progress")) {
39+
#'
40+
#' cpp_source(
41+
#' code = '
42+
#' #include <cpp11/R.hpp>
43+
#' #include <RProgress.h>
44+
#'
45+
#' [[cpp11::linking_to(progress)]]
46+
#'
47+
#' [[cpp11::register]] void
48+
#' show_progress() {
49+
#' RProgress::RProgress pb("Downloading [:bar] ETA: :eta");
50+
#'
51+
#' pb.tick(0);
52+
#' for (int i = 0; i < 100; i++) {
53+
#' usleep(2.0 / 100 * 1000000);
54+
#' pb.tick();
55+
#' }
56+
#' }
57+
#' ')
58+
#'
59+
#' show_progress()
60+
#' }
3361
#' }
3462
#' @export
3563
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE) {
@@ -65,7 +93,9 @@ cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, qu
6593
cpp_path <- file.path(dir, "src", "cpp11.cpp")
6694
brio::write_lines(c('#include "cpp11/declarations.hpp"', "using namespace cpp11;", cpp_functions_definitions), cpp_path)
6795

68-
includes <- generate_include_paths("cpp11")
96+
linking_to <- union(get_linking_to(all_decorations), "cpp11")
97+
98+
includes <- generate_include_paths(linking_to)
6999

70100
if (isTRUE(clean)) {
71101
on.exit(unlink(dir, recursive = TRUE))
@@ -142,3 +172,13 @@ cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE) {
142172
quiet = quiet)
143173
f()
144174
}
175+
176+
get_linking_to <- function(decorations) {
177+
out <- decorations[decorations$decoration == "cpp11::linking_to", ]
178+
179+
if (NROW(decorations) == 0) {
180+
return(character())
181+
}
182+
183+
as.character(unlist(out$params))
184+
}

cpp11test/src/sum.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <numeric>
22
#include "cpp11/doubles.hpp"
33

4-
[[cpp11::register]] double sum_dbl_for_(cpp11::doubles x) {
4+
[[cpp11::linking_to(foo)]]
5+
6+
[[cpp11::register]] double
7+
sum_dbl_for_(cpp11::doubles x) {
58
double sum = 0.;
69
R_xlen_t n = x.size();
710
for (R_xlen_t i = 0; i < n; ++i) {

man/cpp11-package.Rd

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

man/cpp_source.Rd

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)