|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // RcppCommon.cpp: R/C++ interface class library -- common functions 00004 // 00005 // Copyright (C) 2008 - 2009 Dirk Eddelbuettel 00006 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00007 // 00008 // This file is part of Rcpp. 00009 // 00010 // Rcpp is free software: you can redistribute it and/or modify it 00011 // under the terms of the GNU General Public License as published by 00012 // the Free Software Foundation, either version 2 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // Rcpp is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU General Public License 00021 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00022 00023 #include <Rcpp.h> 00024 #include <cstring> 00025 #include <stdio.h> 00026 00027 void logTxtFunction(const char* file, const int line, const char* expression) { 00028 Rprintf("%s:%d %s\n", file, line, expression); 00029 } 00030 00031 SEXP capabilities(){ 00032 SEXP cap = PROTECT( Rf_allocVector( LGLSXP, 8) ) ; 00033 SEXP names = PROTECT( Rf_allocVector( STRSXP, 8 ) ) ; 00034 #ifdef HAS_VARIADIC_TEMPLATES 00035 LOGICAL(cap)[0] = TRUE ; 00036 #else 00037 LOGICAL(cap)[0] = FALSE ; 00038 #endif 00039 #ifdef HAS_INIT_LISTS 00040 LOGICAL(cap)[1] = TRUE ; 00041 #else 00042 LOGICAL(cap)[1] = FALSE ; 00043 #endif 00044 /* exceptions are allways supported */ 00045 LOGICAL(cap)[2] = TRUE ; 00046 00047 #ifdef HAS_TR1_UNORDERED_MAP 00048 LOGICAL(cap)[3] = TRUE ; 00049 #else 00050 LOGICAL(cap)[3] = FALSE ; 00051 #endif 00052 00053 #ifdef HAS_TR1_UNORDERED_SET 00054 LOGICAL(cap)[4] = TRUE ; 00055 #else 00056 LOGICAL(cap)[4] = FALSE ; 00057 #endif 00058 00059 LOGICAL(cap)[5] = TRUE ; 00060 00061 #ifdef RCPP_HAS_DEMANGLING 00062 LOGICAL(cap)[6] = TRUE ; 00063 #else 00064 LOGICAL(cap)[6] = FALSE ; 00065 #endif 00066 00067 LOGICAL(cap)[7] = FALSE ; 00068 00069 SET_STRING_ELT(names, 0, Rf_mkChar("variadic templates") ) ; 00070 SET_STRING_ELT(names, 1, Rf_mkChar("initializer lists") ) ; 00071 SET_STRING_ELT(names, 2, Rf_mkChar("exception handling") ) ; 00072 SET_STRING_ELT(names, 3, Rf_mkChar("tr1 unordered maps") ) ; 00073 SET_STRING_ELT(names, 4, Rf_mkChar("tr1 unordered sets") ) ; 00074 SET_STRING_ELT(names, 5, Rf_mkChar("Rcpp modules") ) ; 00075 SET_STRING_ELT(names, 6, Rf_mkChar("demangling") ) ; 00076 SET_STRING_ELT(names, 7, Rf_mkChar("classic api") ) ; 00077 Rf_setAttrib( cap, R_NamesSymbol, names ) ; 00078 UNPROTECT(2) ; 00079 return cap ; 00080 } 00081 00082 00083 /* this is mainly here so that variadic template errors show up 00084 at compile time */ 00085 SEXP test_named(){ 00086 #ifdef HAS_VARIADIC_TEMPLATES 00087 return Rcpp::Language( "foobar", Rcpp::Named("foo", 2 ), 2, Rcpp::Named("bar", 10) ) ; 00088 #else 00089 return R_NilValue ; 00090 #endif 00091 } 00092 00093 const char * sexp_to_name(int sexp_type) { 00094 switch (sexp_type) { 00095 case NILSXP: return "NILSXP"; 00096 case SYMSXP: return "SYMSXP"; 00097 case RAWSXP: return "RAWSXP"; 00098 case LISTSXP: return "LISTSXP"; 00099 case CLOSXP: return "CLOSXP"; 00100 case ENVSXP: return "ENVSXP"; 00101 case PROMSXP: return "PROMSXP"; 00102 case LANGSXP: return "LANGSXP"; 00103 case SPECIALSXP: return "SPECIALSXP"; 00104 case BUILTINSXP: return "BUILTINSXP"; 00105 case CHARSXP: return "CHARSXP"; 00106 case LGLSXP: return "LGLSXP"; 00107 case INTSXP: return "INTSXP"; 00108 case REALSXP: return "REALSXP"; 00109 case CPLXSXP: return "CPLXSXP"; 00110 case STRSXP: return "STRSXP"; 00111 case DOTSXP: return "DOTSXP"; 00112 case ANYSXP: return "ANYSXP"; 00113 case VECSXP: return "VECSXP"; 00114 case EXPRSXP: return "EXPRSXP"; 00115 case BCODESXP: return "BCODESXP"; 00116 case EXTPTRSXP: return "EXTPTRSXP"; 00117 case WEAKREFSXP: return "WEAKREFSXP"; 00118 case S4SXP: return "S4SXP"; 00119 default: 00120 return "<unknown>"; 00121 } 00122 } 00123 00124 namespace Rcpp{ 00125 namespace internal{ 00126 00127 template<> int* r_vector_start<INTSXP,int>(SEXP x){ return INTEGER(x) ; } 00128 template<> int* r_vector_start<LGLSXP,int>(SEXP x){ return LOGICAL(x) ; } 00129 template<> double* r_vector_start<REALSXP,double>(SEXP x){ return REAL(x) ; } 00130 template<> Rbyte* r_vector_start<RAWSXP,Rbyte>(SEXP x){ return RAW(x) ; } 00131 template<> Rcomplex* r_vector_start<CPLXSXP,Rcomplex>(SEXP x){ return COMPLEX(x) ; } 00132 00133 template<> void r_init_vector<VECSXP>(SEXP x){} 00134 template<> void r_init_vector<EXPRSXP>(SEXP x){} 00135 template<> void r_init_vector<STRSXP>(SEXP x){} 00136 00137 template<> Rcomplex get_zero<CPLXSXP,Rcomplex>(){ 00138 Rcomplex x ; 00139 x.r = 0.0 ; 00140 x.i = 0.0 ; 00141 return x ; 00142 } 00143 00144 template<> Rcomplex caster<std::complex<double>, Rcomplex>( std::complex<double> from){ 00145 Rcomplex cx ; 00146 cx.r = from.real() ; 00147 cx.i = from.imag() ; 00148 return cx ; 00149 } 00150 template<> Rcomplex caster<std::complex<float>, Rcomplex>( std::complex<float> from){ 00151 Rcomplex cx ; 00152 cx.r = static_cast<double>( from.real() ); 00153 cx.i = static_cast<double>( from.imag() ); 00154 return cx ; 00155 } 00156 00157 template<> std::complex<double> caster<Rcomplex,std::complex<double> >( Rcomplex from){ 00158 return std::complex<double>(from.r, from.i ) ; 00159 } 00160 template<> std::complex<float> caster<Rcomplex,std::complex<float> >( Rcomplex from){ 00161 return std::complex<float>(static_cast<float>(from.r), static_cast<float>(from.i) ) ; 00162 } 00163 00164 int rcpp_call_test_(SEXP x){ 00165 RCPP_RETURN_VECTOR( rcpp_call_test, x ); 00166 } 00167 00168 00169 } // internal 00170 } // Rcpp 00171 00172 SEXP rcpp_call_test(SEXP x){ 00173 return Rf_ScalarInteger( ::Rcpp::internal::rcpp_call_test_(x) ) ; 00174 } 00175 00176 SEXP as_character_externalptr(SEXP xp){ 00177 char buffer[20] ; 00178 sprintf( buffer, "%p", (void*)EXTPTR_PTR(xp) ) ; 00179 return Rcpp::wrap( (const char*)buffer ) ; 00180 } 00181