[Rcpp-devel] using Rcpp modules to expose class

Robin Girard robin.girard at mines-paristech.fr
Wed May 15 09:43:07 CEST 2013


I'll try to contribute to the gallery.rcpp.org, after next week. 
R.

----- Mail original -----
De: "Dirk Eddelbuettel" <edd at debian.org>
À: "Anwar Ludin" <anwar.ludin at riskcetera.com>
Cc: "Robin Girard" <robin.girard at mines-paristech.fr>, "Dirk Eddelbuettel" <edd at debian.org>, rcpp-devel at lists.r-forge.r-project.org
Envoyé: Mardi 14 Mai 2013 16:40:37
Objet: Re: [Rcpp-devel] using Rcpp modules to expose class


On 14 May 2013 at 16:17, Anwar Ludin wrote:
| Thanks guys! This is exactly what I was looking for...tested it & works :-)

Nice.

Now it would be even nicer for the rest of us if between you and Robin
someone could volunteer to document this in

   either a new section for the vignette

   or a contributed page / article for gallery.rcpp.org

Dirk
 
 
| On 05/13/2013 10:06 PM, Robin Girard wrote:
| > Actually I have integrated more complex classes copying Romain's example.
| > This included polymorphism (see the answers and discussion around question with subject "Module with vector of a class with inheritance, how to avoid slicing" on this list). I failed to handle very well the case of a template class which might reach the limit of the tool, this was the purpose of my question with subject  "module with a template class" on this list. That question wasn't answered by the way (I see your activity and I confirm this is one over a million)
| >
| > R.
| >
| >
| > ----- Mail original -----
| > De: "Dirk Eddelbuettel" <edd at debian.org>
| > À: "Robin Girard" <robin.girard at mines-paristech.fr>
| > Cc: "Dirk Eddelbuettel" <edd at debian.org>, "Anwar Ludin" <anwar.ludin at riskcetera.com>, rcpp-devel at lists.r-forge.r-project.org
| > Envoyé: Lundi 13 Mai 2013 21:38:34
| > Objet: Re: [Rcpp-devel] using Rcpp modules to expose class
| >
| >
| > On 13 May 2013 at 21:25, Robin Girard wrote:
| > | http://romainfrancois.blog.free.fr/index.php?post/2012/10/25/Rcpp-modules-more-flexible
| >
| > Oh a good one indeed -- I had forgotten about that aspect (and in fact not
| > used it yet in a project of mine).
| >
| > This may indeed work for Anwar, so thanks for the pointer.
| >
| > [ That said, I do like explicit code too and the control one has which
| > explicit converters does come on handy.  Also, his real class may well be
| > more complicated than the token example from the blog posts so things may or
| > may not work.  Proof is in the pudding and all that. ]
| >
| > Dirk
| >
| > | R.
| > |
| > | ----- Mail original -----
| > | De: "Dirk Eddelbuettel" <edd at debian.org>
| > | À: "Robin Girard" <robin.girard at mines-paristech.fr>
| > | Cc: "Anwar Ludin" <anwar.ludin at riskcetera.com>, rcpp-devel at lists.r-forge.r-project.org, "Dirk Eddelbuettel" <edd at debian.org>
| > | Envoyé: Lundi 13 Mai 2013 19:25:09
| > | Objet: Re: [Rcpp-devel] using Rcpp modules to expose class
| > |
| > |
| > | On 13 May 2013 at 19:05, Robin Girard wrote:
| > | | As suggested in the example of Romain's Blog
| > | | here http://romainfrancois.blog.free.fr/
| > |
| > | There are dozens of posts regarding Rcpp on his blog.  If you refer to one
| > | such post, could you show us the relevant (and presumably still valid) URL?
| > |
| > | Otherwise we just don't know what you refer to.
| > |
| > | | you need to define the class before "exposing" it and then implement it (I think it is also important that using namespace Rcpp  comes after the exposed stuff).
| > | |
| > | | class Foo ;
| > | | RCPP_EXPOSED_CLASS(Foo)
| > | |
| > | | using namespace Rcpp ;
| > | |
| > | | class Foo{
| > | |     public:
| > | |         enum Bla{ FOO, BAR } ;
| > | | ...
| > | |
| > | |
| > | | is it what you did ?
| > | | I made it work in some cases and I did not have to implement the wrap
| > | | (which is the whole purpose of module I think).
| > |
| > | Modules help you with setters/getters of _components of a class you expose_
| > | in such a declaration.
| > |
| > | What Anwar wants here is different: he wants to pass the whole class as is.
| > | For which conversion code has to come from somewhere: hence the need for wrap().
| > |
| > | I could of course be misunderstanding the issue too..
| > |
| > | Dirk
| > |
| > |
| > | | R.
| > | |
| > | | ----- Mail original -----
| > | | De: "Anwar Ludin" <anwar.ludin at riskcetera.com>
| > | | À: "Dirk Eddelbuettel" <edd at debian.org>
| > | | Cc: rcpp-devel at lists.r-forge.r-project.org
| > | | Envoyé: Lundi 13 Mai 2013 16:11:35
| > | | Objet: Re: [Rcpp-devel] using Rcpp modules to expose class
| > | |
| > | | Hi Dirk,
| > | |
| > | | Thanks for your answer!
| > | |
| > | | So if I understand correctly, the fact that I exposed previously
| > | | Portfolio is not enough?
| > | | I still need to provide wrap the template specialization:
| > | |
| > | |      template <> SEXP wrap( const Portfolio& ) ;
| > | |
| > | | Thanks,
| > | |
| > | | Anwar
| > | |
| > | | On 05/13/2013 03:28 PM, Dirk Eddelbuettel wrote:
| > | | > On 13 May 2013 at 14:59, Anwar Ludin wrote:
| > | | > | Hello,
| > | | > |
| > | | > | I'm trying to expose 2 classes from a Rcpp module:
| > | | > |
| > | | > | class Portfolio{
| > | | > |   private:
| > | | > |      std::string portfolioId, description;
| > | | > |   public:
| > | | > |       Portfolio(std::string portfolioId, std::string description)
| > | | > |       : portfolioId(portfolioId), description(description) {}
| > | | > |
| > | | > |       std::string getPortfolioId() {return portfolioId;} const
| > | | > |       void setPortfolioId(const std::string&) {this->portfolioId =
| > | | > | portfolioId;}
| > | | > |
| > | | > |       std::string getDescription() {return description;} const
| > | | > |       void setDescription(const std::string& description){this->description =
| > | | > | description;}
| > | | > | };
| > | | > |
| > | | > | class PortfolioDataAccess{
| > | | > |     private:
| > | | > |     mongo::DBClientConnection c;
| > | | > |
| > | | > |     public:
| > | | > |     PortfolioDataAccess();
| > | | > |     virtual Portfolio read(std::string portfolioId);
| > | | > |     virtual void create(std::string portfolioId, std::string description);
| > | | > |
| > | | > |   };
| > | | > |
| > | | > |
| > | | > |
| > | | > |
| > | | > | RCPP_MODULE(riskceteraPortfolio) {
| > | | > |   class_<riskcetera::Portfolio>( "Portfolio" )
| > | | > |   .constructor<std::string, std::string>()
| > | | >
| > | | > That works because we can convert "in" from SEXP to std::strings.
| > | | >
| > | | > |   .method("id", &riskcetera::Portfolio::getPortfolioId)
| > | | > |   .method("description", &riskcetera::Portfolio::getDescription)
| > | | > |   ;
| > | | > |
| > | | > | class_<riskcetera::PortfolioDataAccess>("PortfolioAccess")
| > | | > |   .constructor()
| > | | > |   .method("read", &riskcetera::PortfolioDataAccess::read)
| > | | > |   ;
| > | | > | }
| > | | > |
| > | | > | When trying to compile the module I get the following error:
| > | | > |
| > | | > |
| > | | > | /riskcetera/home/aludin/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include/Rcpp/internal/wrap.h:474:7: error: no viable conversion from 'const riskcetera::Portfolio' to 'SEXP' (aka 'SEXPREC *')
| > | | > |         SEXP x = object ;
| > | | >
| > | | > That is pretty plain:  You need to supply a wrap() converter that tells the
| > | | > compiler how to turn one of your objects ("Portfolio") into R's standard
| > | | > type, the SEXP.
| > | | >
| > | | > The currently-on-CRAN version of RcpBDT may help you.  It does something
| > | | > pretty simply with Boost Date_Time, converting dates between the Boost
| > | | > representation and the Rcpp / R representation.  It uses custom as<>() and
| > | | > wrap(), and exposes a handful of useful functions too.   [ And do look at the
| > | | > CRAN version, the R-Forge version is in heavier development which I started
| > | | > last fall and hope to get back to by the summer. ]
| > | | >
| > | | > Dirk
| > | | >
| > | |
| > | |
| > | | _______________________________________________
| > | | Rcpp-devel mailing list
| > | | Rcpp-devel at lists.r-forge.r-project.org
| > | | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| > |
| > | --
| > | Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| >
| 
| 

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list