On 13 March 2013 at 18:27, Suresh Easwar wrote: | All I want to do is obtain a shared-pointer equivalent in R to a C++ object and | be able to do two things with it - 1. Invoke methods of the object via the | shared-pointer and 2. pass the shared-pointer to the methods of other C++ class | objects. | | In Python, I am able to do this like so (Boost shared-ptrs are easily passed | back and forth across the Python-C++ boundary using Boost-Python): | >>> aptr = mytest.get_aptr() # obtain a shared-pointer to an instance of class | A | >>> aptr.f() # invoke method f() of class A via the pointer | >>> b = mytest.B(aptr) # construct object of class B invoking constructor that | accepts a shared-pointer to object of class A. | | I am looking for the equivalent in R (or at least a close enough idiom).
Thinking by analogy is not always the best way. In essence, Rcpp modules already give you that pointer. It is not clear to me why you can't use it as is. Dirk | Thanks | suresh | | | ------------------------------------------------------------------------------- | From: Dirk Eddelbuettel <e...@debian.org> | To: Suresh Easwar <seas...@yahoo.com> | Cc: "rcpp-devel@lists.r-forge.r-project.org" | <rcpp-devel@lists.r-forge.r-project.org> | Sent: Wednesday, March 13, 2013 5:56 PM | Subject: Re: [Rcpp-devel] Invoking class methods via XPtr<> objects in R | | | On 13 March 2013 at 10:15, Suresh Easwar wrote: | | How does one invoke class methods via XPtr<T> objects in R? I get this error: | Error in aptr$f : object of type 'externalptr' is not subsettable: | | I don't understand what you are trying to do below, and do not have time to | understand and debug it for you. | | Your example is also complicated as you mix XPtr and Modules. Maybe you will | have more success walking in smaller steps --- and a working example of XPtr | via Rcpp is in the RcppDE package where I use XPtr to pass external | (user-given) C interface functions around to the optimiser. | | Dirk | | | | | cpp file: | | #include <Rcpp.h> | | struct A { | | void f() {} | | }; | | typedef Rcpp::XPtr<A> APtr; | | APtr getAPtr() | | { | | return APtr(new A); | | } | | struct B { | | B(APtr) { } | | }; | | RCPP_MODULE(mytest) | | { | | Rcpp::function("get_aptr", &getAPtr); | | Rcpp::class_<A>("A") | | .constructor() | | .method("f", &A::f) | | ; | | Rcpp::class_<B>("B") | | .constructor<APtr>() | | ; | | } | | | | R session: | | R version 2.15.3 (2013-03-01) -- "Security Blanket" | | Copyright (C) 2013 The R Foundation for Statistical Computing | | ISBN 3-900051-07-0 | | Platform: x86_64-unknown-linux-gnu (64-bit) | | > library(mytest) | | Loading required package: Rcpp | | > a <- new(A) | | > aptr <- get_aptr() | | > b <- new(B, aptr) | | > a$f() | | > aptr$f() <- not sure how to invoke f() via aptr | | Error in aptr$f : object of type 'externalptr' is not subsettable | | > | | | | Thanks | | | | Suresh | | _______________________________________________ | | Rcpp-devel mailing list | | Rcpp-devel@lists.r-forge.r-project.org | | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel | | -- | Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com | | -- Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel