1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// rcast.h: Rcpp R/C++ interface class library -- cast from one SEXP type to another
//
// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
#ifndef Rcpp_rcast_h
#define Rcpp_rcast_h
#include <Rcpp/exceptions.h>
namespace Rcpp{
namespace internal {
inline SEXP convert_using_rfunction(SEXP x, const char* const fun){
Armor<SEXP> res ;
try{
SEXP funSym = Rf_install(fun);
res = Rcpp_eval( Rf_lang2( funSym, x ) ) ;
} catch( eval_error& e){
throw not_compatible( std::string("could not convert using R function : ") + fun ) ;
}
return res;
}
// r_true_cast is only meant to be used when the target SEXP type
// is different from the SEXP type of x
template <int TARGET>
SEXP r_true_cast( SEXP x) {
throw not_compatible( "not compatible" ) ;
return x ; // makes solaris happy
}
template <int RTYPE>
SEXP basic_cast( SEXP x){
if( TYPEOF(x) == RTYPE ) return x ;
switch( TYPEOF(x) ){
case REALSXP:
case RAWSXP:
case LGLSXP:
case CPLXSXP:
case INTSXP:
return Rf_coerceVector( x, RTYPE) ;
default:
throw ::Rcpp::not_compatible( "not compatible with requested type" ) ;
}
return R_NilValue ; /* -Wall */
}
template<>
inline SEXP r_true_cast<INTSXP>(SEXP x){
return basic_cast<INTSXP>(x) ;
}
template<>
inline SEXP r_true_cast<REALSXP>(SEXP x){
return basic_cast<REALSXP>(x) ;
}
template<>
inline SEXP r_true_cast<RAWSXP>(SEXP x){
return basic_cast<RAWSXP>(x) ;
}
template<>
inline SEXP r_true_cast<CPLXSXP>(SEXP x){
return basic_cast<CPLXSXP>(x) ;
}
template<>
inline SEXP r_true_cast<LGLSXP>(SEXP x){
return basic_cast<LGLSXP>(x) ;
}
template <>
inline SEXP r_true_cast<STRSXP>(SEXP x){
switch( TYPEOF( x ) ){
case CPLXSXP:
case RAWSXP:
case LGLSXP:
case REALSXP:
case INTSXP:
{
// return Rf_coerceVector( x, STRSXP );
// coerceVector does not work for some reason
Shield<SEXP> call( Rf_lang2( Rf_install( "as.character" ), x ) ) ;
Shield<SEXP> res( Rf_eval( call, R_GlobalEnv ) ) ;
return res ;
}
case CHARSXP:
return Rf_ScalarString( x ) ;
case SYMSXP:
return Rf_ScalarString( PRINTNAME( x ) ) ;
default:
throw ::Rcpp::not_compatible( "not compatible with STRSXP" ) ;
}
return R_NilValue ; /* -Wall */
}
template<>
inline SEXP r_true_cast<VECSXP>(SEXP x) {
return convert_using_rfunction(x, "as.list" ) ;
}
template<>
inline SEXP r_true_cast<EXPRSXP>(SEXP x) {
return convert_using_rfunction(x, "as.expression" ) ;
}
template<>
inline SEXP r_true_cast<LISTSXP>(SEXP x) {
switch( TYPEOF(x) ){
case LANGSXP:
{
Shield<SEXP> y( Rf_duplicate( x ));
SET_TYPEOF(y,LISTSXP) ;
return y ;
}
default:
return convert_using_rfunction(x, "as.pairlist" ) ;
}
}
template<>
inline SEXP r_true_cast<LANGSXP>(SEXP x) {
return convert_using_rfunction(x, "as.call" ) ;
}
} // namespace internal
template <int TARGET> SEXP r_cast(SEXP x) {
if (TYPEOF(x) == TARGET) {
return x;
} else {
#ifdef RCPP_WARN_ON_COERCE
Shield<SEXP> result( internal::r_true_cast<TARGET>(x) );
Rf_warning("coerced object from '%s' to '%s'",
CHAR(Rf_type2str(TYPEOF(x))),
CHAR(Rf_type2str(TARGET))
);
return result;
#else
return internal::r_true_cast<TARGET>(x);
#endif
}
}
} // namespace Rcpp
#endif
|