Rcpp Version 1.0.14
Loading...
Searching...
No Matches
unwindProtect.h
Go to the documentation of this file.
1
2// unwind.h: Rcpp R/C++ interface class library -- Unwind Protect
3//
4// Copyright (C) 2018 - 2020 RStudio
5// Copyright (C) 2021 RStudio, Dirk Eddelbuettel and IƱaki Ucar
6//
7// This file is part of Rcpp.
8//
9// Rcpp is free software: you can redistribute it and/or modify it
10// under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 2 of the License, or
12// (at your option) any later version.
13//
14// Rcpp is distributed in the hope that it will be useful, but
15// WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21
22#ifndef RCPP_UNWINDPROTECT_H
23#define RCPP_UNWINDPROTECT_H
24
25#include <csetjmp>
26
27#ifdef RCPP_USING_CXX11
28#include <functional>
29#endif
30
31
32namespace Rcpp { namespace internal {
33
34struct UnwindData {
35 std::jmp_buf jmpbuf;
36};
37
38// First jump back to the protected context with a C longjmp because
39// `Rcpp_protected_eval()` is called from C and we can't safely throw
40// exceptions across C frames.
41inline void maybeJump(void* unwind_data, Rboolean jump) {
42 if (jump) {
43 UnwindData* data = static_cast<UnwindData*>(unwind_data);
44 longjmp(data->jmpbuf, 1);
45 }
46}
47
48#ifdef RCPP_USING_CXX11
49inline SEXP unwindProtectUnwrap(void* data) {
50 std::function<SEXP(void)>* callback = (std::function<SEXP(void)>*) data;
51 return (*callback)();
52}
53#endif
54
55}} // namespace Rcpp::internal
56
57
58namespace Rcpp {
59
60inline SEXP unwindProtect(SEXP (*callback)(void* data), void* data) {
63
64 if (setjmp(unwind_data.jmpbuf)) {
65 // Keep the token protected while unwinding because R code might run
66 // in C++ destructors. Can't use PROTECT() for this because
67 // UNPROTECT() might be called in a destructor, for instance if a
68 // Shield<SEXP> is on the stack.
69 ::R_PreserveObject(token);
70
71 throw LongjumpException(token);
72 }
73
74 return ::R_UnwindProtect(callback, data,
76 token);
77}
78
79#ifdef RCPP_USING_CXX11
80inline SEXP unwindProtect(std::function<SEXP(void)> callback) {
82}
83#endif
84
85} // namespace Rcpp
86
87#endif
T as(SEXP x, ::Rcpp::traits::r_type_primitive_tag)
Definition as.h:43
void maybeJump(void *unwind_data, Rboolean jump)
Rcpp API.
Definition algo.h:28
SEXP unwindProtect(SEXP(*callback)(void *data), void *data)
T as(SEXP x)
Definition as.h:151