Rcpp Version 1.0.14
All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 - 2023 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
28using 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
37namespace Rcpp {
38 // [[Rcpp::register]]
41 return Rcpp_cout;
42 }
43 // [[Rcpp::register]]
46 return Rcpp_cerr;
47 }
50
51 namespace internal {
52
54
55 // [[Rcpp::register]]
56 unsigned long enterRNGScope() {
59 return 0;
60 }
61
62 // [[Rcpp::register]]
63 unsigned long exitRNGScope() {
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#if R_VERSION >= R_Version(4,4,0) // replaces S4SXP in R 4.4.0
116 case OBJSXP: return Rf_isS4(x) ? "S4SXP" : "OBJSXP"; // cf src/main/inspect.c
117#else
118 case S4SXP: return "S4SXP";
119#endif
120 default:
121 return "<unknown>";
122 }
123 } // #nocov end
124
125
126} // namespace Rcpp
127
128// [[Rcpp::register]]
129std::string demangle(const std::string& name) {
130 #ifdef RCPP_HAS_DEMANGLING
131 std::string real_class;
132 int status =-1;
133 char *dem = 0;
134 dem = abi::__cxa_demangle(name.c_str(), 0, 0, &status);
135 if (status == 0) {
136 real_class = dem;
137 free(dem);
138 } else {
139 real_class = name;
140 }
141 return real_class;
142 #else
143 return name;
144 #endif
145}
146
147// NOTE: remains registered but this routine is now effectively unused by Rcpp;
148// we retain it for backwards compatibility with any existing packages which
149// (explicitly or implicitly) rely on its existence. See also:
150// https://github.com/RcppCore/Rcpp/issues/1066
151
152// [[Rcpp::register]]
153const char* short_file_name(const char* file) { // #nocov start
154 static std::string f;
155 f = file;
156 size_t index = f.find("/include/");
157 if (index != std::string::npos) {
158 f = f.substr(index + 9);
159 }
160 return f.c_str();
161}
162
163// [[Rcpp::internal]]
165 char buffer[20];
166 snprintf(buffer, 20, "%p", (void*)R_ExternalPtrAddr(xp));
167 return Rcpp::wrap((const char*)buffer);
168} // #nocov end
169
170// [[Rcpp::internal]]
174 #ifdef HAS_VARIADIC_TEMPLATES
175 LOGICAL(cap)[0] = TRUE;
176 #else
177 LOGICAL(cap)[0] = FALSE;
178 #endif
179 #ifdef HAS_CXX0X_INITIALIZER_LIST
180 LOGICAL(cap)[1] = TRUE;
181 #else
182 LOGICAL(cap)[1] = FALSE;
183 #endif
184 /* exceptions are always supported */
185 LOGICAL(cap)[2] = TRUE;
186
187 #ifdef HAS_TR1_UNORDERED_MAP
188 LOGICAL(cap)[3] = TRUE;
189 #else
190 LOGICAL(cap)[3] = FALSE;
191 #endif
192
193 #ifdef HAS_TR1_UNORDERED_SET
194 LOGICAL(cap)[4] = TRUE;
195 #else
196 LOGICAL(cap)[4] = FALSE;
197 #endif
198
199 LOGICAL(cap)[5] = TRUE;
200
201 #ifdef RCPP_HAS_DEMANGLING
202 LOGICAL(cap)[6] = TRUE;
203 #else
204 LOGICAL(cap)[6] = FALSE;
205 #endif
206
207 LOGICAL(cap)[7] = FALSE;
208
209 #ifdef RCPP_HAS_LONG_LONG_TYPES
210 LOGICAL(cap)[8] = TRUE;
211 #else
212 LOGICAL(cap)[8] = FALSE;
213 #endif
214
215 #ifdef HAS_CXX0X_UNORDERED_MAP
216 LOGICAL(cap)[9] = TRUE;
217 #else
218 LOGICAL(cap)[9] = FALSE;
219 #endif
220
221 #ifdef HAS_CXX0X_UNORDERED_SET
222 LOGICAL(cap)[10] = TRUE;
223 #else
224 LOGICAL(cap)[10] = FALSE;
225 #endif
226
227 #ifdef RCPP_USING_CXX11
228 LOGICAL(cap)[11] = TRUE;
229 #else
230 LOGICAL(cap)[11] = FALSE;
231 #endif
232
233 #ifdef RCPP_NEW_DATE_DATETIME_VECTORS
234 LOGICAL(cap)[12] = TRUE;
235 #else
236 LOGICAL(cap)[12] = FALSE;
237 #endif
238
239
240 SET_STRING_ELT(names, 0, Rf_mkChar("variadic templates"));
241 SET_STRING_ELT(names, 1, Rf_mkChar("initializer lists"));
242 SET_STRING_ELT(names, 2, Rf_mkChar("exception handling"));
243 SET_STRING_ELT(names, 3, Rf_mkChar("tr1 unordered maps"));
244 SET_STRING_ELT(names, 4, Rf_mkChar("tr1 unordered sets"));
245 SET_STRING_ELT(names, 5, Rf_mkChar("Rcpp modules"));
246 SET_STRING_ELT(names, 6, Rf_mkChar("demangling"));
247 SET_STRING_ELT(names, 7, Rf_mkChar("classic api"));
248 SET_STRING_ELT(names, 8, Rf_mkChar("long long"));
249 SET_STRING_ELT(names, 9, Rf_mkChar("C++0x unordered maps"));
250 SET_STRING_ELT(names, 10, Rf_mkChar("C++0x unordered sets"));
251 SET_STRING_ELT(names, 11, Rf_mkChar("Full C++11 support"));
252 SET_STRING_ELT(names, 12, Rf_mkChar("new date(time) vectors"));
254 return cap;
255}
256
257
258// [[Rcpp::internal]]
259SEXP rcpp_can_use_cxx0x() { // #nocov start
260 #if defined(HAS_VARIADIC_TEMPLATES)
261 return Rf_ScalarLogical(TRUE);
262 #else
263 return Rf_ScalarLogical(FALSE);
264 #endif
265}
266
267
268// [[Rcpp::internal]]
270 #if defined(RCPP_USING_CXX11)
271 return Rf_ScalarLogical(TRUE);
272 #else
273 return Rf_ScalarLogical(FALSE);
274 #endif
275}
276
277
278// [[Rcpp::register]]
279SEXP stack_trace(const char* file, int line) {
280 return R_NilValue;
281} // #nocov end
282
283
284// // [ [ Rcpp::register ] ]
285// void print(SEXP s) {
286// Rf_PrintValue(s); // defined in Rinternals.h
287// }
288
289// }}}
290
291
292// [[Rcpp::internal]]
SEXP as_character_externalptr(SEXP xp)
Definition api.cpp:164
std::string demangle(const std::string &name)
Definition api.cpp:129
SEXP rcpp_capabilities()
Definition api.cpp:171
const char * short_file_name(const char *file)
Definition api.cpp:153
SEXP getRcppVersionStrings()
Definition api.cpp:293
SEXP rcpp_can_use_cxx11()
Definition api.cpp:269
SEXP rcpp_can_use_cxx0x()
Definition api.cpp:259
SEXP stack_trace(const char *file, int line)
Definition api.cpp:279
#define RCPP_VERSION_STRING
Definition config.h:30
#define RCPP_DEV_VERSION_STRING
Definition config.h:34
#define MAXELTSIZE
Definition headers.h:26
attribute_hidden unsigned long exitRNGScope()
Definition routines.h:97
T as(SEXP x, ::Rcpp::traits::r_type_primitive_tag)
Definition as.h:43
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:89
static Rostream< true > Rcout
Definition Rstreambuf.h:88
T as(SEXP x)
Definition as.h:151
SEXP wrap(const Date &date)
Definition Date.h:38