Rcpp Version 1.0.9
api.cpp
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // api.cpp: Rcpp R/C++ interface class library -- Rcpp api
4 //
5 // Copyright (C) 2012 - 2020 Dirk Eddelbuettel and Romain Francois
6 // Copyright (C) 2021 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
7 //
8 // This file is part of Rcpp.
9 //
10 // Rcpp is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 2 of the License, or
13 // (at your option) any later version.
14 //
15 // Rcpp is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22 
23 #define COMPILING_RCPP
24 #define RCPP_USE_GLOBAL_ROSTREAM
25 
26 #include <Rcpp.h>
27 
28 using namespace Rcpp;
29 
30 #include "internal.h"
31 #include <R_ext/PrtUtil.h>
32 
33 #ifdef RCPP_HAS_DEMANGLING
34 #include <cxxabi.h>
35 #endif
36 
37 namespace Rcpp {
38  // [[Rcpp::register]]
40  static Rostream<true> Rcpp_cout;
41  return Rcpp_cout;
42  }
43  // [[Rcpp::register]]
45  static Rostream<false> Rcpp_cerr;
46  return Rcpp_cerr;
47  }
50 
51  namespace internal {
52 
54 
55  // [[Rcpp::register]]
56  unsigned long enterRNGScope() {
58  GetRNGstate();
59  return 0;
60  }
61 
62  // [[Rcpp::register]]
63  unsigned long exitRNGScope() {
65  PutRNGstate();
66  return 0;
67  }
68 
69  // [[Rcpp::register]]
70  unsigned long beginSuspendRNGSynchronization() { // #nocov start
73  }
74 
75  // [[Rcpp::register]]
76  unsigned long endSuspendRNGSynchronization() {
79  } // #nocov end
80 
81  // [[Rcpp::register]]
82  char* get_string_buffer() {
83  static char buffer[MAXELTSIZE];
84  return buffer;
85  }
86 
87  }
88 
89  // [[Rcpp::register]]
90  const char * type2name(SEXP x) { // #nocov start
91  switch (TYPEOF(x)) {
92  case NILSXP: return "NILSXP";
93  case SYMSXP: return "SYMSXP";
94  case RAWSXP: return "RAWSXP";
95  case LISTSXP: return "LISTSXP";
96  case CLOSXP: return "CLOSXP";
97  case ENVSXP: return "ENVSXP";
98  case PROMSXP: return "PROMSXP";
99  case LANGSXP: return "LANGSXP";
100  case SPECIALSXP: return "SPECIALSXP";
101  case BUILTINSXP: return "BUILTINSXP";
102  case CHARSXP: return "CHARSXP";
103  case LGLSXP: return "LGLSXP";
104  case INTSXP: return "INTSXP";
105  case REALSXP: return "REALSXP";
106  case CPLXSXP: return "CPLXSXP";
107  case STRSXP: return "STRSXP";
108  case DOTSXP: return "DOTSXP";
109  case ANYSXP: return "ANYSXP";
110  case VECSXP: return "VECSXP";
111  case EXPRSXP: return "EXPRSXP";
112  case BCODESXP: return "BCODESXP";
113  case EXTPTRSXP: return "EXTPTRSXP";
114  case WEAKREFSXP: return "WEAKREFSXP";
115  case S4SXP: return "S4SXP";
116  default:
117  return "<unknown>";
118  }
119  } // #nocov end
120 
121 
122 } // namespace Rcpp
123 
124 // [[Rcpp::register]]
125 std::string demangle(const std::string& name) {
126  #ifdef RCPP_HAS_DEMANGLING
127  std::string real_class;
128  int status =-1;
129  char *dem = 0;
130  dem = abi::__cxa_demangle(name.c_str(), 0, 0, &status);
131  if (status == 0) {
132  real_class = dem;
133  free(dem);
134  } else {
135  real_class = name;
136  }
137  return real_class;
138  #else
139  return name;
140  #endif
141 }
142 
143 // NOTE: remains registered but this routine is now effectively unused by Rcpp;
144 // we retain it for backwards compatibility with any existing packages which
145 // (explicitly or implicitly) rely on its existence. See also:
146 // https://github.com/RcppCore/Rcpp/issues/1066
147 
148 // [[Rcpp::register]]
149 const char* short_file_name(const char* file) { // #nocov start
150  static std::string f;
151  f = file;
152  size_t index = f.find("/include/");
153  if (index != std::string::npos) {
154  f = f.substr(index + 9);
155  }
156  return f.c_str();
157 }
158 
159 // [[Rcpp::internal]]
161  char buffer[20];
162  snprintf(buffer, 20, "%p", (void*)R_ExternalPtrAddr(xp));
163  return Rcpp::wrap((const char*)buffer);
164 } // #nocov end
165 
166 // [[Rcpp::internal]]
168  Shield<SEXP> cap(Rf_allocVector(LGLSXP, 13));
169  Shield<SEXP> names(Rf_allocVector(STRSXP, 13));
170  #ifdef HAS_VARIADIC_TEMPLATES
171  LOGICAL(cap)[0] = TRUE;
172  #else
173  LOGICAL(cap)[0] = FALSE;
174  #endif
175  #ifdef HAS_CXX0X_INITIALIZER_LIST
176  LOGICAL(cap)[1] = TRUE;
177  #else
178  LOGICAL(cap)[1] = FALSE;
179  #endif
180  /* exceptions are always supported */
181  LOGICAL(cap)[2] = TRUE;
182 
183  #ifdef HAS_TR1_UNORDERED_MAP
184  LOGICAL(cap)[3] = TRUE;
185  #else
186  LOGICAL(cap)[3] = FALSE;
187  #endif
188 
189  #ifdef HAS_TR1_UNORDERED_SET
190  LOGICAL(cap)[4] = TRUE;
191  #else
192  LOGICAL(cap)[4] = FALSE;
193  #endif
194 
195  LOGICAL(cap)[5] = TRUE;
196 
197  #ifdef RCPP_HAS_DEMANGLING
198  LOGICAL(cap)[6] = TRUE;
199  #else
200  LOGICAL(cap)[6] = FALSE;
201  #endif
202 
203  LOGICAL(cap)[7] = FALSE;
204 
205  #ifdef RCPP_HAS_LONG_LONG_TYPES
206  LOGICAL(cap)[8] = TRUE;
207  #else
208  LOGICAL(cap)[8] = FALSE;
209  #endif
210 
211  #ifdef HAS_CXX0X_UNORDERED_MAP
212  LOGICAL(cap)[9] = TRUE;
213  #else
214  LOGICAL(cap)[9] = FALSE;
215  #endif
216 
217  #ifdef HAS_CXX0X_UNORDERED_SET
218  LOGICAL(cap)[10] = TRUE;
219  #else
220  LOGICAL(cap)[10] = FALSE;
221  #endif
222 
223  #ifdef RCPP_USING_CXX11
224  LOGICAL(cap)[11] = TRUE;
225  #else
226  LOGICAL(cap)[11] = FALSE;
227  #endif
228 
229  #ifdef RCPP_NEW_DATE_DATETIME_VECTORS
230  LOGICAL(cap)[12] = TRUE;
231  #else
232  LOGICAL(cap)[12] = FALSE;
233  #endif
234 
235 
236  SET_STRING_ELT(names, 0, Rf_mkChar("variadic templates"));
237  SET_STRING_ELT(names, 1, Rf_mkChar("initializer lists"));
238  SET_STRING_ELT(names, 2, Rf_mkChar("exception handling"));
239  SET_STRING_ELT(names, 3, Rf_mkChar("tr1 unordered maps"));
240  SET_STRING_ELT(names, 4, Rf_mkChar("tr1 unordered sets"));
241  SET_STRING_ELT(names, 5, Rf_mkChar("Rcpp modules"));
242  SET_STRING_ELT(names, 6, Rf_mkChar("demangling"));
243  SET_STRING_ELT(names, 7, Rf_mkChar("classic api"));
244  SET_STRING_ELT(names, 8, Rf_mkChar("long long"));
245  SET_STRING_ELT(names, 9, Rf_mkChar("C++0x unordered maps"));
246  SET_STRING_ELT(names, 10, Rf_mkChar("C++0x unordered sets"));
247  SET_STRING_ELT(names, 11, Rf_mkChar("Full C++11 support"));
248  SET_STRING_ELT(names, 12, Rf_mkChar("new date(time) vectors"));
249  Rf_setAttrib(cap, R_NamesSymbol, names);
250  return cap;
251 }
252 
253 
254 // [[Rcpp::internal]]
255 SEXP rcpp_can_use_cxx0x() { // #nocov start
256  #if defined(HAS_VARIADIC_TEMPLATES) || defined(RCPP_USING_CXX11)
257  return Rf_ScalarLogical(TRUE);
258  #else
259  return Rf_ScalarLogical(FALSE);
260  #endif
261 }
262 
263 
264 // [[Rcpp::internal]]
266  #if defined(RCPP_USING_CXX11)
267  return Rf_ScalarLogical(TRUE);
268  #else
269  return Rf_ScalarLogical(FALSE);
270  #endif
271 }
272 
273 
274 // [[Rcpp::register]]
275 SEXP stack_trace(const char* file, int line) {
276  return R_NilValue;
277 } // #nocov end
278 
279 
280 // // [ [ Rcpp::register ] ]
281 // void print(SEXP s) {
282 // Rf_PrintValue(s); // defined in Rinternals.h
283 // }
284 
285 // }}}
286 
287 
288 // [[Rcpp::internal]]
290  Shield<SEXP> versionstring(Rf_allocVector(STRSXP,2));
291  SET_STRING_ELT(versionstring, 0, Rf_mkChar(RCPP_VERSION_STRING));
292  SET_STRING_ELT(versionstring, 1, Rf_mkChar(RCPP_DEV_VERSION_STRING));
293  return versionstring;
294 }
SEXP as_character_externalptr(SEXP xp)
Definition: api.cpp:160
std::string demangle(const std::string &name)
Definition: api.cpp:125
SEXP rcpp_capabilities()
Definition: api.cpp:167
SEXP getRcppVersionStrings()
Definition: api.cpp:289
const char * short_file_name(const char *file)
Definition: api.cpp:149
SEXP rcpp_can_use_cxx11()
Definition: api.cpp:265
SEXP rcpp_can_use_cxx0x()
Definition: api.cpp:255
SEXP stack_trace(const char *file, int line)
Definition: api.cpp:275
#define RCPP_VERSION_STRING
Definition: config.h:30
#define RCPP_DEV_VERSION_STRING
Definition: config.h:34
#define MAXELTSIZE
Definition: headers.h:25
attribute_hidden unsigned long exitRNGScope()
Definition: routines.h:97
attribute_hidden unsigned long beginSuspendRNGSynchronization()
Definition: routines.h:103
attribute_hidden unsigned long enterRNGScope()
Definition: routines.h:91
attribute_hidden char * get_string_buffer()
Definition: routines.h:115
int rngSynchronizationSuspended
Definition: api.cpp:53
attribute_hidden unsigned long endSuspendRNGSynchronization()
Definition: routines.h:109
Rcpp API.
Definition: algo.h:28
attribute_hidden Rostream< true > & Rcpp_cout_get()
Definition: routines.h:163
attribute_hidden const char * type2name(SEXP x)
Definition: routines.h:84
attribute_hidden Rostream< false > & Rcpp_cerr_get()
Definition: routines.h:168
static Rostream< false > Rcerr
Definition: Rstreambuf.h:90
static Rostream< true > Rcout
Definition: Rstreambuf.h:89
SEXP wrap(const Date &date)
Definition: Date.h:38