9 #include <RcppCommon.h> 11 #if !defined(RCPP_USING_CXX11) 12 int main(
int argc,
char *argv[]) {
13 std::cout <<
"This example requires a c++11 compatible compiler. Upgrade your compiler and/or add the -std=c++11 compiler option.\n";
16 #elif RCPP_VERSION < Rcpp_Version(0,11,3) 17 int main(
int argc,
char *argv[]) {
18 std::cout <<
"This example requires Rcpp 0.11.3 or later. Upgrade Rcpp and recompile this example.\n";
33 Foo(
int a,
int b) :
a(a),
b(b) {
40 throw "Cannot copy construct Foo";
43 Foo &operator=(
const Foo &f) {
44 throw "Cannot copy assign Foo";
64 template<> SEXP
wrap(
const Foo &f);
65 template<> SEXP
wrap(
const std::unique_ptr<Foo> &f);
66 template<> std::unique_ptr<Foo>
as(SEXP sexp);
78 template<> SEXP
Rcpp::wrap(
const std::unique_ptr<Foo> &f) {
93 template<> std::unique_ptr<Foo>
Rcpp::as(SEXP sexp) {
94 Rcpp::List list = Rcpp::as<Rcpp::List>(sexp);
99 return std::unique_ptr<Foo>(
new Foo(a, b));
104 std::unique_ptr<Foo>
swapFoo(std::unique_ptr<Foo> input) {
105 return std::unique_ptr<Foo>(
new Foo(input->b, input->a));
108 std::unique_ptr<Foo> addFoo(std::unique_ptr<Foo> foo1, std::unique_ptr<Foo> foo2) {
109 return std::unique_ptr<Foo>(
new Foo(foo1->a + foo2->a, foo1->b + foo2->b));
118 FooDatabase(
int database_id) : database_id(database_id) {
121 std::unique_ptr<Foo> queryFoo(
int id) {
122 return std::unique_ptr<Foo>(
new Foo(database_id,
id));
125 void destroyDatabase() {
133 int main(
int argc,
char *argv[]) {
138 R[
"swapFoo"] = Rcpp::InternalFunction( &
swapFoo );
139 R[
"addFoo"] = Rcpp::InternalFunction( &addFoo );
142 FooDatabase db1(1), db2(2), db3(3);
145 std::function< std::unique_ptr<Foo>(int) > queryDB1 = std::bind(&FooDatabase::queryFoo, std::ref(db1), std::placeholders::_1);
146 R[
"queryDB1"] = Rcpp::InternalFunction( queryDB1 );
149 std::function< std::unique_ptr<Foo>() > queryDB2 = std::bind(&FooDatabase::queryFoo, std::ref(db2), 42);
150 R[
"queryDB2"] = Rcpp::InternalFunction( queryDB2 );
153 std::function< std::unique_ptr<Foo>(int) > queryDB3 =
154 [&db3] (
int id) -> std::unique_ptr<Foo> {
155 if (id < 0 || id > 20)
156 throw "id out of allowed range";
157 return db3.queryFoo(
id);
159 R[
"queryDB3"] = Rcpp::InternalFunction( queryDB3 );
162 std::unique_ptr<Foo> result = R.parseEvalNT(
163 "foo1 = queryDB1(20);" 167 "foo3 = queryDB3(10);" 170 "foo1 = swapFoo(foo1);" 172 "foo = addFoo(foo1, addFoo(foo2, foo3));" 178 std::cout <<
" Got result a=" << result->a <<
", b=" << result->b << std::endl;
179 std::cout <<
" Expected a=25, b=53" << std::endl;
SEXP wrap(const Bar &bar)
Foo(const std::string &name, int32_t a, int32_t b)
int main(int argc, char *argv[])