Rcpp Version 1.1.2
Loading...
Searching...
No Matches
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 - 2025 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
32#ifdef RCPP_HAS_DEMANGLING
33#include <cxxabi.h>
34#endif
35
36namespace Rcpp {
37 // [[Rcpp::register]]
39 static Rostream<true> Rcpp_cout;
40 return Rcpp_cout;
41 }
42 // [[Rcpp::register]]
44 static Rostream<false> Rcpp_cerr;
45 return Rcpp_cerr;
46 }
49
50 namespace internal {
51
53
54 // [[Rcpp::register]]
55 unsigned long enterRNGScope() {
57 GetRNGstate();
58 return 0;
59 }
60
61 // [[Rcpp::register]]
62 unsigned long exitRNGScope() {
64 PutRNGstate();
65 return 0;
66 }
67
68 // [[Rcpp::register]]
69 unsigned long beginSuspendRNGSynchronization() { // #nocov start
72 }
73
74 // [[Rcpp::register]]
75 unsigned long endSuspendRNGSynchronization() {
78 } // #nocov end
79
80 // [[Rcpp::register]]
81 char* get_string_buffer() {
82 static char buffer[MAXELTSIZE];
83 return buffer;
84 }
85
86 }
87
88 // [[Rcpp::register]]
89 const char * type2name(SEXP x) { // #nocov start
90 switch (TYPEOF(x)) {
91 case NILSXP: return "NILSXP";
92 case SYMSXP: return "SYMSXP";
93 case RAWSXP: return "RAWSXP";
94 case LISTSXP: return "LISTSXP";
95 case CLOSXP: return "CLOSXP";
96 case ENVSXP: return "ENVSXP";
97 case PROMSXP: return "PROMSXP";
98 case LANGSXP: return "LANGSXP";
99 case SPECIALSXP: return "SPECIALSXP";
100 case BUILTINSXP: return "BUILTINSXP";
101 case CHARSXP: return "CHARSXP";
102 case LGLSXP: return "LGLSXP";
103 case INTSXP: return "INTSXP";
104 case REALSXP: return "REALSXP";
105 case CPLXSXP: return "CPLXSXP";
106 case STRSXP: return "STRSXP";
107 case DOTSXP: return "DOTSXP";
108 case ANYSXP: return "ANYSXP";
109 case VECSXP: return "VECSXP";
110 case EXPRSXP: return "EXPRSXP";
111 case BCODESXP: return "BCODESXP";
112 case EXTPTRSXP: return "EXTPTRSXP";
113 case WEAKREFSXP: return "WEAKREFSXP";
114#if R_VERSION >= R_Version(4,4,0) // replaces S4SXP in R 4.4.0
115 case OBJSXP: return Rf_isS4(x) ? "S4SXP" : "OBJSXP"; // cf src/main/inspect.c
116#else
117 case S4SXP: return "S4SXP";
118#endif
119 default:
120 return "<unknown>";
121 }
122 } // #nocov end
123
124
125} // namespace Rcpp
126
127// [[Rcpp::register]]
128std::string demangle(const std::string& name) {
129 #ifdef RCPP_HAS_DEMANGLING
130 std::string real_class;
131 int status =-1;
132 char *dem = 0;
133 dem = abi::__cxa_demangle(name.c_str(), 0, 0, &status);
134 if (status == 0) {
135 real_class = dem;
136 free(dem);
137 } else {
138 real_class = name;
139 }
140 return real_class;
141 #else
142 return name;
143 #endif
144}
145
146// NOTE: remains registered but this routine is now effectively unused by Rcpp;
147// we retain it for backwards compatibility with any existing packages which
148// (explicitly or implicitly) rely on its existence. See also:
149// https://github.com/RcppCore/Rcpp/issues/1066
150
151// [[Rcpp::register]]
152const char* short_file_name(const char* file) { // #nocov start
153 static std::string f;
154 f = file;
155 size_t index = f.find("/include/");
156 if (index != std::string::npos) {
157 f = f.substr(index + 9);
158 }
159 return f.c_str();
160}
161
162// [[Rcpp::internal]]
164 char buffer[20];
165 snprintf(buffer, 20, "%p", (void*)R_ExternalPtrAddr(xp));
166 return Rcpp::wrap((const char*)buffer);
167} // #nocov end
168
169// [[Rcpp::internal]]
171 Shield<SEXP> cap(Rf_allocVector(LGLSXP, 13));
172 Shield<SEXP> names(Rf_allocVector(STRSXP, 13));
173 LOGICAL(cap)[0] = TRUE; // HAS_VARIADIC_TEMPLATES
174 LOGICAL(cap)[1] = TRUE; // HAS_CXX0X_INITIALIZER_LIST
175 LOGICAL(cap)[2] = TRUE; /* exceptions are always supported */
176 LOGICAL(cap)[3] = TRUE; // HAS_TR1_UNORDERED_MAP
177 LOGICAL(cap)[4] = TRUE; // HAS_TR1_UNORDERED_SET
178 LOGICAL(cap)[5] = TRUE; // Modules (TODO respect header choice ?)
179
180 #ifdef RCPP_HAS_DEMANGLING
181 LOGICAL(cap)[6] = TRUE;
182 #else
183 LOGICAL(cap)[6] = FALSE;
184 #endif
185
186 LOGICAL(cap)[7] = FALSE; // Classic API
187 LOGICAL(cap)[8] = TRUE; // RCPP_HAS_LONG_LONG_TYPES
188 LOGICAL(cap)[9] = TRUE; // HAS_CXX0X_UNORDERED_MAP
189 LOGICAL(cap)[10] = TRUE; // HAS_CXX0X_UNORDERED_SET
190 LOGICAL(cap)[11] = TRUE; // RCPP_USING_CXX11
191
192 #ifdef RCPP_NEW_DATE_DATETIME_VECTORS
193 LOGICAL(cap)[12] = TRUE;
194 #else
195 LOGICAL(cap)[12] = FALSE;
196 #endif
197
198
199 SET_STRING_ELT(names, 0, Rf_mkChar("variadic templates"));
200 SET_STRING_ELT(names, 1, Rf_mkChar("initializer lists"));
201 SET_STRING_ELT(names, 2, Rf_mkChar("exception handling"));
202 SET_STRING_ELT(names, 3, Rf_mkChar("tr1 unordered maps"));
203 SET_STRING_ELT(names, 4, Rf_mkChar("tr1 unordered sets"));
204 SET_STRING_ELT(names, 5, Rf_mkChar("Rcpp modules"));
205 SET_STRING_ELT(names, 6, Rf_mkChar("demangling"));
206 SET_STRING_ELT(names, 7, Rf_mkChar("classic api"));
207 SET_STRING_ELT(names, 8, Rf_mkChar("long long"));
208 SET_STRING_ELT(names, 9, Rf_mkChar("C++0x unordered maps"));
209 SET_STRING_ELT(names, 10, Rf_mkChar("C++0x unordered sets"));
210 SET_STRING_ELT(names, 11, Rf_mkChar("Full C++11 support"));
211 SET_STRING_ELT(names, 12, Rf_mkChar("new date(time) vectors"));
212 Rf_setAttrib(cap, R_NamesSymbol, names);
213 return cap;
214}
215
216
217// [[Rcpp::internal]]
218SEXP rcpp_can_use_cxx0x() { // #nocov start
219 return Rf_ScalarLogical(TRUE);
220}
221
222
223// [[Rcpp::internal]]
225 return Rf_ScalarLogical(TRUE);
226}
227
228// [[Rcpp::register]]
229SEXP stack_trace(const char* file, int line) {
230 return R_NilValue;
231} // #nocov end
232
233
234// // [ [ Rcpp::register ] ]
235// void print(SEXP s) {
236// Rf_PrintValue(s); // defined in Rinternals.h
237// }
238
239// }}}
240
241
242// [[Rcpp::internal]]
244 Shield<SEXP> versionstring(Rf_allocVector(STRSXP,2));
245 SET_STRING_ELT(versionstring, 0, Rf_mkChar(RCPP_VERSION_STRING));
246 SET_STRING_ELT(versionstring, 1, Rf_mkChar(RCPP_DEV_VERSION_STRING));
247 return versionstring;
248}
SEXP as_character_externalptr(SEXP xp)
Definition api.cpp:163
std::string demangle(const std::string &name)
Definition api.cpp:128
SEXP rcpp_capabilities()
Definition api.cpp:170
const char * short_file_name(const char *file)
Definition api.cpp:152
SEXP getRcppVersionStrings()
Definition api.cpp:243
SEXP rcpp_can_use_cxx11()
Definition api.cpp:224
SEXP rcpp_can_use_cxx0x()
Definition api.cpp:218
SEXP stack_trace(const char *file, int line)
Definition api.cpp:229
#define RCPP_VERSION_STRING
Definition config.h:30
#define RCPP_DEV_VERSION_STRING
Definition config.h:34
#define MAXELTSIZE
Definition headers.h:27
internal implementation details
Definition Date.h:43
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
std::string const_string_proxy< RTYPE, StoragePolicy >::buffer
attribute_hidden char * get_string_buffer()
Definition routines.h:115
int rngSynchronizationSuspended
Definition api.cpp:52
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
SEXP wrap(const Date &date)
Definition Date.h:38