[Rcpp-devel] using Rcpp modules to expose class

Dirk Eddelbuettel edd at debian.org
Mon May 13 15:28:02 CEST 2013


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

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


More information about the Rcpp-devel mailing list