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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
// Language.h: Rcpp R/C++ interface class library -- language objects (calls)
//
// Copyright (C) 2010 - 2025 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_Language_h
#define Rcpp_Language_h
namespace Rcpp{
/**
* C++ wrapper around calls (LANGSXP SEXP)
*
* This represents calls that can be evaluated
*/
RCPP_API_CLASS(Language_Impl),
public DottedPairProxyPolicy< Language_Impl<StoragePolicy> > ,
public DottedPairImpl< Language_Impl<StoragePolicy> >
{
public:
typedef typename DottedPairProxyPolicy<Language_Impl>::DottedPairProxy Proxy;
typedef typename DottedPairProxyPolicy<Language_Impl>::const_DottedPairProxy const_Proxy;
RCPP_GENERATE_CTOR_ASSIGN(Language_Impl)
Language_Impl(){}
/**
* Attempts to convert the SEXP to a call
*
* @throw not_compatible if the SEXP could not be converted
* to a call using as.call
*/
Language_Impl(SEXP x){
Storage::set__( r_cast<LANGSXP>(x) );
}
/**
* Creates a call using the given symbol as the function name
*
* @param symbol symbol name to call
*
* Language( "rnorm" ) makes a SEXP similar to this (expressed in R)
* > as.call( as.list( as.name( "rnorm") ) )
* > call( "rnorm" )
*/
explicit Language_Impl( const std::string& symbol ){
Storage::set__( Rf_lang1( Rf_install(symbol.c_str()) ) );
}
/**
* Creates a call using the given symbol as the function name
*
* @param symbol symbol name to call
*
* Language( Symbol("rnorm") ) makes a SEXP similar to this:
* > call( "rnorm" )
*/
explicit Language_Impl( const Symbol& symbol ){
Storage::set__( Rf_lang1( symbol ) );
}
/**
* Creates a call to the function
*
* @param function function to call
*/
explicit Language_Impl( const Function& function) {
Storage::set__( Rf_lang1( function ) );
}
/**
* Creates a call to the given symbol using variable number of
* arguments
*
* @param symbol symbol
* @param ...Args variable length argument list. The type of each
* argument must be wrappable, meaning there need to be
* a wrap function that takes this type as its parameter
*
* For example, Language( "rnorm", 10, 0.0 )
* will create the same call as
* > call( "rnorm", 10L, 0.0 )
*
* 10 is wrapped as an integer vector using wrap( const& int )
* 0.0 is wrapped as a numeric vector using wrap( const& double )
* ...
*/
template <typename... T>
Language_Impl(const std::string& symbol, const T&... t) {
Storage::set__(langlist(Rf_install(symbol.c_str()), t...) );
}
template <typename... T>
Language_Impl(const Function& function, const T&... t) {
Storage::set__(langlist(function, t...));
}
/**
* sets the symbol of the call
*/
void setSymbol( const std::string& symbol){
setSymbol( Symbol( symbol ) );
}
/**
* sets the symbol of the call
*/
void setSymbol( const Symbol& symbol ){
SEXP x = Storage::get__();
SETCAR( x, symbol );
SET_TAG(x, R_NilValue);
}
/**
* sets the function
*/
void setFunction( const Function& function){
SEXP x = Storage::get__();
SETCAR( x, function );
SET_TAG(x, R_NilValue); /* probably not necessary */
}
/**
* eval this call in the global environment
*/
SEXP eval() const {
return Rcpp_fast_eval( Storage::get__(), R_GlobalEnv );
}
/**
* eval this call in the requested environment
*/
SEXP eval(SEXP env) const {
return Rcpp_fast_eval( Storage::get__(), env );
}
SEXP fast_eval() const {
return internal::Rcpp_eval_impl( Storage::get__(), R_GlobalEnv);
}
SEXP fast_eval(SEXP env ) const {
return internal::Rcpp_eval_impl( Storage::get__(), env);
}
void update(SEXP x) {
if (TYPEOF(x) != LANGSXP) {
Storage::set__(r_cast<LANGSXP>(x));
}
SET_TAG( x, R_NilValue );
}
};
typedef Language_Impl<PreserveStorage> Language;
template <typename RESULT_TYPE=SEXP>
class fixed_call {
public:
typedef RESULT_TYPE result_type;
fixed_call( Language call_ ) : call(call_){}
fixed_call( Function fun ) : call(fun){}
RESULT_TYPE operator()(){
return as<RESULT_TYPE>( call.eval() );
}
private:
Language call;
};
template <typename T, typename RESULT_TYPE = SEXP>
class unary_call : public std::function<RESULT_TYPE(T)> {
public:
unary_call( Language call_ ) : call(call_), proxy(call_,1) {}
unary_call( Language call_, R_xlen_t index ) : call(call_), proxy(call_,index){}
unary_call( Function fun ) : call( fun, R_NilValue), proxy(call,1) {}
RESULT_TYPE operator()( const T& object ){
proxy = object;
return as<RESULT_TYPE>( call.eval() );
}
private:
Language call;
Language::Proxy proxy;
};
template <typename T1, typename T2, typename RESULT_TYPE = SEXP>
class binary_call : public std::function<RESULT_TYPE(T1,T2)> {
public:
binary_call( Language call_ ) : call(call_), proxy1(call_,1), proxy2(call_,2) {}
binary_call( Language call_, R_xlen_t index1, R_xlen_t index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){}
binary_call( Function fun) : call(fun, R_NilValue, R_NilValue), proxy1(call,1), proxy2(call,2){}
RESULT_TYPE operator()( const T1& o1, const T2& o2 ){
proxy1 = o1;
proxy2 = o2;
return as<RESULT_TYPE>( call.eval() );
}
private:
Language call;
Language::Proxy proxy1;
Language::Proxy proxy2;
};
} // namespace Rcpp
#endif
|