26
26
27
27
namespace Rcpp {
28
28
namespace internal {
29
-
29
+
30
30
inline SEXP convert_using_rfunction (SEXP x, const char * const fun){
31
31
Armor<SEXP> res ;
32
32
try {
@@ -37,15 +37,15 @@ namespace Rcpp{
37
37
}
38
38
return res;
39
39
}
40
-
40
+
41
41
// r_true_cast is only meant to be used when the target SEXP type
42
- // is different from the SEXP type of x
42
+ // is different from the SEXP type of x
43
43
template <int TARGET>
44
44
SEXP r_true_cast ( SEXP x) {
45
45
throw not_compatible ( " not compatible" ) ;
46
46
return x ; // makes solaris happy
47
47
}
48
-
48
+
49
49
template <int RTYPE>
50
50
SEXP basic_cast ( SEXP x){
51
51
if ( TYPEOF (x) == RTYPE ) return x ;
@@ -62,28 +62,28 @@ namespace Rcpp{
62
62
return R_NilValue ; /* -Wall */
63
63
}
64
64
65
- template <>
65
+ template <>
66
66
inline SEXP r_true_cast<INTSXP>(SEXP x){
67
- return basic_cast<INTSXP>(x) ;
67
+ return basic_cast<INTSXP>(x) ;
68
68
}
69
- template <>
69
+ template <>
70
70
inline SEXP r_true_cast<REALSXP>(SEXP x){
71
- return basic_cast<REALSXP>(x) ;
71
+ return basic_cast<REALSXP>(x) ;
72
72
}
73
- template <>
73
+ template <>
74
74
inline SEXP r_true_cast<RAWSXP>(SEXP x){
75
- return basic_cast<RAWSXP>(x) ;
75
+ return basic_cast<RAWSXP>(x) ;
76
76
}
77
- template <>
77
+ template <>
78
78
inline SEXP r_true_cast<CPLXSXP>(SEXP x){
79
79
return basic_cast<CPLXSXP>(x) ;
80
80
}
81
- template <>
81
+ template <>
82
82
inline SEXP r_true_cast<LGLSXP>(SEXP x){
83
83
return basic_cast<LGLSXP>(x) ;
84
84
}
85
-
86
- template <>
85
+
86
+ template <>
87
87
inline SEXP r_true_cast<STRSXP>(SEXP x){
88
88
switch ( TYPEOF ( x ) ){
89
89
case CPLXSXP:
@@ -101,42 +101,55 @@ namespace Rcpp{
101
101
case CHARSXP:
102
102
return Rf_ScalarString ( x ) ;
103
103
case SYMSXP:
104
- return Rf_ScalarString ( PRINTNAME ( x ) ) ;
104
+ return Rf_ScalarString ( PRINTNAME ( x ) ) ;
105
105
default :
106
106
throw ::Rcpp::not_compatible ( " not compatible with STRSXP" ) ;
107
107
}
108
108
return R_NilValue ; /* -Wall */
109
109
}
110
- template <>
110
+ template <>
111
111
inline SEXP r_true_cast<VECSXP>(SEXP x) {
112
- return convert_using_rfunction (x, " as.list" ) ;
112
+ return convert_using_rfunction (x, " as.list" ) ;
113
113
}
114
- template <>
114
+ template <>
115
115
inline SEXP r_true_cast<EXPRSXP>(SEXP x) {
116
116
return convert_using_rfunction (x, " as.expression" ) ;
117
117
}
118
- template <>
118
+ template <>
119
119
inline SEXP r_true_cast<LISTSXP>(SEXP x) {
120
120
switch ( TYPEOF (x) ){
121
121
case LANGSXP:
122
122
{
123
- Shield<SEXP> y ( Rf_duplicate ( x ));
123
+ Shield<SEXP> y ( Rf_duplicate ( x ));
124
124
SET_TYPEOF (y,LISTSXP) ;
125
125
return y ;
126
126
}
127
127
default :
128
128
return convert_using_rfunction (x, " as.pairlist" ) ;
129
129
}
130
130
}
131
- template <>
131
+ template <>
132
132
inline SEXP r_true_cast<LANGSXP>(SEXP x) {
133
- return convert_using_rfunction (x, " as.call" ) ;
133
+ return convert_using_rfunction (x, " as.call" ) ;
134
134
}
135
135
136
- } // namespace internal
136
+ } // namespace internal
137
137
138
- template <int TARGET> SEXP r_cast ( SEXP x) {
139
- return (TYPEOF (x)== TARGET) ? x : internal::r_true_cast<TARGET>(x) ;
138
+ template <int TARGET> SEXP r_cast (SEXP x) {
139
+ if (TYPEOF (x) == TARGET) {
140
+ return x;
141
+ } else {
142
+ #ifndef RCPP_DONT_WARN_ON_COERCE
143
+ Shield<SEXP> result ( internal::r_true_cast<TARGET>(x) );
144
+ Rf_warning (" coerced object from '%s' to '%s'" ,
145
+ CHAR (Rf_type2str (TYPEOF (x))),
146
+ CHAR (Rf_type2str (TARGET))
147
+ );
148
+ return result;
149
+ #else
150
+ return internal::r_true_cast<TARGET>(x);
151
+ #endif
152
+ }
140
153
}
141
154
142
155
} // namespace Rcpp
0 commit comments