[Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?
Sorry for the long subject line but I hope it describes what I want to do. I have a C++ class, dgTMatrix, that is exported in a module definition. RCPP_MODULE(RcppSp) { ... class_("dgCMatrix") ... ; } Many of the methods for this class return other members of this class. Can I create a new instance of the reference class in R from within the module? I'm sure it is possible, I'm just wondering if there is an easy way to do this without duplicating large chunks of the code in Rcpp/src/Module.cpp. Can i somehow clone the SEXP representing the current instance then swap the external pointer to the C++ object? ___ 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
Re: [Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?
This is something we have to add at some point. At the moment the only way i see is to make an R call to "new" and construct the object this way... Le 2 juin 2011 à 21:31, Douglas Bates a écrit : > Sorry for the long subject line but I hope it describes what I want to > do. I have a C++ class, dgTMatrix, that is exported in a module > definition. > > RCPP_MODULE(RcppSp) { > ... >class_("dgCMatrix") >... >; > } > > Many of the methods for this class return other members of this class. > Can I create a new instance of the reference class in R from within > the module? I'm sure it is possible, I'm just wondering if there is > an easy way to do this without duplicating large chunks of the code in > Rcpp/src/Module.cpp. Can i somehow clone the SEXP representing the > current instance then swap the external pointer to the C++ object? > ___ > 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 ___ 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
Re: [Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?
> At the moment the only way i see is to make an R call to "new" and construct > the object this way... >> Many of the methods for this class return other members of this class. >> Can I create a new instance of the reference class in R from within >> the module? I'm sure it is possible, I'm just wondering if there is >> an easy way to do this without duplicating large chunks of the code in >> Rcpp/src/Module.cpp. Can i somehow clone the SEXP representing the >> current instance then swap the external pointer to the C++ object? To elaborate a bit: Am I off-base in thinking you can make a custom constructor for your class that calls R's new(), being careful to avoid circularity, that returns a blank instance to your method to be filled? Is something like the following anywhere near this? class MyClass { public: Bar(bool x_) : {} // for spawn Bar() : {} private: Bar spawn() { Environment meth("package:methods"); Function Rnew = meth["new"]; Rcpp::XPtr NewObj( Rnew(false) ); } }; -Christian -- A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama! ___ 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
Re: [Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?
> At the moment the only way i see is to make an R call to "new" and construct > the object this way... >> Many of the methods for this class return other members of this class. >> Can I create a new instance of the reference class in R from within >> the module? I'm sure it is possible, I'm just wondering if there is >> an easy way to do this without duplicating large chunks of the code in >> Rcpp/src/Module.cpp. Can i somehow clone the SEXP representing the >> current instance then swap the external pointer to the C++ object? I played with this a little more, and the following works fine. Nothing striking here. The part I don't understand is, can you employ the SEXP returned by spawn() as an instance of Bar, or are you then stuck with the R interface to Bar? -Christian ## in R Rcpp.package.skeleton('testmod', module=T) //in testmod/src/rcpp_module.cpp #include using namespace Rcpp; class Bar { public: Bar() {} void set(int state_) { this->state = state_; } int get() { return state; } SEXP spawn() { Environment meth("package:methods"); Function Rnew = meth["new"]; Function Rget("get"); SEXP NewObj( Rnew(Rget("Bar")) ); return NewObj; } private: int state; }; ## in R require(testmod) bb = new(Bar); bb$set(3) cc = bb$spawn(); cc$set(4) bb$get(); cc$get() -- A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama! ___ 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
Re: [Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?
Le 06/06/11 06:09, Christian Gunning a écrit : At the moment the only way i see is to make an R call to "new" and construct the object this way... Many of the methods for this class return other members of this class. Can I create a new instance of the reference class in R from within the module? I'm sure it is possible, I'm just wondering if there is an easy way to do this without duplicating large chunks of the code in Rcpp/src/Module.cpp. Can i somehow clone the SEXP representing the current instance then swap the external pointer to the C++ object? I played with this a little more, and the following works fine. Nothing striking here. The part I don't understand is, can you employ the SEXP returned by spawn() as an instance of Bar, or are you then stuck with the R interface to Bar? -Christian ## in R Rcpp.package.skeleton('testmod', module=T) //in testmod/src/rcpp_module.cpp #include using namespace Rcpp; class Bar { public: Bar() {} void set(int state_) { this->state = state_; } int get() { return state; } SEXP spawn() { Environment meth("package:methods"); Function Rnew = meth["new"]; Function Rget("get"); SEXP NewObj( Rnew(Rget("Bar")) ); return NewObj; } private: int state; }; ## in R require(testmod) bb = new(Bar); bb$set(3) cc = bb$spawn(); cc$set(4) bb$get(); cc$get() I'm playing with this now. More later ... Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr http://romain-francois.com |- http://bit.ly/kaSV6U : Stand up set at Up The Creek |- http://bit.ly/hdKhCy : Rcpp article in JSS `- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011 ___ 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
Re: [Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?
Le 02/06/11 22:31, Douglas Bates a écrit : Sorry for the long subject line but I hope it describes what I want to do. I have a C++ class, dgTMatrix, that is exported in a module definition. RCPP_MODULE(RcppSp) { ... class_("dgCMatrix") ... ; } Many of the methods for this class return other members of this class. Can I create a new instance of the reference class in R from within the module? I'm sure it is possible, I'm just wondering if there is an easy way to do this without duplicating large chunks of the code in Rcpp/src/Module.cpp. Can i somehow clone the SEXP representing the current instance then swap the external pointer to the C++ object? As part of the changes I've made today [see the post "returning pointers in module methods" ], there is this function you might be interested in (in Module.h): namespace internal { template SEXP make_new_object( Class* ptr ){ Rcpp::XPtr xp( ptr, true ) ; Function maker = Environment::Rcpp_namespace()[ "cpp_object_maker"] ; return maker( typeid(Class).name() , xp ) ; } } Maybe I should make it more public (i.e not have it in internal). Anyway, if you give this function a pointer to an instance of a class that has been exposed through modules, it will create the associated R ref class object. Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr http://romain-francois.com |- http://bit.ly/kaSV6U : Stand up set at Up The Creek |- http://bit.ly/hdKhCy : Rcpp article in JSS `- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011 ___ 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