I have the following problem with the Rcpp module: let's assume I've two classes in a Rcpp module
class A {
public:
int x;
};
class B
public:
A get_an_a(){
A an_a();
an_a.x=3;
return an_a;
}
};
RCPP_MODULE(mod){
using namespace Rcpp ;
class_<A>("A")
.constructor()
.property("x",&A::get_x)
;
class_<B>("B)
.constructor()
.method("get_an_A",&get_an_a)
;
}
.
Right now compilation fails as it does not know what to do with the return type of A.
I figured I could do something with Rcpp::Xptr, however, then I can't connect it to the S4 structure that Rcpp generated for the class A. I actually get an external pointer object from the method in R.
Is there any possiblity to get a correctly wrapped object back from a method of the second class?
Thanks, Thomas
[edit]
According to Dirk's answer I constructed a wrapper that can create the wrapped S4 object:
template <> SEXP wrap(const A &obj) { // insprired from "make_new_object" from Rcpp/Module.h
Rcpp::XPtr<A> xp( new A(obj), true ) ; // copy and mark as finalizable
Function maker=Environment::Rcpp_namespace()[ "cpp_object_maker"];
return maker ( typeid(A).name() , xp );
}
Still, I don't know how to get the object back in as an parameter to a method/function. The following is not working:
template <> A* as( SEXP obj){
Rcpp::List l(obj);
Rcpp::XPtr<A> xp( (SEXP) l[".pointer"] );
return (A*) xp;
}
So how could I get the external pointer to the C++ object from the S4 object provided as SEXP in the parameter list?