File: r_cast.h

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (178 lines) | stat: -rw-r--r-- 6,100 bytes parent folder | download | duplicates (2)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// -*- 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 - 2017  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) { 	// #nocov start
            Armor<SEXP> res;
            try{
                SEXP funSym = Rf_install(fun);
                Shield<SEXP> call(Rf_lang2(funSym, x));
                res = Rcpp_fast_eval(call, R_GlobalEnv);
            } catch( eval_error& e) {
                const char* fmt = "Could not convert using R function: %s.";
                throw not_compatible(fmt, fun);
            }
            return res;								// #nocov end
        }

        // 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) {

            const char* fmt = "Not compatible conversion to target: "
                              "[type=%s; target=%s].";

            throw not_compatible(fmt,
                                 Rf_type2char(TYPEOF(x)),
                                 Rf_type2char(TARGET));

            return x; // makes solaris happy
        }

        template <int RTYPE>
        SEXP basic_cast( SEXP x) {				// #nocov start
            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:
                const char* fmt = "Not compatible with requested type: "
                                  "[type=%s; target=%s].";
#ifndef NDEBUG
                REprintf(fmt,
                         Rf_type2char(TYPEOF(x)),
                         Rf_type2char(RTYPE));
                abort();
#else
                throw ::Rcpp::not_compatible(fmt,
                                             Rf_type2char(TYPEOF(x)),
                                             Rf_type2char(RTYPE));
#endif
            }							// #nocov end
            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){		// #nocov
            return basic_cast<REALSXP>(x);			// #nocov
        }
        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){		// #nocov
            return basic_cast<LGLSXP>(x);			// #nocov
        }

        template <>
        inline SEXP r_true_cast<STRSXP>(SEXP x){		// #nocov start
            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( Rcpp_fast_eval( call, R_GlobalEnv ) );
                    return res;
                }
            case CHARSXP:
                return Rf_ScalarString( x );
            case SYMSXP:
                return Rf_ScalarString( PRINTNAME( x ) );
            default:
                const char* fmt = "Not compatible with STRSXP: [type=%s].";
#ifndef NDEBUG
                REprintf(fmt, Rf_type2char(TYPEOF(x)));
                abort();
#else
                throw ::Rcpp::not_compatible(fmt, Rf_type2char(TYPEOF(x)));
#endif
            }
            return R_NilValue; /* -Wall */
        }
        template<>
        inline SEXP r_true_cast<VECSXP>(SEXP x) {
            return convert_using_rfunction(x, "as.list");	// #nocov end
        }
        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) {
            if (TYPEOF(x) == LANGSXP) {
                return Rf_cons(CAR(x), CDR(x));
            } else {
                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) );
            ::Rcpp::warning("Coerced object from '%s' to '%s'.",
                            Rf_type2char(TYPEOF(x)),
                            Rf_type2char(TARGET)
            );
            return result;
            #else
            return internal::r_true_cast<TARGET>(x);		// #nocov
            #endif
        }
    }

} // namespace Rcpp

#endif