[Rcpp-devel] Registering a custom delete_finalizer for my XPtrs

Dirk Eddelbuettel edd at debian.org
Mon Jul 4 10:00:19 CEST 2011


Hi Steve,

On 3 July 2011 at 19:02, Steve Lianoglou wrote:
| Greetings,
| 
| Preamble: My C++ is quite ... hmmm, does anybody know any good
| euphemisms for "weak"?. So, sorry if this (or questions that will
| likely follow) is too basic.
| 
| I've been using Rcpp to help make a more R-like wrapper library to the
| shogun-toolbox[1]. So far, it's been pretty fun, and (to my surprise

Cool! You mean 'more R-like' in the sense of augmenting / replacing the
wrapper they have? Which, I guess, uses Swig or something like Swig as Shogun
wraps to R, Octave, Python, ...  

I guess you also looked into altering their wrapper?

| :-) I've been able to get basic things working, such as building a
| handful of different types of SVMs. A testament to the quality of both
| Rcpp and the shogun-toolbox code, since, as I said, my C++ isn't "the
| best".
| 
| I know I'm not making the most out of using Rcpp, and wanted to write
| my code in a more Rcpp-inspired manner before I get too deep. (I
| actually don't know if Rcpp-modules would work here, but maybe will
| try that in the future as well).
| 
| So, in my C++ code, I wire different objects together, and return a
| pointer to the shogun object that I just made. Currently I've got a
| simple function that wraps pointers to shogun objects into externalptr
| SEXP's and registers my custom finalizer, which basically does what
| you'd expect, eg:
| 
| SEXP SG2SEXP(shogun::CSGObject *o) {
|     SEXP xp = R_MakeExternalPtr(o, R_NilValue, R_NilValue);
|     R_RegisterCFinalizer(xp, _shogun_ref_count_down);
|     return xp;
| }

That seems quite right. I would have to check details as I don't use R's
external pointers (or even our XPtr) all that frequently.
 
You could use our XPtr class instead too.  I do so in RcppDE to wrap a
pointer to a user-supplied C function.

| What I thought a more Rcpp-inspired thing to do is to instead
| instantiate pointers to shogun objects using
| Rcpp::XPtr<SomeShogunObject>and just rely on the auto-wrapping to make
| things "more clean", maybe like:
| 
| // ...
| Rcpp::XPtr<SomeShogunObject> so(new SomeShogunObject(what,ever), true);
| so->do_something(special);
| // ...
| return so;

But where would the logic to deal with 'SomeShogunObject' come from?
 
| But I don't want Rcpp's "vanilla" delete_finalizer to be invoked on my
| object's R-side destruction -- I want whatever I've defined in my
| `_shogun_ref_count_down` function to be used instead.
| 
| (this is where my n00b-ness becomes self-evident):

No n00bness here. You are trying pretty advanced stuff.

I would always go pedestrian first. Make contact with Shogun (as you did).
Call functions, return results (as you did).  See that it does sensible stuff
in sensible time, establish a baseline to compare against.  Only then go
crazy :)

Because the persistence stuff is harder.  It helps to know the other projects
internals well enough.  So my advice would be to keep the fancy stuff for
'version 2.0'.  Unless you have plenty of time to learn and try fancy things.
 
| Since XPtr::setDeleteFinalizer doesn't take a parameter for the
| finalizer function to use, I thought I the "expected" way to achieve
| this is through template specialization of delete_finalizer in my
| package. Since every object in the shogun-toolbox extends CSGObject, I
| thought this would be easy enough and maybe do something like:
| 
| template<>
| void delete_finalizer<shogun::CSGObject>(SEXP p) { /* something special */ }
| 
| But that doesn't work/compile -- I guess because this specialization
| and Rcpp's delete_finalizer definition aren't in the same compilation
| units, as described here:
| 
| http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
| 
| -- or maybe I have to stick it in the Rcpp namespace somehow, like the
| custom wrap/as trick?

You're doing good.  They may be a way to use Rcpp along with this -- just
like the Rcpp-package vignette shows with the 'three ways to do wrap' (and as).

I would have to think through if we really had an example where the entire
class hierarchy descends from a 'base' object. I don't think this fits the
Armadillo or Eigen mold.  But it is a good thing to try---I think that one
day someone will want to do the same for the wonderful Qt library too.  

| So -- long story short ... is there a more Rcpp/spiffy way for me to
| register my own delete_finalizer function, or should I just keep going
| with my SG2SEXP function for now, and save the Rcpp-swagger for when I
| see if Rccp-modules is a good fit (and just use its *.finalizer()
| business)?

In a way Rcpp modules is orthogonal, and also more restricted (but easier for
simple connections to libraries).

Hope this helps,  Dirk
 
| Thanks,
| 
| -steve
| 
| [1] shogun-toolbox: http://www.shogun-toolbox.org/
| 
| -- 
| Steve Lianoglou
| Graduate Student: Computational Systems Biology
|  | Memorial Sloan-Kettering Cancer Center
|  | Weill Medical College of Cornell University
| Contact Info: http://cbio.mskcc.org/~lianos/contact
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list