Hello,

I have added (in rev 3120) some code that allows the developper to register his own finalizer instead of the default one.

For example:

require( Rcpp )
require( inline )

inc <- '

    class Foo{
        public:
            Foo(double x_) :x(x_){}
            double x;
    } ;

    void bla(Foo* x){
        Rprintf( "hello\\n" ) ;
    }

'
fx <- cxxfunction( , '
    Foo* f = new Foo(2.0) ;
    XPtr<Foo,bla> xp(f,true) ;
    return xp ;
', inc = inc, plugin = "Rcpp" )
fx()
gc()
gc()

This is achieved by adding a second template parameter to XPtr, with a default value so that the original behaviour is retained.

The class signature of XPtr hence becomes:

template <typename T, void Finalizer(T*) = standard_delete_finalizer<T> >
class XPtr : public RObject

The second parameter is a function that takes a a single argument (a pointer to the target class).

Hope this gives you what you need.

Romain


Le 04/07/11 01:02, Steve Lianoglou a écrit :
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
:-) 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;
}

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 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):

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?

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)?

Thanks,

-steve

[1] shogun-toolbox: http://www.shogun-toolbox.org/



--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
http://romain-francois.com
|- http://bit.ly/lJoWbH : Montpellier Comédie Club - Juin 2011
|- http://bit.ly/kaSV6U : Stand up set at Up The Creek
`- http://bit.ly/hdKhCy : Rcpp article in JSS


_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to