Skip to content

Commit 8206706

Browse files
committed
register.md
1 parent 2e8d519 commit 8206706

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

tests/testthat/_snaps/register.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# get_call_entries / returns an empty string for packages with .Call entries and NAMESPACE files
2+
3+
Code
4+
call_entries
5+
Output
6+
[1] "/* .Call calls */"
7+
[2] "extern SEXP bar(void);"
8+
[3] ""
9+
[4] "static const R_CallMethodDef CallEntries[] = {"
10+
[5] " {\"bar\", (DL_FUNC) &bar, 0},"
11+
[6] " {NULL, NULL, 0}"
12+
[7] "};"
13+
14+
# get_call_entries / works with multiple register functions.
15+
16+
Code
17+
cat(read_file(cpp_bindings))
18+
Output
19+
// Generated by cpp4r: do not edit by hand
20+
// clang-format off
21+
22+
23+
#include "cpp4r/declarations.hpp"
24+
#include <R_ext/Visibility.h>
25+
26+
// multiple.cpp
27+
int foo();
28+
extern "C" SEXP _testPkg_foo() {
29+
BEGIN_CPP4R
30+
return cpp4r::as_sexp(foo());
31+
END_CPP4R
32+
}
33+
// multiple.cpp
34+
double bar(bool run);
35+
extern "C" SEXP _testPkg_bar(SEXP run) {
36+
BEGIN_CPP4R
37+
return cpp4r::as_sexp(bar(cpp4r::as_cpp<cpp4r::decay_t<bool>>(run)));
38+
END_CPP4R
39+
}
40+
// multiple.cpp
41+
bool baz(bool run, int value);
42+
extern "C" SEXP _testPkg_baz(SEXP run, SEXP value) {
43+
BEGIN_CPP4R
44+
return cpp4r::as_sexp(baz(cpp4r::as_cpp<cpp4r::decay_t<bool>>(run), cpp4r::as_cpp<cpp4r::decay_t<int>>(value)));
45+
END_CPP4R
46+
}
47+
48+
extern "C" {
49+
static const R_CallMethodDef CallEntries[] = {
50+
{"_testPkg_bar", (DL_FUNC) &_testPkg_bar, 1},
51+
{"_testPkg_baz", (DL_FUNC) &_testPkg_baz, 2},
52+
{"_testPkg_foo", (DL_FUNC) &_testPkg_foo, 0},
53+
{NULL, NULL, 0}
54+
};
55+
}
56+
57+
extern "C" attribute_visible void R_init_testPkg(DllInfo* dll){
58+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
59+
R_useDynamicSymbols(dll, FALSE);
60+
R_forceSymbols(dll, TRUE);
61+
}
62+
63+
# register / works with a package that registers a single c++ function
64+
65+
Code
66+
cat(read_file(r_bindings))
67+
Output
68+
# Generated by cpp4r: do not edit by hand
69+
70+
foo <- function() {
71+
.Call(`_testPkg_foo`)
72+
}
73+
74+
---
75+
76+
Code
77+
cat(read_file(cpp_bindings))
78+
Output
79+
// Generated by cpp4r: do not edit by hand
80+
// clang-format off
81+
82+
83+
#include "cpp4r/declarations.hpp"
84+
#include <R_ext/Visibility.h>
85+
86+
// single.cpp
87+
int foo();
88+
extern "C" SEXP _testPkg_foo() {
89+
BEGIN_CPP4R
90+
return cpp4r::as_sexp(foo());
91+
END_CPP4R
92+
}
93+
94+
extern "C" {
95+
static const R_CallMethodDef CallEntries[] = {
96+
{"_testPkg_foo", (DL_FUNC) &_testPkg_foo, 0},
97+
{NULL, NULL, 0}
98+
};
99+
}
100+
101+
extern "C" attribute_visible void R_init_testPkg(DllInfo* dll){
102+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
103+
R_useDynamicSymbols(dll, FALSE);
104+
R_forceSymbols(dll, TRUE);
105+
}
106+
107+
# register / can be run with messages
108+
109+
Code
110+
register(p, quiet = FALSE)
111+
Message
112+
i 1 functions decorated with [[cpp4r::register]]
113+
Generated file 'cpp4r.R'
114+
Generated file 'cpp4r.cpp'
115+

0 commit comments

Comments
 (0)