Rcpp Version 1.0.14
Loading...
Searching...
No Matches
Language.h
Go to the documentation of this file.
1
2// Language.h: Rcpp R/C++ interface class library -- language objects (calls)
3//
4// Copyright (C) 2010 - 2022 Dirk Eddelbuettel and Romain Francois
5//
6// This file is part of Rcpp.
7//
8// Rcpp is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rcpp is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef Rcpp_Language_h
22#define Rcpp_Language_h
23
24namespace Rcpp{
25
34 {
35 public:
36
39
41
43
51 Storage::set__( r_cast<LANGSXP>(x) );
52 }
53
63 explicit Language_Impl( const std::string& symbol ){
64 Storage::set__( Rf_lang1( Rf_install(symbol.c_str()) ) );
65 }
66
75 explicit Language_Impl( const Symbol& symbol ){
76 Storage::set__( Rf_lang1( symbol ) );
77 }
78
84 explicit Language_Impl( const Function& function) {
85 Storage::set__( Rf_lang1( function ) );
86 }
87
105 #if defined(HAS_VARIADIC_TEMPLATES)
106 template <typename... T>
107 Language_Impl(const std::string& symbol, const T&... t) {
108 Storage::set__(pairlist(Rf_install(symbol.c_str()), t...) );
109 }
110
111 template <typename... T>
112 Language_Impl(const Function& function, const T&... t) {
113 Storage::set__(pairlist(function, t...));
114 }
115 #else
116 #include <Rcpp/generated/Language__ctors.h>
117 #endif
118
122 void setSymbol( const std::string& symbol){
123 setSymbol( Symbol( symbol ) );
124 }
125
129 void setSymbol( const Symbol& symbol ){
130 SEXP x = Storage::get__();
131 SETCAR( x, symbol );
133 }
134
138 void setFunction( const Function& function){
139 SEXP x = Storage::get__();
140 SETCAR( x, function );
141 SET_TAG(x, R_NilValue); /* probably not necessary */
142 }
143
147 SEXP eval() const {
148 return Rcpp_fast_eval( Storage::get__(), R_GlobalEnv );
149 }
150
154 SEXP eval(SEXP env) const {
155 return Rcpp_fast_eval( Storage::get__(), env );
156 }
157
158 SEXP fast_eval() const {
159 return internal::Rcpp_eval_impl( Storage::get__(), R_GlobalEnv);
160 }
161 SEXP fast_eval(SEXP env ) const {
162 return internal::Rcpp_eval_impl( Storage::get__(), env);
163 }
164
165 void update(SEXP x) {
166 if (TYPEOF(x) != LANGSXP) {
167 Storage::set__(r_cast<LANGSXP>(x));
168 }
169 SET_TAG( x, R_NilValue );
170 }
171
172 };
173
175
176 template <typename RESULT_TYPE=SEXP>
178 public:
180
182 fixed_call( Function fun ) : call(fun){}
183
185 return as<RESULT_TYPE>( call.eval() );
186 }
187
188 private:
190 };
191
192 template <typename T, typename RESULT_TYPE = SEXP>
193#if __cplusplus < 201103L
194 class unary_call : public std::unary_function<T,RESULT_TYPE> {
195#else
196 class unary_call : public std::function<RESULT_TYPE(T)> {
197#endif
198 public:
202
203 RESULT_TYPE operator()( const T& object ){
204 proxy = object;
205 return as<RESULT_TYPE>( call.eval() );
206 }
207
208 private:
210 Language::Proxy proxy;
211 };
212
213 template <typename T1, typename T2, typename RESULT_TYPE = SEXP>
214#if __cplusplus < 201103L
215 class binary_call : public std::binary_function<T1,T2,RESULT_TYPE> {
216#else
217 class binary_call : public std::function<RESULT_TYPE(T1,T2)> {
218#endif
219 public:
220 binary_call( Language call_ ) : call(call_), proxy1(call_,1), proxy2(call_,2) {}
221 binary_call( Language call_, R_xlen_t index1, R_xlen_t index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){}
222 binary_call( Function fun) : call(fun, R_NilValue, R_NilValue), proxy1(call,1), proxy2(call,2){}
223
224 RESULT_TYPE operator()( const T1& o1, const T2& o2 ){
225 proxy1 = o1;
226 proxy2 = o2;
227 return as<RESULT_TYPE>( call.eval() );
228 }
229
230 private:
232 Language::Proxy proxy1;
233 Language::Proxy proxy2;
234 };
235
236} // namespace Rcpp
237
238#endif
Language::Proxy proxy1
Definition Language.h:232
binary_call(Function fun)
Definition Language.h:222
binary_call(Language call_)
Definition Language.h:220
Language::Proxy proxy2
Definition Language.h:233
binary_call(Language call_, R_xlen_t index1, R_xlen_t index2)
Definition Language.h:221
RESULT_TYPE operator()(const T1 &o1, const T2 &o2)
Definition Language.h:224
RESULT_TYPE operator()()
Definition Language.h:184
fixed_call(Language call_)
Definition Language.h:181
RESULT_TYPE result_type
Definition Language.h:179
Language call
Definition Language.h:189
fixed_call(Function fun)
Definition Language.h:182
RESULT_TYPE operator()(const T &object)
Definition Language.h:203
Language call
Definition Language.h:209
Language::Proxy proxy
Definition Language.h:210
unary_call(Language call_, R_xlen_t index)
Definition Language.h:200
unary_call(Function fun)
Definition Language.h:201
unary_call(Language call_)
Definition Language.h:199
#define RCPP_GENERATE_CTOR_ASSIGN(__CLASS__)
Definition interface.h:21
#define RCPP_API_CLASS(__CLASS__)
Definition interface.h:49
SEXP Rcpp_eval_impl(SEXP expr, SEXP env)
Definition Rcpp_eval.h:48
Rcpp API.
Definition algo.h:28
Function_Impl< PreserveStorage > Function
Definition Function.h:131
SEXP pairlist()
Definition grow.h:30
SEXP fast_eval() const
Definition Language.h:158
Language_Impl()
Definition Language.h:42
void update(SEXP)
DottedPairProxyPolicy< Language_Impl >::const_DottedPairProxy const_Proxy
Definition Language.h:38
SEXP Rcpp_fast_eval(SEXP expr, SEXP env)
Definition Rcpp_eval.h:68
void setFunction(const Function &function)
Definition Language.h:138
SEXP eval() const
Definition Language.h:147
void setSymbol(const std::string &symbol)
Definition Language.h:122
T as(SEXP x)
Definition as.h:151
Language_Impl< PreserveStorage > Language
Definition Language.h:174
Symbol_Impl< NoProtectStorage > Symbol
Definition Symbol.h:84