File: as.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 (168 lines) | stat: -rw-r--r-- 6,576 bytes parent folder | download | duplicates (5)
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
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// as.h: Rcpp R/C++ interface class library -- convert SEXP to C++ objects
//
// Copyright (C) 2009 - 2015  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__as__h
#define Rcpp__as__h

#include <Rcpp/internal/Exporter.h>

namespace Rcpp {

    namespace internal {

        template <typename T> T primitive_as(SEXP x) {
            if (::Rf_length(x) != 1) {
                const char* fmt = "Expecting a single value: [extent=%i].";
                throw ::Rcpp::not_compatible(fmt, ::Rf_length(x));
            }
            const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
            Shield<SEXP> y(r_cast<RTYPE>(x));
            typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
            T res = caster<STORAGE,T>(*r_vector_start<RTYPE>(y));
            return res;
        }

        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_primitive_tag) {
            return primitive_as<T>(x);
        }

        inline const char* check_single_string(SEXP x) {
            if (TYPEOF(x) == CHARSXP) return CHAR(x);
            if (! ::Rf_isString(x) || Rf_length(x) != 1) {
                const char* fmt = "Expecting a single string value: "
                                  "[type=%s; extent=%i].";
                throw ::Rcpp::not_compatible(fmt,
                                             Rf_type2char(TYPEOF(x)),
                                             Rf_length(x));
            }

            return CHAR(STRING_ELT(::Rcpp::r_cast<STRSXP>(x), 0));
        }


        template <typename T> T as_string(SEXP x, Rcpp::traits::true_type) {
            const char* y = check_single_string(x);
            return std::wstring(y, y+strlen(y));
        }

        template <typename T> T as_string(SEXP x, Rcpp::traits::false_type) {
            return check_single_string(x);
        }

        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_string_tag) {
            return as_string<T>(x, typename Rcpp::traits::is_wide_string<T>::type());
        }

        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_RcppString_tag) {
            if (! ::Rf_isString(x)) {
                const char* fmt = "Expecting a single string value: "
                                  "[type=%s; extent=%i].";
                throw ::Rcpp::not_compatible(fmt,
                                             Rf_type2char(TYPEOF(x)),
                                             Rf_length(x));
            }
            return STRING_ELT(::Rcpp::r_cast<STRSXP>(x), 0);
        }

        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_generic_tag) {
            RCPP_DEBUG_1("as(SEXP = <%p>, r_type_generic_tag )", x);
            ::Rcpp::traits::Exporter<T> exporter(x);
            RCPP_DEBUG_1("exporter type = %s", DEMANGLE(exporter));
            return exporter.get();
        }

        void* as_module_object_internal(SEXP obj);

        template <typename T> object<T> as_module_object(SEXP x) {
            return (T*) as_module_object_internal(x);
        }

        /** handling object<T> */
        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_module_object_const_pointer_tag) {
            typedef typename Rcpp::traits::remove_const<T>::type T_NON_CONST;
            return const_cast<T>((T_NON_CONST)as_module_object_internal(x));
        }

        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_module_object_pointer_tag) {
            return as_module_object<typename traits::un_pointer<T>::type>(x);
        }

        /** handling T such that T is exposed by a module */
        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_module_object_tag) {
            T* obj = as_module_object<T>(x);
            return *obj;
        }

        /** handling T such that T is a reference of a class handled by a module */
        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_module_object_reference_tag) {
            typedef typename traits::remove_reference<T>::type KLASS;
            KLASS* obj = as_module_object<KLASS>(x);
            return *obj;
        }

        /** handling T such that T is a reference of a class handled by a module */
        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_module_object_const_reference_tag) {
            typedef typename traits::remove_const_and_reference<T>::type KLASS;
            KLASS* obj = as_module_object<KLASS>(x);
            return const_cast<T>(*obj);
        }

        /** handling enums by converting to int first */
        template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_enum_tag) {
            return T(primitive_as<int>(x));
        }

    }


    /**
     * Generic converted from SEXP to the typename. T can be any type that
     * has a constructor taking a SEXP, which is the case for all our
     * RObject and derived classes.
     *
     * If it is not possible to add the SEXP constructor, e.g you don't control
     * the type, you can specialize the as template to perform the
     * requested conversion
     *
     * This is used for example in Environment, so that for example the code
     * below will work as long as there is a way to as<> the Foo type
     *
     * Environment x = ... ; // some environment
     * Foo y = x["bla"] ;    // if as<Foo> makes sense then this works !!
     */
    template <typename T> T as(SEXP x) {
        return internal::as<T>(x, typename traits::r_type_traits<T>::r_category());
    }

    template <> inline char as<char>(SEXP x) {
        return internal::check_single_string(x)[0];
    }

    template <typename T>
    inline typename traits::remove_const_and_reference<T>::type bare_as(SEXP x) {
        return as< typename traits::remove_const_and_reference<T>::type >(x);
    }

    template<> inline SEXP as(SEXP x) { return x; }

} // Rcpp

#endif