Skip to content
Prev 35505 / 63424 Next

R object protection [Was: R-devel Digest, Vol 83, Issue 2]

On 01/03/2010 01:34 AM, Simon Urbanek wrote:
Many thanks for this clarification. Rcpp is using a jri wanabee 
approach. Essentially we have :

class RObject{
public:
RObject(SEXP x){ preserve x }
~RObject(){ release x}
private:
SEXP x ;
}

For the story, I'm also doing the other way (rJava wanabee) in the CPP 
package 
(http://romainfrancois.blog.free.fr/index.php?post/2009/12/22/CPP-package-%3A-exposing-C-objects) 
that wraps up arbitrary C++ objects (currently stl containers) as 
external pointers. You might recognize some patterns here.

 > # create the object
 > x <- new( CPP("set<int>") )
 > # insert data using the insert method
 > x$insert( sample( 1:20, size = 50, replace = TRUE ) )
 > # ask for the size of the set
 > x$size()
[1] 17
 > # bring it back as an R classic integer vector
 > # see how it comes back sorted and unique (which is set's job)
 > as.vector(x)
  [1]  1  3  4  6  7  8  9 10 11 12 13 14 15 17 18 19 20


no magic however since C++ reflection capabilities are very limited, 
well some magic based on the brew package.

Romain