Skip to content

[Rcpp-devel] Ease passing pointers

5 messages · Dirk Eddelbuettel, Tim Keitt

#
Can the following be simplified?

(Typed in email, so not live code.)

// [[Rcpp::export]]
void func(SEXP x)
{
  XPtr<aType> y(x);
  func2(&*y);
  y->memfunc();
}

Is there a way to avoid the need to construct the XPtr in the 1st line?
Hoping for:

// [[Rcpp::export]]
void func(aType* x)
{
  func2(x);
  x->memfunc();
}

THK
#
On 31 December 2013 at 14:16, Tim Keitt wrote:
| Can the following be simplified?
| 
| (Typed in email, so not live code.)
| 
| // [[Rcpp::export]]
| void func(SEXP x)
| {
| ? XPtr<aType> y(x);
| ? func2(&*y);
| ? y->memfunc();
| }
| 
| Is there a way to avoid the need to construct the XPtr in the 1st line? Hoping
| for:
| 
| // [[Rcpp::export]]
| void func(aType* x)
| {
| ? func2(x);
| ? x->memfunc();
| }

We don't know what func2() is, or what it's signature is. Ditto for y. 

In general, we go the route of using XPtr if we need to get the pointer back
to R, maybe because our R logic then dispatches it somewhere else.

Your examples are admiringly short, which is very good. They are also
incomplete and a little incomprehensible which is less good :)

Cheers, Dirk
#
On Tue, Dec 31, 2013 at 3:10 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
A little obscure indeed. I got a little lost in the documentation and was
looking for a shortcut. I think I need to specialize "wrap" and "as".

THK
#
On 31 December 2013 at 15:27, Tim Keitt wrote:
| A little obscure indeed. I got a little lost in the documentation and was
| looking for a shortcut. I think I need to specialize "wrap" and "as".

Yes, many adaptations to local data structures need that. There are a few
example posts, eg on the Rcpp Gallery (http://gallery.rcpp.org) which should
get you started.

Dirk
#
On Tue, Dec 31, 2013 at 3:55 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
Tried it out. There is however an issue using it with attributes with
Rcpp::export, which is that types defined in the source file do not get
carried over to RcppExports.cpp, at least as far as I can tell. So

class target { ... } ;

target as<target>(SEXP x) { ... }
SEXP wrap<target>(const target& x) { ... }

[[Rcpp::export]]
target doSomething(target x)
{
...
}

does not work because the type "target" is undefined in the RcppExports.cpp
file.

Is there a way to include an arbitrary header in RcppExports.cpp?

THK