Skip to content

Commit b484a55

Browse files
authored
optional formatting for cpp11::stop() and cpp11::warning() using fmt library
1 parent 8676065 commit b484a55

27 files changed

+8648
-62
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Error messages now output original file name rather than the temporary file name (@sbearrows, #194)
66
* Fixed bug when running `cpp_source()` on the same file more than once (@sbearrows, #202)
77
* Removed internal instances of `cpp11::stop()` and replaced with C++ exceptions (@sbearrows, #203)
8+
* Added optionally formatting to `stop()` and `warning()` using {fmt} library (@sbearrows, #169)
89

910
# cpp11 0.3.1
1011

cpp11test/R/cpp11.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ data_frame_ <- function() {
88
.Call(`_cpp11test_data_frame_`)
99
}
1010

11+
my_stop <- function(mystring, myarg) {
12+
invisible(.Call(`_cpp11test_my_stop`, mystring, myarg))
13+
}
14+
15+
my_warning <- function(mystring, myarg) {
16+
invisible(.Call(`_cpp11test_my_warning`, mystring, myarg))
17+
}
18+
19+
my_stop_n1fmt <- function(mystring) {
20+
invisible(.Call(`_cpp11test_my_stop_n1fmt`, mystring))
21+
}
22+
23+
my_stop_n2fmt <- function(mystring, myarg) {
24+
invisible(.Call(`_cpp11test_my_stop_n2fmt`, mystring, myarg))
25+
}
26+
27+
my_warning_n1fmt <- function(mystring) {
28+
invisible(.Call(`_cpp11test_my_warning_n1fmt`, mystring))
29+
}
30+
31+
my_warning_n2fmt <- function(mystring, myarg) {
32+
invisible(.Call(`_cpp11test_my_warning_n2fmt`, mystring, myarg))
33+
}
34+
1135
remove_altrep <- function(x) {
1236
.Call(`_cpp11test_remove_altrep`, x)
1337
}

cpp11test/src/cpp11.cpp

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,54 @@ extern "C" SEXP _cpp11test_data_frame_() {
2020
return cpp11::as_sexp(data_frame_());
2121
END_CPP11
2222
}
23+
// errors_fmt.cpp
24+
void my_stop(std::string mystring, int myarg);
25+
extern "C" SEXP _cpp11test_my_stop(SEXP mystring, SEXP myarg) {
26+
BEGIN_CPP11
27+
my_stop(cpp11::as_cpp<cpp11::decay_t<std::string>>(mystring), cpp11::as_cpp<cpp11::decay_t<int>>(myarg));
28+
return R_NilValue;
29+
END_CPP11
30+
}
31+
// errors_fmt.cpp
32+
void my_warning(std::string mystring, std::string myarg);
33+
extern "C" SEXP _cpp11test_my_warning(SEXP mystring, SEXP myarg) {
34+
BEGIN_CPP11
35+
my_warning(cpp11::as_cpp<cpp11::decay_t<std::string>>(mystring), cpp11::as_cpp<cpp11::decay_t<std::string>>(myarg));
36+
return R_NilValue;
37+
END_CPP11
38+
}
39+
// errors.cpp
40+
void my_stop_n1fmt(std::string mystring);
41+
extern "C" SEXP _cpp11test_my_stop_n1fmt(SEXP mystring) {
42+
BEGIN_CPP11
43+
my_stop_n1fmt(cpp11::as_cpp<cpp11::decay_t<std::string>>(mystring));
44+
return R_NilValue;
45+
END_CPP11
46+
}
47+
// errors.cpp
48+
void my_stop_n2fmt(std::string mystring, std::string myarg);
49+
extern "C" SEXP _cpp11test_my_stop_n2fmt(SEXP mystring, SEXP myarg) {
50+
BEGIN_CPP11
51+
my_stop_n2fmt(cpp11::as_cpp<cpp11::decay_t<std::string>>(mystring), cpp11::as_cpp<cpp11::decay_t<std::string>>(myarg));
52+
return R_NilValue;
53+
END_CPP11
54+
}
55+
// errors.cpp
56+
void my_warning_n1fmt(std::string mystring);
57+
extern "C" SEXP _cpp11test_my_warning_n1fmt(SEXP mystring) {
58+
BEGIN_CPP11
59+
my_warning_n1fmt(cpp11::as_cpp<cpp11::decay_t<std::string>>(mystring));
60+
return R_NilValue;
61+
END_CPP11
62+
}
63+
// errors.cpp
64+
void my_warning_n2fmt(std::string mystring, std::string myarg);
65+
extern "C" SEXP _cpp11test_my_warning_n2fmt(SEXP mystring, SEXP myarg) {
66+
BEGIN_CPP11
67+
my_warning_n2fmt(cpp11::as_cpp<cpp11::decay_t<std::string>>(mystring), cpp11::as_cpp<cpp11::decay_t<std::string>>(myarg));
68+
return R_NilValue;
69+
END_CPP11
70+
}
2371
// find-intervals.cpp
2472
SEXP remove_altrep(SEXP x);
2573
extern "C" SEXP _cpp11test_remove_altrep(SEXP x) {
@@ -321,49 +369,6 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
321369

322370
extern "C" {
323371
/* .Call calls */
324-
extern SEXP _cpp11test_cpp11_add_vec_for_(SEXP, SEXP);
325-
extern SEXP _cpp11test_cpp11_insert_(SEXP);
326-
extern SEXP _cpp11test_cpp11_release_(SEXP);
327-
extern SEXP _cpp11test_cpp11_safe_(SEXP);
328-
extern SEXP _cpp11test_data_frame_();
329-
extern SEXP _cpp11test_findInterval2(SEXP, SEXP);
330-
extern SEXP _cpp11test_findInterval2_5(SEXP, SEXP);
331-
extern SEXP _cpp11test_findInterval3(SEXP, SEXP);
332-
extern SEXP _cpp11test_findInterval4(SEXP, SEXP);
333-
extern SEXP _cpp11test_gibbs_cpp(SEXP, SEXP);
334-
extern SEXP _cpp11test_gibbs_cpp2(SEXP, SEXP);
335-
extern SEXP _cpp11test_gibbs_rcpp(SEXP, SEXP);
336-
extern SEXP _cpp11test_gibbs_rcpp2(SEXP, SEXP);
337-
extern SEXP _cpp11test_grow_(SEXP);
338-
extern SEXP _cpp11test_protect_many_(SEXP);
339-
extern SEXP _cpp11test_protect_many_cpp11_(SEXP);
340-
extern SEXP _cpp11test_protect_many_preserve_(SEXP);
341-
extern SEXP _cpp11test_protect_many_rcpp_(SEXP);
342-
extern SEXP _cpp11test_protect_many_sexp_(SEXP);
343-
extern SEXP _cpp11test_protect_one_(SEXP, SEXP);
344-
extern SEXP _cpp11test_protect_one_cpp11_(SEXP, SEXP);
345-
extern SEXP _cpp11test_protect_one_preserve_(SEXP, SEXP);
346-
extern SEXP _cpp11test_protect_one_sexp_(SEXP, SEXP);
347-
extern SEXP _cpp11test_rcpp_grow_(SEXP);
348-
extern SEXP _cpp11test_rcpp_release_(SEXP);
349-
extern SEXP _cpp11test_rcpp_sum_dbl_accumulate_(SEXP);
350-
extern SEXP _cpp11test_rcpp_sum_dbl_for_(SEXP);
351-
extern SEXP _cpp11test_rcpp_sum_dbl_foreach_(SEXP);
352-
extern SEXP _cpp11test_rcpp_sum_int_for_(SEXP);
353-
extern SEXP _cpp11test_remove_altrep(SEXP);
354-
extern SEXP _cpp11test_row_sums(SEXP);
355-
extern SEXP _cpp11test_sum_dbl_accumulate2_(SEXP);
356-
extern SEXP _cpp11test_sum_dbl_accumulate_(SEXP);
357-
extern SEXP _cpp11test_sum_dbl_for2_(SEXP);
358-
extern SEXP _cpp11test_sum_dbl_for3_(SEXP);
359-
extern SEXP _cpp11test_sum_dbl_for_(SEXP);
360-
extern SEXP _cpp11test_sum_dbl_foreach2_(SEXP);
361-
extern SEXP _cpp11test_sum_dbl_foreach_(SEXP);
362-
extern SEXP _cpp11test_sum_int_accumulate_(SEXP);
363-
extern SEXP _cpp11test_sum_int_for2_(SEXP);
364-
extern SEXP _cpp11test_sum_int_for_(SEXP);
365-
extern SEXP _cpp11test_sum_int_foreach_(SEXP);
366-
extern SEXP _cpp11test_upper_bound(SEXP, SEXP);
367372
extern SEXP run_testthat_tests(SEXP);
368373

369374
static const R_CallMethodDef CallEntries[] = {
@@ -381,6 +386,12 @@ static const R_CallMethodDef CallEntries[] = {
381386
{"_cpp11test_gibbs_rcpp", (DL_FUNC) &_cpp11test_gibbs_rcpp, 2},
382387
{"_cpp11test_gibbs_rcpp2", (DL_FUNC) &_cpp11test_gibbs_rcpp2, 2},
383388
{"_cpp11test_grow_", (DL_FUNC) &_cpp11test_grow_, 1},
389+
{"_cpp11test_my_stop", (DL_FUNC) &_cpp11test_my_stop, 2},
390+
{"_cpp11test_my_stop_n1fmt", (DL_FUNC) &_cpp11test_my_stop_n1fmt, 1},
391+
{"_cpp11test_my_stop_n2fmt", (DL_FUNC) &_cpp11test_my_stop_n2fmt, 2},
392+
{"_cpp11test_my_warning", (DL_FUNC) &_cpp11test_my_warning, 2},
393+
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
394+
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
384395
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
385396
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
386397
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},

cpp11test/src/errors.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "cpp11/protect.hpp"
2+
using namespace cpp11;
3+
4+
[[cpp11::register]] void my_stop_n1fmt(std::string mystring) { cpp11::stop(mystring); }
5+
[[cpp11::register]] void my_stop_n2fmt(std::string mystring, std::string myarg) {
6+
cpp11::stop(mystring, myarg.c_str());
7+
}
8+
[[cpp11::register]] void my_warning_n1fmt(std::string mystring) {
9+
cpp11::warning(mystring);
10+
}
11+
[[cpp11::register]] void my_warning_n2fmt(std::string mystring, std::string myarg) {
12+
cpp11::warning(mystring, myarg.c_str());
13+
}

cpp11test/src/errors_fmt.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define CPP11_USE_FMT
2+
#include "cpp11/protect.hpp"
3+
using namespace cpp11;
4+
5+
[[cpp11::register]] void my_stop(std::string mystring, int myarg) {
6+
cpp11::stop(mystring, myarg);
7+
}
8+
[[cpp11::register]] void my_warning(std::string mystring, std::string myarg) {
9+
cpp11::warning(mystring, myarg);
10+
}

cpp11test/src/test-as.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include <testthat.h>
2-
31
#include <deque>
42
#include <string>
53
#include <vector>
64

75
#include "cpp11/declarations.hpp"
86

7+
#include <testthat.h>
8+
99
#include "Rcpp.h"
1010

1111
context("as_cpp-C++") {

cpp11test/src/test-data_frame.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <testthat.h>
21
#include "cpp11/data_frame.hpp"
32
#include "cpp11/function.hpp"
43
#include "cpp11/integers.hpp"
54
#include "cpp11/strings.hpp"
65

6+
#include <testthat.h>
7+
78
context("data_frame-C++") {
89
test_that("data_frame works") {
910
auto getExportedValue = cpp11::package("base")["getExportedValue"];

cpp11test/src/test-doubles.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#include <testthat.h>
21
#include <cstring>
32
#include "cpp11/doubles.hpp"
43
#include "cpp11/integers.hpp"
54
#include "cpp11/sexp.hpp"
65
#include "cpp11/strings.hpp"
76

7+
#include <testthat.h>
8+
89
context("doubles-C++") {
910
test_that("doubles::r_vector(SEXP)") {
1011
cpp11::doubles x(Rf_allocVector(REALSXP, 2));

cpp11test/src/test-environment.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include <testthat.h>
2-
31
#include "cpp11/as.hpp"
42
#include "cpp11/environment.hpp"
53
#include "cpp11/function.hpp"
64
#include "cpp11/strings.hpp"
75

6+
#include <testthat.h>
7+
88
context("environment-C++") {
99
test_that("environment works") {
1010
auto new_env = cpp11::package("base")["new.env"];

cpp11test/src/test-external_pointer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#include <testthat.h>
21
#include <iostream>
32
#include "cpp11/external_pointer.hpp"
43

4+
#include <testthat.h>
5+
56
bool deleted = false;
67

78
void deleter(int* ptr) {

cpp11test/src/test-function.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include <testthat.h>
21
#include "cpp11/function.hpp"
32

3+
#include <testthat.h>
4+
45
context("function-C++") {
56
test_that("functions can be called") {
67
auto median = cpp11::package("stats")["median"];

cpp11test/src/test-integers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <testthat.h>
21
#include "Rversion.h"
32
#include "cpp11/doubles.hpp"
43
#include "cpp11/integers.hpp"
54
#include "cpp11/strings.hpp"
65

6+
#include <testthat.h>
7+
78
context("integers-C++") {
89
test_that("as_integers(doubles)") {
910
// TYPEOF(x) == INTSXP

cpp11test/src/test-list.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#include <testthat.h>
21
#include "cpp11/doubles.hpp"
32
#include "cpp11/integers.hpp"
43
#include "cpp11/list.hpp"
54
#include "cpp11/logicals.hpp"
65
#include "cpp11/raws.hpp"
76
#include "cpp11/strings.hpp"
87

8+
#include <testthat.h>
9+
910
context("list-C++") {
1011
test_that("list.push_back()") {
1112
cpp11::writable::list x;

cpp11test/src/test-list_of.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <testthat.h>
21
#include "cpp11/doubles.hpp"
32
#include "cpp11/list.hpp"
43
#include "cpp11/list_of.hpp"
54
#include "cpp11/strings.hpp"
65

6+
#include <testthat.h>
7+
78
context("list_of-C++") {
89
test_that("list_of works") {
910
using namespace cpp11::literals;

cpp11test/src/test-logicals.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include <testthat.h>
21
#include "cpp11/logicals.hpp"
32

3+
#include <testthat.h>
4+
45
context("logicals-C++") {
56
test_that("logicals.push_back()") {
67
cpp11::writable::logicals x;

cpp11test/src/test-matrix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <testthat.h>
21
#include "cpp11/doubles.hpp"
32
#include "cpp11/function.hpp"
43
#include "cpp11/integers.hpp"
54
#include "cpp11/matrix.hpp"
65

6+
#include <testthat.h>
7+
78
context("matrix-C++") {
89
test_that("matrix dim attributes are correct for writable matrices") {
910
cpp11::writable::doubles_matrix x(5, 2);

cpp11test/src/test-nas.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <testthat.h>
21
#include "cpp11/doubles.hpp"
32
#include "cpp11/integers.hpp"
43
#include "cpp11/r_bool.hpp"
54
#include "cpp11/r_string.hpp"
65

6+
#include <testthat.h>
7+
78
context("nas-C++") {
89
test_that("na integer") { expect_true(cpp11::na<int>() == NA_INTEGER); }
910
test_that("na double") { expect_true(ISNA(cpp11::na<double>())); }

cpp11test/src/test-protect.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define CPP11_USE_FMT
12
#include "cpp11/protect.hpp"
23
#include "testthat.h"
34

@@ -39,7 +40,9 @@ context("unwind_protect-C++") {
3940

4041
test_that("stop throws an unwind_exception") {
4142
expect_error_as(cpp11::stop("error"), cpp11::unwind_exception);
42-
expect_error_as(cpp11::stop("error: %s", "message"), cpp11::unwind_exception);
43+
expect_error_as(cpp11::stop("error {}", "message"), cpp11::unwind_exception);
44+
expect_error_as(cpp11::stop("error {a}", fmt::arg("a", "message")),
45+
cpp11::unwind_exception);
4346
}
4447

4548
test_that("safe wraps R functions and works if there is an R error") {

cpp11test/src/test-raws.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include <testthat.h>
21
#include "cpp11/raws.hpp"
32

3+
#include <testthat.h>
4+
45
context("raws-C++") {
56
test_that("raws.push_back()") {
67
cpp11::writable::raws x;

cpp11test/src/test-sexp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include <testthat.h>
21
#include "cpp11/list.hpp"
32

3+
#include <testthat.h>
4+
45
context("sexp-C++") {
56
test_that("sexp initializer lists work") {
67
using namespace cpp11::literals;

cpp11test/src/test-string.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include <testthat.h>
21
#include "cpp11/strings.hpp"
32

3+
#include <testthat.h>
4+
45
context("string-C++") {
56
test_that("is_na(string)") {
67
cpp11::r_string x("foo");

cpp11test/src/test-strings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#include <testthat.h>
21
#include <cstring>
32
#include "cpp11/sexp.hpp"
43
#include "cpp11/strings.hpp"
54

5+
#include <testthat.h>
6+
67
context("strings-C++") {
78
test_that("strings.push_back()") {
89
cpp11::writable::strings x;

0 commit comments

Comments
 (0)