|
24 | 24 |
|
25 | 25 | #include <Rversion.h>
|
26 | 26 |
|
| 27 | +#ifndef RCPP_DEFAULT_INCLUDE_CALL |
| 28 | +#define RCPP_DEFAULT_INCLUDE_CALL true |
| 29 | +#endif |
27 | 30 |
|
28 | 31 | #define GET_STACKTRACE() stack_trace( __FILE__, __LINE__ )
|
29 | 32 |
|
30 | 33 | namespace Rcpp {
|
31 | 34 |
|
32 | 35 | class exception : public std::exception {
|
33 | 36 | public:
|
34 |
| - explicit exception(const char* message_, bool include_call = true) : // #nocov start |
| 37 | + explicit exception(const char* message_, bool include_call = RCPP_DEFAULT_INCLUDE_CALL) : // #nocov start |
35 | 38 | message(message_),
|
36 | 39 | include_call_(include_call){
|
37 | 40 | rcpp_set_stack_trace(Shield<SEXP>(stack_trace()));
|
38 | 41 | }
|
39 |
| - exception(const char* message_, const char*, int, bool include_call = true) : |
| 42 | + exception(const char* message_, const char*, int, bool include_call = RCPP_DEFAULT_INCLUDE_CALL) : |
40 | 43 | message(message_),
|
41 | 44 | include_call_(include_call){
|
42 | 45 | rcpp_set_stack_trace(Shield<SEXP>(stack_trace()));
|
@@ -306,34 +309,32 @@ inline SEXP make_condition(const std::string& ex_msg, SEXP call, SEXP cppstack,
|
306 | 309 | return res ;
|
307 | 310 | }
|
308 | 311 |
|
| 312 | +template <typename Exception> |
| 313 | +inline SEXP exception_to_condition_template( const Exception& ex, bool include_call) { |
| 314 | + std::string ex_class = demangle( typeid(ex).name() ) ; |
| 315 | + std::string ex_msg = ex.what() ; |
| 316 | + |
| 317 | + Rcpp::Shelter<SEXP> shelter; |
| 318 | + SEXP call, cppstack; |
| 319 | + if (include_call) { |
| 320 | + call = shelter(get_last_call()); |
| 321 | + cppstack = shelter(rcpp_get_stack_trace()); |
| 322 | + } else { |
| 323 | + call = R_NilValue; |
| 324 | + cppstack = R_NilValue; |
| 325 | + } |
| 326 | + SEXP classes = shelter( get_exception_classes(ex_class) ); |
| 327 | + SEXP condition = shelter( make_condition( ex_msg, call, cppstack, classes) ); |
| 328 | + rcpp_set_stack_trace( R_NilValue ) ; |
| 329 | + return condition ; |
| 330 | +} |
| 331 | + |
309 | 332 | inline SEXP rcpp_exception_to_r_condition(const Rcpp::exception& ex) {
|
310 |
| - std::string ex_class = demangle( typeid(ex).name() ) ; |
311 |
| - std::string ex_msg = ex.what() ; |
312 |
| - |
313 |
| - SEXP call, cppstack; |
314 |
| - if (ex.include_call()) { |
315 |
| - call = Rcpp::Shield<SEXP>(get_last_call()); |
316 |
| - cppstack = Rcpp::Shield<SEXP>( rcpp_get_stack_trace()); |
317 |
| - } else { |
318 |
| - call = R_NilValue; |
319 |
| - cppstack = R_NilValue; |
320 |
| - } |
321 |
| - Rcpp::Shield<SEXP> classes( get_exception_classes(ex_class) ); |
322 |
| - Rcpp::Shield<SEXP> condition( make_condition( ex_msg, call, cppstack, classes) ); |
323 |
| - rcpp_set_stack_trace( R_NilValue ) ; |
324 |
| - return condition ; |
| 333 | + return exception_to_condition_template(ex, ex.include_call()); |
325 | 334 | }
|
326 | 335 |
|
327 | 336 | inline SEXP exception_to_r_condition( const std::exception& ex){
|
328 |
| - std::string ex_class = demangle( typeid(ex).name() ) ; |
329 |
| - std::string ex_msg = ex.what() ; |
330 |
| - |
331 |
| - Rcpp::Shield<SEXP> cppstack( rcpp_get_stack_trace() ); |
332 |
| - Rcpp::Shield<SEXP> call( get_last_call() ); |
333 |
| - Rcpp::Shield<SEXP> classes( get_exception_classes(ex_class) ); |
334 |
| - Rcpp::Shield<SEXP> condition( make_condition( ex_msg, call, cppstack, classes) ); |
335 |
| - rcpp_set_stack_trace( R_NilValue ) ; |
336 |
| - return condition ; |
| 337 | + return exception_to_condition_template(ex, RCPP_DEFAULT_INCLUDE_CALL); |
337 | 338 | }
|
338 | 339 |
|
339 | 340 | inline SEXP string_to_try_error( const std::string& str){
|
|
0 commit comments