forked from r-lib/cpp11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe.cpp
More file actions
35 lines (29 loc) · 846 Bytes
/
safe.cpp
File metadata and controls
35 lines (29 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstring>
#include "cpp11/doubles.hpp"
#include "cpp11/protect.hpp"
[[cpp11::register]] SEXP cpp11_safe_(SEXP x_sxp) {
SEXP err = R_NilValue;
const size_t ERROR_SIZE = 2048;
char buf[ERROR_SIZE] = "";
try {
const cpp11::doubles x(x_sxp);
// Rf_error("R error"); // This will not call dtors
// throw std::runtime_error("C++ error");
// cpp11::unwind_protect([&]() { Rf_error("R error"); });
SEXP out = cpp11::unwind_protect_sexp([&]() { return Rf_allocVector(REALSXP, 1); });
return out;
}
catch (cpp11::unwind_exception& e) {
err = e.token;
} catch (std::exception& e) {
strncpy(buf, e.what(), ERROR_SIZE - 1);
}
if (buf[0] != '\0') {
Rf_error("%s", buf);
} else if (err != R_NilValue) {
#ifdef HAS_UNWIND_PROTECT
R_ContinueUnwind(err);
#endif
}
return R_NilValue;
}