Skip to content

Commit ab20cd2

Browse files
sbearrowsjimhester
andauthored
Bug cpp11 decorators (#197)
Co-authored-by: Jim Hester <james.f.hester@gmail.com>
1 parent 532cd3f commit ab20cd2

10 files changed

+98
-12
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# cpp11 (development version)
2+
* Allow cpp11 decorators of the form `cpp11::linking_to` (@sbearrows, #193)
23

34
# cpp11 0.3.1
45

R/register.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ get_cpp_register_needs <- function() {
279279

280280
check_valid_attributes <- function(decorations) {
281281

282-
bad_decor <- !decorations$decoration %in% c("cpp11::register", "cpp11::init")
282+
bad_decor <- startsWith(decorations$decoration, "cpp11::") &
283+
(!decorations$decoration %in% c("cpp11::register", "cpp11::init", "cpp11::linking_to"))
283284

284285
if(any(bad_decor)) {
285286
lines <- decorations$line[bad_decor]
@@ -288,7 +289,7 @@ check_valid_attributes <- function(decorations) {
288289
bad_lines <- glue::glue_collapse(glue::glue("- Invalid attribute `{names}` on
289290
line {lines} in file '{file}'."), "\n")
290291

291-
msg <- glue::glue("cpp11 attributes must be either `cpp11::register` or `cpp11::init`:
292+
msg <- glue::glue("cpp11 attributes must be one of `cpp11::register`, `cpp11::init` or `cpp11::linking_to`:
292293
{bad_lines}
293294
")
294295
stop(msg, call. = FALSE)

R/source.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#' [dyn.load()] (invisibly). For `[cpp_eval()]` the results of the evaluated
2323
#' expression.
2424
#' @examples
25-
#' \dontrun{
25+
#'
2626
#' cpp_source(
2727
#' code = '#include "cpp11/integers.hpp"
2828
#'
@@ -51,7 +51,7 @@
5151
#'
5252
#' [[cpp11::register]] void
5353
#' show_progress() {
54-
#' RProgress::RProgress pb("Downloading [:bar] ETA: :eta");
54+
#' RProgress::RProgress pb("Processing [:bar] ETA: :eta");
5555
#'
5656
#' pb.tick(0);
5757
#' for (int i = 0; i < 100; i++) {
@@ -63,7 +63,7 @@
6363
#'
6464
#' show_progress()
6565
#' }
66-
#' }
66+
#'
6767
#' @export
6868
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), dir = tempfile()) {
6969
stop_unless_installed(c("brio", "callr", "cli", "decor", "desc", "glue", "tibble", "vctrs"))

man/cpp11-package.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[cpp11::link_to("progress")]]
2+
3+
[[cpp11::register]] void
4+
show_progress() {
5+
RProgress::RProgress pb("Processing [:bar] ETA: :eta");
6+
7+
pb.tick(0);
8+
for (int i = 0; i < 100; i++) {
9+
usleep(2.0 / 100 * 1000000);
10+
pb.tick();
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[cpp11::linking_to("progress")]]
2+
3+
[[cpp11::register]] void
4+
show_progress() {
5+
RProgress::RProgress pb("Processing [:bar] ETA: :eta");
6+
7+
pb.tick(0);
8+
for (int i = 0; i < 100; i++) {
9+
usleep(2.0 / 100 * 1000000);
10+
pb.tick();
11+
}
12+
}

tests/testthat/multiple_incorrect.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
[[cpp11:register]] int foo() { return 1; }
1+
[[cpp11::registe]] int foo() { return 1; }
22

3-
[[cpp11:register]] double bar(bool run) { return 1.0; }
3+
[[cpp11::register]] double bar(bool run) { return 1.0; }
44

5-
[[cpp11::register]] bool baz(bool run, int value = 0) { return true; }
5+
[[cpp11::reg]] bool baz(bool run, int value = 0) { return true; }

tests/testthat/single_incorrect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[[cpp11:register]] int foo() { return 1; }
1+
[[cpp11::registe]] int foo() { return 1; }

tests/testthat/test-register.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,13 @@ test_that("check_valid_attributes does not return an error if all registers are
718718
file.copy(test_path("multiple.cpp"), file.path(p, "src", "multiple.cpp"))
719719

720720
expect_error_free(cpp_register(p))
721+
722+
pkg <- local_package()
723+
p <- pkg_path(pkg)
724+
dir.create(file.path(p, "src"))
725+
file.copy(test_path("linking_to_registers.cpp"), file.path(p, "src", "linking_to_registers.cpp"))
726+
727+
expect_error_free(cpp_register(p))
721728
})
722729

723730

@@ -735,4 +742,11 @@ test_that("check_valid_attributes returns an error if one or more registers is i
735742
file.copy(test_path("multiple_incorrect.cpp"), file.path(p, "src", "multiple_incorrect.cpp"))
736743

737744
expect_error(cpp_register(p))
745+
746+
pkg <- local_package()
747+
p <- pkg_path(pkg)
748+
dir.create(file.path(p, "src"))
749+
file.copy(test_path("linking_to_incorrect_registers.cpp"), file.path(p, "src", "linking_to_incorrect_registers.cpp"))
750+
751+
expect_error(cpp_register(p))
738752
})

tests/testthat/test-source.R

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,31 @@ test_that("check_valid_attributes does not return an error if all registers are
118118
x.push_back({"foo"_nm = 1});
119119
return x;
120120
}'))
121+
expect_error_free(
122+
cpp11::cpp_source(
123+
code = '#include <cpp11/R.hpp>
124+
#include <RProgress.h>
125+
126+
[[cpp11::linking_to("progress")]]
127+
128+
[[cpp11::register]] void show_progress() {
129+
RProgress::RProgress pb("Processing [:bar] ETA: :eta");
130+
131+
pb.tick(0);
132+
for (int i = 0; i < 100; i++) {
133+
usleep(2.0 / 100 * 1000000);
134+
pb.tick();
135+
}
136+
}
137+
')
138+
)
121139
})
122140

123141
test_that("check_valid_attributes returns an error if one or more registers is incorrect", {
124142
expect_error(
125143
cpp11::cpp_source(code = '#include <cpp11.hpp>
126144
using namespace cpp11::literals;
127-
[[cpp11:register]]
145+
[[cpp11::reg]]
128146
cpp11::list fn() {
129147
cpp11::writable::list x;
130148
x.push_back({"foo"_nm = 1});
@@ -140,7 +158,17 @@ test_that("check_valid_attributes returns an error if one or more registers is i
140158
expect_error(
141159
cpp11::cpp_source(code = '#include <cpp11.hpp>
142160
using namespace cpp11::literals;
143-
[[cpp11:register]]
161+
[[cpp11::reg]]
162+
cpp11::list fn() {
163+
cpp11::writable::list x;
164+
x.push_back({"foo"_nm = 1});
165+
return x;
166+
}'))
167+
168+
expect_error(
169+
cpp11::cpp_source(code = '#include <cpp11.hpp>
170+
using namespace cpp11::literals;
171+
[[cpp11::reg]]
144172
cpp11::list fn() {
145173
cpp11::writable::list x;
146174
x.push_back({"foo"_nm = 1});
@@ -152,4 +180,21 @@ test_that("check_valid_attributes returns an error if one or more registers is i
152180
x.push_back({"foo"_nm = 1});
153181
return x;
154182
}'))
155-
})
183+
184+
expect_error(
185+
cpp11::cpp_source(
186+
code = '
187+
#include <cpp11/R.hpp>
188+
#include <RProgress.h>
189+
[[cpp11::link_to("progress")]]
190+
[[cpp11::register]] void show_progress() {
191+
RProgress::RProgress pb("Processing [:bar] ETA: :eta");
192+
pb.tick(0);
193+
for (int i = 0; i < 100; i++) {
194+
usleep(2.0 / 100 * 1000000);
195+
pb.tick();
196+
}
197+
}
198+
')
199+
)
200+
})

0 commit comments

Comments
 (0)