Skip to content
Prev 4643 / 10988 Next

[Rcpp-devel] inline plug-in linking

Thanks for the help. I just thought I should report back with the solution to my issues for posterity.

It seems that the header only interface is the way to go for my purposes because I only have a few data structures which need to be shared across compilation units, and almost all of the code is in the headers. To expose the data structures, I created a few accessor functions like:

void registerDirectedStatistic(Rcpp::XPtr< ernm::Stat<ernm::Directed> > ps){
	ernm::StatController<ernm::Directed>::addStat(
			std::tr1::shared_ptr< ernm::Stat<ernm::Directed> >(ps->cloneUnsafe()));
}

Something that had tripped me up is that I had previously had my objects returning safe pointers (shared_ptr) when they were cloned. For reasons beyond my current understanding, these pointers became unsafe when passed from code compiled in inline, to the package object. This caused the segfault that I reported before. Next, I followed JJ's advise and registered the function with R, and created a macro to simplify calling

R_RegisterCCallable("ernm",
	"registerDirectedStatistic",(DL_FUNC) &registerDirectedStatistic);

#define REGISTER_DIRECTED_STATISTIC(x) ((void(*)(Rcpp::XPtr< ernm::Stat<ernm::Directed> >))R_GetCCallable("ernm", "registerDirectedStatistic"))(x)

I also exposed the function in my module.

	function("registerDirectedStatistic",&registerDirectedStatistic);

So now there are two ways for the user to add Stats to the data structure. They can pass a pointer up to R, and then down to ernm.

getPointer <- cxxfunction(signature(), 
	"
	return Rcpp::XPtr< Stat<Directed> >(new Edges2<Directed>());
	", 
	plugin="ernm",includes=src)
pointerToNewStat <- getPointer()
registerDirectedStatistic(pointerToNewStat)

or, just register it directly in compiled code

registerEdges2Statistic <- cxxfunction(signature(), 
	"
	Rcpp::XPtr< Stat<Directed> > ps(new Edges2<Directed>());
	REGISTER_DIRECTED_STATISTIC(ps);
	", 
	plugin="ernm",includes=src)
registerEdges2Statistic()

Best,
Ian
On Nov 16, 2012, at 8:50 AM, JJ Allaire <jj.allaire at gmail.com> wrote: