-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFunction.h
More file actions
75 lines (64 loc) · 2.1 KB
/
Function.h
File metadata and controls
75 lines (64 loc) · 2.1 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
#ifndef Rcpp_Function_h
#define Rcpp_Function_h
namespace Rcpp{
RCPP_API_CLASS(Function_Impl){
public:
RCPP_GENERATE_CTOR_ASSIGN(Function_Impl)
/**
* Attempts to convert the SEXP to a pair list
*
* @throw not_compatible if the SEXP could not be converted
* to a pair list using as.pairlist
*/
Function_Impl(SEXP x = R_NilValue){
RCPP_DEBUG( "Function::Function(SEXP = <%p>)", x)
switch( TYPEOF(x) ){
case CLOSXP:
case SPECIALSXP:
case BUILTINSXP:
Storage::set__(x) ;
break;
default:
throw not_compatible("cannot convert to function") ;
}
}
/**
* Finds a function, searching from the global environment
*
* @param name name of the function
*/
Function_Impl(const std::string& name){
SEXP nameSym = Rf_install( name.c_str() );
Storage::set__( Rf_findFun( nameSym, R_GlobalEnv ) ) ;
}
/**
* calls the function with the specified arguments
*
* @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
*
*/
template<typename... Args>
SEXP operator()( const Args&... args) const {
Shield<SEXP> call = language( Storage::get__() , args... ) ;
return Rcpp_eval( call ) ;
}
/**
* Returns the environment of this function
*/
SEXP environment() const {
if( TYPEOF(Storage::get__()) != CLOSXP ) {
throw not_a_closure() ;
}
return CLOENV(Storage::get__()) ;
}
/**
* Returns the body of the function
*/
SEXP body() const {
return BODY( Storage::get__() ) ;
}
};
} // namespace Rcpp
#endif