Rcpp Version 0.12.12
Language.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // Language.h: Rcpp R/C++ interface class library -- language objects (calls)
4 //
5 // Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp_Language_h
23 #define Rcpp_Language_h
24 
25 namespace Rcpp{
26 
35  {
36  public:
37 
40 
42 
44 
51  Language_Impl(SEXP x){
52  Storage::set__( r_cast<LANGSXP>(x) ) ;
53  }
54 
64  explicit Language_Impl( const std::string& symbol ){
65  Storage::set__( Rf_lang1( Rf_install(symbol.c_str()) ) ) ;
66  }
67 
76  explicit Language_Impl( const Symbol& symbol ){
77  Storage::set__( Rf_lang1( symbol ) ) ;
78  }
79 
85  explicit Language_Impl( const Function& function) {
86  Storage::set__( Rf_lang1( function ) ) ;
87  }
88 
106  #include <Rcpp/generated/Language__ctors.h>
107 
111  void setSymbol( const std::string& symbol){
112  setSymbol( Symbol( symbol ) ) ;
113  }
114 
118  void setSymbol( const Symbol& symbol ){
119  SEXP x = Storage::get__() ;
120  SETCAR( x, symbol ) ;
121  SET_TAG(x, R_NilValue);
122  }
123 
127  void setFunction( const Function& function){
128  SEXP x = Storage::get__() ;
129  SETCAR( x, function );
130  SET_TAG(x, R_NilValue); /* probably not necessary */
131  }
132 
136  SEXP eval() const {
137  return Rcpp_eval( Storage::get__(), R_GlobalEnv ) ;
138  }
139 
143  SEXP eval(SEXP env) const {
144  return Rcpp_eval( Storage::get__(), env ) ;
145  }
146 
147  SEXP fast_eval() const {
148  return Rf_eval( Storage::get__(), R_GlobalEnv) ;
149  }
150  SEXP fast_eval(SEXP env ) const {
151  return Rf_eval( Storage::get__(), env) ;
152  }
153 
154  void update( SEXP x){
155  SET_TYPEOF( x, LANGSXP ) ;
156  SET_TAG( x, R_NilValue ) ;
157  }
158 
159  };
160 
161  typedef Language_Impl<PreserveStorage> Language ;
162 
163  template <typename RESULT_TYPE=SEXP>
164  class fixed_call {
165  public:
166  typedef RESULT_TYPE result_type ;
167 
168  fixed_call( Language call_ ) : call(call_){}
169  fixed_call( Function fun ) : call(fun){}
170 
171  RESULT_TYPE operator()(){
172  return as<RESULT_TYPE>( call.eval() ) ;
173  }
174 
175  private:
176  Language call ;
177  } ;
178 
179  template <typename T, typename RESULT_TYPE = SEXP>
180  class unary_call : public std::unary_function<T,RESULT_TYPE> {
181  public:
182  unary_call( Language call_ ) : call(call_), proxy(call_,1) {}
183  unary_call( Language call_, R_xlen_t index ) : call(call_), proxy(call_,index){}
184  unary_call( Function fun ) : call( fun, R_NilValue), proxy(call,1) {}
185 
186  RESULT_TYPE operator()( const T& object ){
187  proxy = object ;
188  return as<RESULT_TYPE>( call.eval() ) ;
189  }
190 
191  private:
192  Language call ;
193  Language::Proxy proxy ;
194  } ;
195 
196  template <typename T1, typename T2, typename RESULT_TYPE = SEXP>
197  class binary_call : public std::binary_function<T1,T2,RESULT_TYPE> {
198  public:
199  binary_call( Language call_ ) : call(call_), proxy1(call_,1), proxy2(call_,2) {}
200  binary_call( Language call_, R_xlen_t index1, R_xlen_t index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){}
201  binary_call( Function fun) : call(fun, R_NilValue, R_NilValue), proxy1(call,1), proxy2(call,2){}
202 
203  RESULT_TYPE operator()( const T1& o1, const T2& o2 ){
204  proxy1 = o1 ;
205  proxy2 = o2 ;
206  return as<RESULT_TYPE>( call.eval() ) ;
207  }
208 
209  private:
210  Language call ;
211  Language::Proxy proxy1 ;
212  Language::Proxy proxy2 ;
213  } ;
214 
215 } // namespace Rcpp
216 
217 #endif
Language::Proxy proxy
Definition: Language.h:193
void setSymbol(const std::string &symbol)
Definition: Language.h:111
RESULT_TYPE result_type
Definition: Language.h:166
unary_call(Function fun)
Definition: Language.h:184
#define RCPP_GENERATE_CTOR_ASSIGN(__CLASS__)
Definition: interface.h:21
SEXP eval() const
Definition: Language.h:136
unary_call(Language call_, R_xlen_t index)
Definition: Language.h:183
Function_Impl< PreserveStorage > Function
Definition: Function.h:114
RESULT_TYPE operator()(const T1 &o1, const T2 &o2)
Definition: Language.h:203
SEXP Rcpp_eval(SEXP expr, SEXP env)
Definition: Rcpp_eval.h:25
RESULT_TYPE operator()(const T &object)
Definition: Language.h:186
unary_call(Language call_)
Definition: Language.h:182
fixed_call(Language call_)
Definition: Language.h:168
RCPP_API_CLASS(DottedPair_Impl)
Language::Proxy proxy2
Definition: Language.h:212
Symbol_Impl< NoProtectStorage > Symbol
Definition: Symbol.h:82
SEXP fast_eval() const
Definition: Language.h:147
fixed_call(Function fun)
Definition: Language.h:169
Language call
Definition: Language.h:192
Language call
Definition: Language.h:210
binary_call(Language call_)
Definition: Language.h:199
binary_call(Language call_, R_xlen_t index1, R_xlen_t index2)
Definition: Language.h:200
RESULT_TYPE operator()()
Definition: Language.h:171
Language_Impl()
Definition: Language.h:43
Language_Impl< PreserveStorage > Language
Definition: Language.h:159
binary_call(Function fun)
Definition: Language.h:201
Rcpp API.
Definition: algo.h:28
Language call
Definition: Language.h:176
DottedPairProxyPolicy< Language_Impl >::const_DottedPairProxy const_Proxy
Definition: Language.h:39
Language::Proxy proxy1
Definition: Language.h:211
void update(SEXP)
Definition: Environment.h:400
void setFunction(const Function &function)
Definition: Language.h:127