Rcpp Version 1.1.2
Loading...
Searching...
No Matches
Function.h
Go to the documentation of this file.
1
2// Function.h: Rcpp R/C++ interface class library -- functions (also primitives and builtins)
3//
4// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
5// Copyright (C) 2026 Dirk Eddelbuettel, Romain François and Iñaki Ucar
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_Function_h
23#define Rcpp_Function_h
24
25#include <RcppCommon.h>
26
27#include <Rcpp/grow.h>
28
29namespace Rcpp{
30
34 RCPP_API_CLASS(Function_Impl) {
35 public:
36
37 RCPP_GENERATE_CTOR_ASSIGN(Function_Impl)
38
39 Function_Impl(SEXP x){
40 switch( TYPEOF(x) ){
41 case CLOSXP:
42 case SPECIALSXP:
43 case BUILTINSXP:
44 Storage::set__(x);
45 break;
46 default: // #nocov start
47 const char* fmt = "Cannot convert object to a function: "
48 "[type=%s; target=CLOSXP, SPECIALSXP, or "
49 "BUILTINSXP].";
50 throw not_compatible(fmt, Rf_type2char(TYPEOF(x)));
51 } // #nocov end
52 }
53
61 Function_Impl(const std::string& name) {
62 get_function(name, R_GlobalEnv);
63 }
64
65 Function_Impl(const std::string& name, const SEXP env) {
66 if (!Rf_isEnvironment(env)) {
67 stop("env is not an environment");
68 }
69 get_function(name, env);
70 }
71
72 Function_Impl(const std::string& name, const std::string& ns) {
73#if R_VERSION < R_Version(4,5,0)
74 // before R 4.5.0 we would use Rf_findVarInFrame
75 Shield<SEXP> env(Rf_findVarInFrame(R_NamespaceRegistry, Rf_install(ns.c_str())));
76 if (env == R_UnboundValue)
77 stop("there is no namespace called \"%s\"", ns);
78#elif R_VERSION < R_Version(4,6,0) || R_SVN_REVISION < 89746
79 // during R 4.5.* and before final R 4.6.0 we could use R_getVarEx
80 // along with R_NamespaceRegistry but avoid R_UnboundValue
81 Shield<SEXP> env(R_getVarEx(Rf_install(ns.c_str()), R_NamespaceRegistry, FALSE, R_NilValue));
82 if (env == R_NilValue)
83 stop("there is no namespace called \"%s\"", ns);
84#else
85 // late R 4.6.0 development got us R_getRegisteredNamespace
86 Shield<SEXP> env(R_getRegisteredNamespace(ns.c_str()));
87 if (env == R_NilValue)
88 stop("there is no namespace called \"%s\"", ns);
89#endif
90 get_function(name, env);
91 }
92
93 SEXP operator()() const {
94 Shield<SEXP> call(Rf_lang1(Storage::get__()));
95 return Rcpp_fast_eval(call, R_GlobalEnv);
96 }
97
98 template <typename... T>
99 SEXP operator()(const T&... args) const {
100 return invoke(pairlist(args...), R_GlobalEnv);
101 }
102
106 SEXP environment() const {
107 SEXP fun = Storage::get__() ;
108 if( TYPEOF(fun) != CLOSXP ) {
109 throw not_a_closure(Rf_type2char(TYPEOF(fun)));
110 }
111 #if (defined(R_VERSION) && R_VERSION >= R_Version(4,5,0))
112 return R_ClosureEnv(fun);
113 #else
114 return CLOENV(fun);
115 #endif
116 }
117
121 SEXP body() const {
122 return BODY( Storage::get__() ) ;
123 }
124
125 void update(SEXP){}
126
127
128 private:
129 void get_function(const std::string& name, const SEXP env) {
130 SEXP nameSym = Rf_install( name.c_str() ); // cannot be gc()'ed once in symbol table
131 Shield<SEXP> x( Rf_findFun( nameSym, env ) ) ;
132 Storage::set__(x) ;
133 }
134
135 SEXP invoke(SEXP args_, SEXP env) const {
136 Shield<SEXP> args(args_);
137 Shield<SEXP> call(Rcpp_lcons(Storage::get__(), args));
138 SEXP out = Rcpp_fast_eval(call, env);
139 return out;
140 }
141
142 };
143
144 typedef Function_Impl<PreserveStorage> Function ;
145
146} // namespace Rcpp
147
148#endif
#define RCPP_GENERATE_CTOR_ASSIGN(__CLASS__)
Definition interface.h:21
#define RCPP_API_CLASS(__CLASS__)
Definition interface.h:49
#define Rcpp_lcons
Definition lang.h:32
Rcpp API.
Definition algo.h:28
Function_Impl< PreserveStorage > Function
Definition Function.h:144
SEXP pairlist()
Definition grow.h:30
void NORET stop(const std::string &message)
Definition exceptions.h:119
void update(SEXP)
SEXP Rcpp_fast_eval(SEXP expr, SEXP env)
Definition Rcpp_eval.h:49