-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathr_cast.h
More file actions
46 lines (37 loc) · 1.53 KB
/
r_cast.h
File metadata and controls
46 lines (37 loc) · 1.53 KB
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
#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) {
SEXP res = R_NilValue ;
try{
SEXP funSym = Rf_install(fun);
res = Rcpp_eval( Rf_lang2( funSym, x ) ) ;
} catch( eval_error& /* e */){
throw ::Rcpp::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" ) ;
}
template<> SEXP r_true_cast<INTSXP>(SEXP x) ;
template<> SEXP r_true_cast<REALSXP>(SEXP x) ;
template<> SEXP r_true_cast<RAWSXP>(SEXP x) ;
template<> SEXP r_true_cast<CPLXSXP>(SEXP x) ;
template<> SEXP r_true_cast<LGLSXP>(SEXP x) ;
template<> SEXP r_true_cast<STRSXP>(SEXP x) ;
template<> SEXP r_true_cast<VECSXP>(SEXP x) ;
template<> SEXP r_true_cast<EXPRSXP>(SEXP x) ;
template<> SEXP r_true_cast<LISTSXP>(SEXP x) ;
template<> SEXP r_true_cast<LANGSXP>(SEXP x) ;
} // namespace internal
template <int TARGET> SEXP r_cast( SEXP x) {
return (TYPEOF(x)== TARGET) ? x : internal::r_true_cast<TARGET>(x) ;
}
} // namespace Rcpp
#endif