Skip to content
Prev 7321 / 10988 Next

[Rcpp-devel] named and default arguments in exposed C++ classes

Thanks a lot for your quick response,

I got a working version, also inspired by
your comments from
http://stackoverflow.com/questions/12405655/returning-a-custom-object-from-a-wrapped-method-in-rcpp.

I did something like this in my simple example
(maybe some typos, I tried with my real package..)

for Foo:

template <> Foo* as( SEXP obj)
{
	Rcpp::Environment e(obj);
   	Rcpp::XPtr<Foo> exPointer( (SEXP) e.get(".pointer") );
   	return (Foo*) exPointer;
}

RCPP_EXPOSED_CLASS(Foo)

SEXP createFoo() {
     Rcpp::XPtr<Foo> exPointer( new Foo(), true ) ;
     Function maker=Environment::Rcpp_namespace()[ "cpp_object_maker"];
     return maker ( typeid(Foo).name() , exPointer );
}
int bar(Foo* f,int x, int y) {return f->bar(x,y);}

RCPP_MODULE(PsiMin) {
	function( "createFoo", &createFoo);
	function( "bar", &bar, List::create(_["f"], _["x"], _["y"] = 2));	
}
in R:
 > f <- createFoo()
 > bar(f = f, x = 3);

Is this OK, are there some memory issues I've overlooked?

What I'm now thinking of (because in my case there are
normally only 1-16 objects created, depending on the cpu cores),
is to store the objects on the C++ side,
so a call of
bar(f = f, x = 3);
would be
bar(3) //calling first object Foo::f inside package

But what is with the  allocated memory?
Of course I could write a cleanup method,
but this seems not very elegant.

Is there a safe way, some kind of "hooked method",
that will be always called when the module
is unloaded. I only found the finalize method
in the modules/class context.

(Maybe a question for a new thread)


Andreas





Am 05.03.2014 16:36, schrieb Dirk Eddelbuettel: