Skip to content
Prev 8259 / 10988 Next

[Rcpp-devel] Rcpp and ExternalPtr

On Sun, Dec 7, 2014 at 4:52 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
I don't know if this example is helpful. Any class that can be initialized
with an SEXP and has an SEXP member function can be used as the argument or
return type from an Rcpp function call. I wrote this wrapper to avoid
dereferencing passed null pointers and as a return type for a lot of
functions where I want an R NULL return if the pointer is null.

THK

class RGDALHandleWrapper{public: RGDALHandleWrapper(void* h) : handle(h) {} //
If initialized NULL will return NULL SEXP RGDALHandleWrapper(const SEXP h)
: handle(R_ExternalPtrAddr(h)) { if ( !handle ) stop("Null pointer passed
to function\n"); } operator SEXP() const { return handle ? // Return NULL
SEXP on NULL pointer R_MakeExternalPtr(handle, R_NilValue, R_NilValue) :
R_NilValue; } void* operator*() const { return handle; }private: void*
handle;};