Rcpp Version 1.0.9
macros.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // macros.h: Rcpp R/C++ interface class library -- Rcpp macros
4 //
5 // Copyright (C) 2012 - 2015 Dirk Eddelbuettel and Romain Francois
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_macros_macros_h
23 #define Rcpp_macros_macros_h
24 
25 #include <string>
26 
27 namespace Rcpp {
28 namespace internal {
29 namespace debug {
30 
31 inline std::string short_file_name(const char* file)
32 {
33  std::string f(file);
34  size_t index = f.find("/include/");
35  if (index != std::string::npos)
36  f = f.substr(index + 9);
37  return f;
38 }
39 
40 } // namespace debug
41 } // namespace internal
42 } // namespace Rcpp
43 
44 #define RCPP_DECORATE(__FUN__) __FUN__##__rcpp__wrapper__
45 #define RCPP_GET_NAMES(x) Rf_getAttrib(x, R_NamesSymbol)
46 #define RCPP_GET_CLASS(x) Rf_getAttrib(x, R_ClassSymbol)
47 
48 #ifndef BEGIN_RCPP
49 #define BEGIN_RCPP \
50  int rcpp_output_type = 0 ; \
51  int nprot = 0; \
52  (void)rcpp_output_type; \
53  SEXP rcpp_output_condition = R_NilValue ; \
54  (void)rcpp_output_condition; \
55  static SEXP stop_sym = Rf_install("stop"); \
56  try {
57 #endif
58 
59 #ifndef VOID_END_RCPP
60 #define VOID_END_RCPP \
61  } \
62  catch( Rcpp::internal::InterruptedException &__ex__) { \
63  rcpp_output_type = 1 ; \
64  } \
65  catch (Rcpp::LongjumpException& __ex__) { \
66  rcpp_output_type = 3 ; \
67  rcpp_output_condition = __ex__.token; \
68  } \
69  catch(Rcpp::exception& __ex__) { \
70  rcpp_output_type = 2 ; \
71  rcpp_output_condition = PROTECT(rcpp_exception_to_r_condition(__ex__)) ; \
72  ++nprot; \
73  } \
74  catch( std::exception& __ex__ ){ \
75  rcpp_output_type = 2 ; \
76  rcpp_output_condition = PROTECT(exception_to_r_condition(__ex__)) ; \
77  ++nprot; \
78  } \
79  catch( ... ){ \
80  rcpp_output_type = 2 ; \
81  rcpp_output_condition = PROTECT(string_to_try_error("c++ exception (unknown reason)")) ; \
82  ++nprot; \
83  } \
84  if( rcpp_output_type == 1 ){ \
85  Rf_onintr() ; \
86  } \
87  if( rcpp_output_type == 2 ){ \
88  SEXP expr = PROTECT( Rf_lang2( stop_sym , rcpp_output_condition ) ) ; \
89  ++nprot; \
90  Rf_eval( expr, R_BaseEnv ) ; \
91  } \
92  if (rcpp_output_type == 3) { \
93  Rcpp::internal::resumeJump(rcpp_output_condition); \
94  } \
95  UNPROTECT(nprot);
96 #endif
97 
98 #ifndef END_RCPP
99 #define END_RCPP VOID_END_RCPP return R_NilValue;
100 #endif
101 
102 
103 // There is no return in case of a longjump exception
104 
105 #ifndef END_RCPP_RETURN_ERROR
106 #define END_RCPP_RETURN_ERROR \
107  } \
108  catch (Rcpp::internal::InterruptedException &__ex__) { \
109  return Rcpp::internal::interruptedError(); \
110  } \
111  catch (Rcpp::LongjumpException& __ex__) { \
112  return Rcpp::internal::longjumpSentinel(__ex__.token); \
113  } \
114  catch (std::exception &__ex__) { \
115  return exception_to_try_error(__ex__); \
116  } \
117  catch (...) { \
118  return string_to_try_error("c++ exception (unknown reason)"); \
119  } \
120  UNPROTECT(nprot); \
121  return R_NilValue; \
122  (void) stop_sym; /* never reached but suppresses warning */
123 #endif
124 
125 #define Rcpp_error(MESSAGE) throw Rcpp::exception(MESSAGE, __FILE__, __LINE__)
126 
127 #include <Rcpp/macros/debug.h>
128 #include <Rcpp/macros/unroll.h>
129 #include <Rcpp/macros/dispatch.h>
130 #include <Rcpp/macros/xp.h>
131 #include <Rcpp/macros/traits.h>
132 #include <Rcpp/macros/config.hpp>
133 #include <Rcpp/macros/cat.hpp>
134 #include <Rcpp/macros/module.h>
135 #include <Rcpp/macros/interface.h>
136 
137 #endif
std::string short_file_name(const char *file)
Definition: macros.h:31
Rcpp API.
Definition: algo.h:28