Rcpp Version 1.0.9
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 
24 namespace Rcpp{
25 
34  {
35  public:
36 
39 
41 
43 
50  Language_Impl(SEXP x){
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  #include <Rcpp/generated/Language__ctors.h>
106 
110  void setSymbol( const std::string& symbol){
111  setSymbol( Symbol( symbol ) );
112  }
113 
117  void setSymbol( const Symbol& symbol ){
118  SEXP x = Storage::get__();
119  SETCAR( x, symbol );
120  SET_TAG(x, R_NilValue);
121  }
122 
126  void setFunction( const Function& function){
127  SEXP x = Storage::get__();
128  SETCAR( x, function );
129  SET_TAG(x, R_NilValue); /* probably not necessary */
130  }
131 
135  SEXP eval() const {
136  return Rcpp_fast_eval( Storage::get__(), R_GlobalEnv );
137  }
138 
142  SEXP eval(SEXP env) const {
143  return Rcpp_fast_eval( Storage::get__(), env );
144  }
145 
146  SEXP fast_eval() const {
147  return internal::Rcpp_eval_impl( Storage::get__(), R_GlobalEnv);
148  }
149  SEXP fast_eval(SEXP env ) const {
150  return internal::Rcpp_eval_impl( Storage::get__(), env);
151  }
152 
153  void update( SEXP x){
154  SET_TYPEOF( x, LANGSXP );
155  SET_TAG( x, R_NilValue );
156  }
157 
158  };
159 
160  typedef Language_Impl<PreserveStorage> Language;
161 
162  template <typename RESULT_TYPE=SEXP>
163  class fixed_call {
164  public:
165  typedef RESULT_TYPE result_type;
166 
167  fixed_call( Language call_ ) : call(call_){}
168  fixed_call( Function fun ) : call(fun){}
169 
170  RESULT_TYPE operator()(){
171  return as<RESULT_TYPE>( call.eval() );
172  }
173 
174  private:
176  };
177 
178  template <typename T, typename RESULT_TYPE = SEXP>
179 #if __cplusplus < 201103L
180  class unary_call : public std::unary_function<T,RESULT_TYPE> {
181 #else
182  class unary_call : public std::function<RESULT_TYPE(T)> {
183 #endif
184  public:
185  unary_call( Language call_ ) : call(call_), proxy(call_,1) {}
186  unary_call( Language call_, R_xlen_t index ) : call(call_), proxy(call_,index){}
187  unary_call( Function fun ) : call( fun, R_NilValue), proxy(call,1) {}
188 
189  RESULT_TYPE operator()( const T& object ){
190  proxy = object;
191  return as<RESULT_TYPE>( call.eval() );
192  }
193 
194  private:
196  Language::Proxy proxy;
197  };
198 
199  template <typename T1, typename T2, typename RESULT_TYPE = SEXP>
200 #if __cplusplus < 201103L
201  class binary_call : public std::binary_function<T1,T2,RESULT_TYPE> {
202 #else
203  class binary_call : public std::function<RESULT_TYPE(T1,T2)> {
204 #endif
205  public:
206  binary_call( Language call_ ) : call(call_), proxy1(call_,1), proxy2(call_,2) {}
207  binary_call( Language call_, R_xlen_t index1, R_xlen_t index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){}
208  binary_call( Function fun) : call(fun, R_NilValue, R_NilValue), proxy1(call,1), proxy2(call,2){}
209 
210  RESULT_TYPE operator()( const T1& o1, const T2& o2 ){
211  proxy1 = o1;
212  proxy2 = o2;
213  return as<RESULT_TYPE>( call.eval() );
214  }
215 
216  private:
218  Language::Proxy proxy1;
219  Language::Proxy proxy2;
220  };
221 
222 } // namespace Rcpp
223 
224 #endif
Language::Proxy proxy1
Definition: Language.h:218
binary_call(Function fun)
Definition: Language.h:208
binary_call(Language call_)
Definition: Language.h:206
Language::Proxy proxy2
Definition: Language.h:219
binary_call(Language call_, R_xlen_t index1, R_xlen_t index2)
Definition: Language.h:207
Language call
Definition: Language.h:217
RESULT_TYPE operator()(const T1 &o1, const T2 &o2)
Definition: Language.h:210
RESULT_TYPE operator()()
Definition: Language.h:170
fixed_call(Language call_)
Definition: Language.h:167
RESULT_TYPE result_type
Definition: Language.h:165
Language call
Definition: Language.h:175
fixed_call(Function fun)
Definition: Language.h:168
RESULT_TYPE operator()(const T &object)
Definition: Language.h:189
Language call
Definition: Language.h:195
Language::Proxy proxy
Definition: Language.h:196
unary_call(Language call_, R_xlen_t index)
Definition: Language.h:186
unary_call(Function fun)
Definition: Language.h:187
unary_call(Language call_)
Definition: Language.h:185
#define RCPP_GENERATE_CTOR_ASSIGN(__CLASS__)
Definition: interface.h:21
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:122
SEXP fast_eval() const
Definition: Language.h:146
Language_Impl()
Definition: Language.h:42
void update(SEXP)
Definition: Environment.h:400
RCPP_API_CLASS(DottedPair_Impl)
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:126
SEXP eval() const
Definition: Language.h:135
void setSymbol(const std::string &symbol)
Definition: Language.h:110
Language_Impl< PreserveStorage > Language
Definition: Language.h:158
Symbol_Impl< NoProtectStorage > Symbol
Definition: Symbol.h:82