-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathLanguage.h
More file actions
147 lines (121 loc) · 4.26 KB
/
Language.h
File metadata and controls
147 lines (121 loc) · 4.26 KB
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
#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:
using Proxy = typename DottedPairProxyPolicy<Language_Impl<StoragePolicy>>::DottedPairProxy ;
using const_Proxy = typename DottedPairProxyPolicy<Language_Impl<StoragePolicy>>::const_DottedPairProxy ;
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 lang){
Storage::set__( r_cast<LANGSXP>(lang) ) ;
}
/**
* 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 ) ) ;
}
template<typename... Args>
Language_Impl( const std::string& symbol, const Args&... args) {
Storage::set__( language( Rf_install( symbol.c_str() ), args...) ) ;
}
template<typename... Args>
Language_Impl( const Function& function, const Args&... args) {
Storage::set__( language( function, args...) ) ;
}
/**
* 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 data = Storage::get__() ;
SETCAR( data, symbol ) ;
SET_TAG(data, R_NilValue);
}
/**
* eval this call in the global environment
*/
SEXP eval() const {
return eval( R_GlobalEnv ) ;
}
/**
* eval this call in the requested environment
*/
SEXP eval(SEXP env) const {
return Rcpp_eval( Storage::get__(), env ) ;
}
};
template <typename OUT = SEXP, typename... Args>
class typed_call {
public:
using Proxy = Language::Proxy ;
typed_call( Language call_) : call(call_){
for( int i=0; i<n; i++){
proxies.emplace_back( call, i+1 ) ;
}
}
inline OUT operator()( const Args&... args ){
set__impl( traits::number_to_type<n>(), args... ) ;
return as<OUT>( call.eval() ) ;
}
private:
const static int n = sizeof...(Args) ;
Language call ;
std::vector<Proxy> proxies ;
template <int N, typename First, typename... Types>
inline void set__impl( traits::number_to_type<N>, const First& first, const Types&... args ){
proxies[n-N] = first ;
set_impl( traits::number_to_type<N-1>(), args...) ;
}
template <typename First>
inline void set_impl( traits::number_to_type<1>, const First& first ){
proxies[n-1] = first ;
}
} ;
} // namespace Rcpp
#endif