[Rcpp-devel] Rcpp and ExternalPtr

Dirk Eddelbuettel edd at debian.org
Sun Dec 7 11:44:24 CET 2014

On 5 December 2014 at 05:37, JJ Allaire wrote:
| I think the XPtr class is what you are looking for:


XPtr is perfect for managing external resources.
| http://www.r-bloggers.com/external-pointers-with-rcpp/

And there are four Rcpp Gallery posts highlighted as a result of a you search for XPtr.

| On Fri, Dec 5, 2014 at 2:45 AM, Jeroen Ooms <jeroen.ooms at stat.ucla.edu> wrote:
| > Does Rcpp provide some elegant way to let the R user manage persistent
| > c++ objects that have no R type (e.g. internal handles or sessions of
| > some sort)?
| >
| > In C, I would use R_MakeExternalPtr to create a ptr SEXP that the user
| > can pass from one R function to another. This also makes it easy to
| > add a finalizer with R_RegisterCFinalizerEx so that the garbage
| > collector will automatically clean up the session/handle whenever user
| > deletes the R object.

The ability of _explicitly_ managing resource deterministically via
constructors and destructors is one of major differences of C++ you will come
to enjoy here. 

| > Most Rcpp examples I have seen use function arguments with standard
| > data types that are automatically mapped to R types. Does Rcpp have
| > any special mechanics for managing external objects (pointers) from R
| > which do not map to a SEXP type? Or do I have to fall back on
| > Rinternals for this?

You can do better.  The Rcpp + bigmemory examples may give you one idea,
there may well be other packages doing useful tricks so you could try one of
the code search engines.

Dirk, enjoying some free wifi at Zurich airport  

http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org

More information about the Rcpp-devel mailing list