Rcpp Version 1.0.9
module.cpp
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // Module.cpp: Rcpp R/C++ interface class library -- Rcpp modules
4 //
5 // Copyright (C) 2010 - 2017 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 #define COMPILING_RCPP
23 
24 #include <Rcpp.h>
25 #include "internal.h"
26 
30 
31 RCPP_FUN_1(bool, Class__has_default_constructor, XP_Class cl) {
32  return cl->has_default_constructor();
33 }
34 RCPP_FUN_2(SEXP, Module__get_function, XP_Module module, std::string fun) {
35  return module->get_function(fun);
36 }
37 RCPP_FUN_2(bool, Class__has_method, XP_Class cl, std::string m) { // #nocov start
38  return cl->has_method(m);
39 }
40 RCPP_FUN_2(bool, Class__has_property, XP_Class cl, std::string m) {
41  return cl->has_property(m);
42 }
43 RCPP_FUN_1(std::string, Class__name, XP_Class cl) {
44  return cl->name;
45 }
46 RCPP_FUN_2(bool, Module__has_function, XP_Module module, std::string met) {
47  return module->has_function(met);
48 }
49 RCPP_FUN_2(bool, Module__has_class, XP_Module module, std::string cl) {
50  return module->has_class(cl);
51 } // #nocov end
52 RCPP_FUN_2(Rcpp::CppClass, Module__get_class, XP_Module module, std::string cl) {
53  return module->get_class(cl);
54 }
55 RCPP_FUN_1(bool, CppObject__needs_init, SEXP xp) { // #nocov start
56  return R_ExternalPtrAddr(xp) == 0;
57 }
58 RCPP_FUN_1(Rcpp::CharacterVector, CppClass__methods, XP_Class cl) {
59  return cl->method_names();
60 }
61 RCPP_FUN_1(Rcpp::CharacterVector, CppClass__properties, XP_Class cl) {
62  return cl->property_names();
63 }
64 RCPP_FUN_1(Rcpp::List, CppClass__property_classes, XP_Class cl) {
65  return cl->property_classes();
66 }
67 
68 RCPP_FUN_1(Rcpp::IntegerVector, CppClass__methods_arity, XP_Class cl) {
69  return cl->methods_arity();
70 }
71 RCPP_FUN_1(Rcpp::LogicalVector, CppClass__methods_voidness, XP_Class cl) {
72  return cl->methods_voidness();
73 }
74 
75 
76 RCPP_FUN_2(bool, CppClass__property_is_readonly, XP_Class cl, std::string p) {
77  return cl->property_is_readonly(p);
78 }
79 RCPP_FUN_2(std::string, CppClass__property_class, XP_Class cl, std::string p) {
80  return cl->property_class(p);
81 }
82 
83 RCPP_FUN_1(Rcpp::IntegerVector, Module__functions_arity, XP_Module module) {
84  return module-> functions_arity();
85 } // #nocov end
86 RCPP_FUN_1(Rcpp::CharacterVector, Module__functions_names, XP_Module module) {
87  return module-> functions_names();
88 }
89 RCPP_FUN_1(std::string, Module__name, XP_Module module) { // #nocov start
90  return module->name;
91 } // #nocov end
92 RCPP_FUN_1(Rcpp::List, Module__classes_info, XP_Module module) {
93  return module->classes_info();
94 }
95 RCPP_FUN_1(Rcpp::CharacterVector, Module__complete, XP_Module module) { // #nocov start
96  return module->complete();
97 }
98 RCPP_FUN_1(Rcpp::CharacterVector, CppClass__complete, XP_Class cl) {
99  return cl->complete();
100 }
101 
102 // these operate directly on the external pointers, rather than
103 // looking up the property in the map // #nocov end
104 RCPP_FUN_3(SEXP, CppField__get, XP_Class cl, SEXP field_xp, SEXP obj) {
105  return cl->getProperty(field_xp, obj);
106 }
107 RCPP_FUN_4(SEXP, CppField__set, XP_Class cl, SEXP field_xp, SEXP obj, SEXP value) {
108  cl->setProperty(field_xp, obj, value);
109  return R_NilValue;
110 }
111 RCPP_FUN_2(SEXP, CppObject__finalize, XP_Class cl, SEXP obj) {
112  cl->run_finalizer(obj);
113  return R_NilValue;
114 }
115 
116 // .External functions
117 SEXP InternalFunction_invoke(SEXP args) {
119  SEXP p = CDR(args);
120  XP_Function fun(CAR(p)); p = CDR(p);
121  UNPACK_EXTERNAL_ARGS(cargs,p)
122  return fun->operator()(cargs);
123 END_RCPP
124 }
125 
126 SEXP Module__invoke(SEXP args) { // #nocov start
128  SEXP p = CDR(args);
129  XP_Module module(CAR(p)); p = CDR(p);
130  std::string fun = Rcpp::as<std::string>(CAR(p)); p = CDR(p);
131 
132  UNPACK_EXTERNAL_ARGS(cargs,p)
133  return module->invoke(fun, cargs, nargs);
134 END_RCPP
135 } // #nocov end
136 
137 SEXP class__newInstance(SEXP args) {
138  SEXP p = CDR(args);
139 
140  XP_Module module(CAR(p)); p = CDR(p);
141  XP_Class clazz(CAR(p)); p = CDR(p);
142  UNPACK_EXTERNAL_ARGS(cargs,p)
143  return clazz->newInstance(cargs, nargs);
144 }
145 
146 // relies on being set in .onLoad()
147 SEXP rcpp_dummy_pointer = R_NilValue;
148 
149 #define CHECK_DUMMY_OBJ(p) if (p == rcpp_dummy_pointer) throw Rcpp::not_initialized()
150 
151 
152 SEXP class__dummyInstance(SEXP args) {
153  SEXP p;
154 
155  if (args == R_NilValue) {
156  return rcpp_dummy_pointer; // #nocov
157  }
158  p = CDR(args);
159 
160  if (p != R_NilValue) {
161  rcpp_dummy_pointer = CAR(p);
162  }
163  return rcpp_dummy_pointer;
164 }
165 
166 SEXP CppMethod__invoke(SEXP args) { // #nocov start
167  SEXP p = CDR(args);
168 
169  // the external pointer to the class
170  XP_Class clazz(CAR(p)); p = CDR(p);
171 
172  // the external pointer to the method
173  SEXP met = CAR(p); p = CDR(p);
174 
175  // the external pointer to the object
176  SEXP obj = CAR(p); p = CDR(p);
177  CHECK_DUMMY_OBJ(obj);
178 
179  // additional arguments, processed the same way as .Call does
180  UNPACK_EXTERNAL_ARGS(cargs,p)
181 
182  return clazz->invoke(met, obj, cargs, nargs);
183 } // #nocov end
184 
185 SEXP CppMethod__invoke_void(SEXP args) {
186  SEXP p = CDR(args);
187 
188  // the external pointer to the class
189  XP_Class clazz(CAR(p)); p = CDR(p);
190 
191  // the external pointer to the method
192  SEXP met = CAR(p); p = CDR(p);
193 
194  // the external pointer to the object
195  SEXP obj = CAR(p); p = CDR(p);
196  CHECK_DUMMY_OBJ(obj);
197 
198  // additional arguments, processed the same way as .Call does
199  UNPACK_EXTERNAL_ARGS(cargs,p)
200  clazz->invoke_void(met, obj, cargs, nargs);
201  return R_NilValue;
202 }
203 
204 SEXP CppMethod__invoke_notvoid(SEXP args) {
205  SEXP p = CDR(args);
206 
207  // the external pointer to the class
208  XP_Class clazz(CAR(p)); p = CDR(p);
209 
210  // the external pointer to the method
211  SEXP met = CAR(p); p = CDR(p);
212 
213  // the external pointer to the object
214  SEXP obj = CAR(p); p = CDR(p);
215  CHECK_DUMMY_OBJ(obj);
216 
217  // additional arguments, processed the same way as .Call does
218  UNPACK_EXTERNAL_ARGS(cargs,p)
219 
220  return clazz->invoke_notvoid(met, obj, cargs, nargs);
221 }
222 
223 namespace Rcpp{
225 }
226 
228  return Rcpp::current_scope;
229 }
231  Rcpp::current_scope = scope;
232 }
233 
#define UNPACK_EXTERNAL_ARGS(__CARGS__, __P__)
Definition: internal.h:39
#define END_RCPP
Definition: macros.h:99
#define BEGIN_RCPP
Definition: macros.h:49
SEXP rcpp_dummy_pointer
Definition: module.cpp:147
SEXP CppMethod__invoke_void(SEXP args)
Definition: module.cpp:185
SEXP CppMethod__invoke(SEXP args)
Definition: module.cpp:166
RCPP_FUN_3(SEXP, CppField__get, XP_Class cl, SEXP field_xp, SEXP obj)
Definition: module.cpp:104
SEXP InternalFunction_invoke(SEXP args)
Definition: module.cpp:117
Rcpp::XPtr< Rcpp::CppFunctionBase > XP_Function
Definition: module.cpp:29
Rcpp::XPtr< Rcpp::Module > XP_Module
Definition: module.cpp:27
RCPP_FUN_2(SEXP, Module__get_function, XP_Module module, std::string fun)
Definition: module.cpp:34
SEXP CppMethod__invoke_notvoid(SEXP args)
Definition: module.cpp:204
Rcpp::Module * getCurrentScope()
Definition: module.cpp:227
#define CHECK_DUMMY_OBJ(p)
Definition: module.cpp:149
SEXP class__dummyInstance(SEXP args)
Definition: module.cpp:152
RCPP_FUN_4(SEXP, CppField__set, XP_Class cl, SEXP field_xp, SEXP obj, SEXP value)
Definition: module.cpp:107
RCPP_FUN_1(bool, Class__has_default_constructor, XP_Class cl)
Definition: module.cpp:31
SEXP Module__invoke(SEXP args)
Definition: module.cpp:126
void setCurrentScope(Rcpp::Module *scope)
Definition: module.cpp:230
SEXP class__newInstance(SEXP args)
Definition: module.cpp:137
Rcpp::XPtr< Rcpp::class_Base > XP_Class
Definition: module.cpp:28
Rcpp API.
Definition: algo.h:28
static Module * current_scope
Definition: module.cpp:224