Rcpp Version 1.1.2
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 - 2025 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 template <typename... T>
106 Language_Impl(const std::string& symbol, const T&... t) {
107 Storage::set__(langlist(Rf_install(symbol.c_str()), t...) );
108 }
109
110 template <typename... T>
111 Language_Impl(const Function& function, const T&... t) {
112 Storage::set__(langlist(function, t...));
113 }
114
118 void setSymbol( const std::string& symbol){
119 setSymbol( Symbol( symbol ) );
120 }
121
125 void setSymbol( const Symbol& symbol ){
126 SEXP x = Storage::get__();
127 SETCAR( x, symbol );
128 SET_TAG(x, R_NilValue);
129 }
130
135 SEXP x = Storage::get__();
136 SETCAR( x, function );
137 SET_TAG(x, R_NilValue); /* probably not necessary */
138 }
139
143 SEXP eval() const {
144 return Rcpp_fast_eval( Storage::get__(), R_GlobalEnv );
145 }
146
150 SEXP eval(SEXP env) const {
151 return Rcpp_fast_eval( Storage::get__(), env );
152 }
153
154 SEXP fast_eval() const {
155 return internal::Rcpp_eval_impl( Storage::get__(), R_GlobalEnv);
156 }
157 SEXP fast_eval(SEXP env ) const {
158 return internal::Rcpp_eval_impl( Storage::get__(), env);
159 }
160
161 void update(SEXP x) {
162 if (TYPEOF(x) != LANGSXP) {
163 Storage::set__(r_cast<LANGSXP>(x));
164 }
165 SET_TAG( x, R_NilValue );
166 }
167
168 };
169
171
172 template <typename RESULT_TYPE=SEXP>
174 public:
175 typedef RESULT_TYPE result_type;
176
177 fixed_call( Language call_ ) : call(call_){}
178 fixed_call( Function fun ) : call(fun){}
179
180 RESULT_TYPE operator()(){
181 return as<RESULT_TYPE>( call.eval() );
182 }
183
184 private:
186 };
187
188 template <typename T, typename RESULT_TYPE = SEXP>
189 class unary_call : public std::function<RESULT_TYPE(T)> {
190 public:
191 unary_call( Language call_ ) : call(call_), proxy(call_,1) {}
192 unary_call( Language call_, R_xlen_t index ) : call(call_), proxy(call_,index){}
193 unary_call( Function fun ) : call( fun, R_NilValue), proxy(call,1) {}
194
195 RESULT_TYPE operator()( const T& object ){
196 proxy = object;
197 return as<RESULT_TYPE>( call.eval() );
198 }
199
200 private:
202 Language::Proxy proxy;
203 };
204
205 template <typename T1, typename T2, typename RESULT_TYPE = SEXP>
206 class binary_call : public std::function<RESULT_TYPE(T1,T2)> {
207 public:
208 binary_call( Language call_ ) : call(call_), proxy1(call_,1), proxy2(call_,2) {}
209 binary_call( Language call_, R_xlen_t index1, R_xlen_t index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){}
210 binary_call( Function fun) : call(fun, R_NilValue, R_NilValue), proxy1(call,1), proxy2(call,2){}
211
212 RESULT_TYPE operator()( const T1& o1, const T2& o2 ){
213 proxy1 = o1;
214 proxy2 = o2;
215 return as<RESULT_TYPE>( call.eval() );
216 }
217
218 private:
220 Language::Proxy proxy1;
221 Language::Proxy proxy2;
222 };
223
224} // namespace Rcpp
225
226#endif
Language::Proxy proxy1
Definition Language.h:220
binary_call(Function fun)
Definition Language.h:210
binary_call(Language call_)
Definition Language.h:208
Language::Proxy proxy2
Definition Language.h:221
binary_call(Language call_, R_xlen_t index1, R_xlen_t index2)
Definition Language.h:209
RESULT_TYPE operator()(const T1 &o1, const T2 &o2)
Definition Language.h:212
RESULT_TYPE operator()()
Definition Language.h:180
fixed_call(Language call_)
Definition Language.h:177
RESULT_TYPE result_type
Definition Language.h:175
Language call
Definition Language.h:185
fixed_call(Function fun)
Definition Language.h:178
RESULT_TYPE operator()(const T &object)
Definition Language.h:195
Language call
Definition Language.h:201
Language::Proxy proxy
Definition Language.h:202
unary_call(Language call_, R_xlen_t index)
Definition Language.h:192
unary_call(Function fun)
Definition Language.h:193
unary_call(Language call_)
Definition Language.h:191
#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:40
Rcpp API.
Definition algo.h:28
Function_Impl< PreserveStorage > Function
Definition Function.h:144
SEXP fast_eval() const
Definition Language.h:154
Language_Impl()
Definition Language.h:42
void update(SEXP)
SEXP r_cast(SEXP x)
Definition r_cast.h:158
DottedPairProxyPolicy< Language_Impl >::const_DottedPairProxy const_Proxy
Definition Language.h:38
SEXP Rcpp_fast_eval(SEXP expr, SEXP env)
Definition Rcpp_eval.h:49
SEXP langlist(const T1 &t1)
Definition lgrow.h:66
void setFunction(const Function &function)
Definition Language.h:134
SEXP eval() const
Definition Language.h:143
void setSymbol(const std::string &symbol)
Definition Language.h:118
T as(SEXP x)
Definition as.h:150
Language_Impl< PreserveStorage > Language
Definition Language.h:170
void function(const char *name_, RESULT_TYPE(*fun)(T... t), const char *docstring=0)
Definition Module.h:544
Symbol_Impl< NoProtectStorage > Symbol
Definition Symbol.h:76