[Rcpp-devel] Extending as-wrap question

Dirk Eddelbuettel edd at debian.org
Mon Apr 29 13:19:38 CEST 2013


On 29 April 2013 at 09:28, Finlay Scott wrote:
| Hi,
| I have some questions regarding extending 'as' and 'wrap' for custom classes.
| 
| In my simple example I have declared a new class which has a single member.
| This member is of type NumericVector, i.e. it is an Rcpp class. I have written
| simple 'as' and 'wrap' extensions using non-intrusive extension. My simple
| example compiles and appears to work as expected.
| 
| My question is about the placement of #include <Rcpp.h>. In the Rcpp-extending
| vignette, it says that for non-intrusive extension, the #include <Rcpp.h> must
| appear after the specialisation otherwise the specialisation will not be seen
| by Rcpp types. However, if I do this compilation fails because the
| NumericVector member is not recognised by the compiler in the class definition.
| 
| As an experiment, I moved the #include <Rcpp.h> to before the class definition.
| My example then compiles and appears to run correctly. I was surprised because
| what I did appears to contradict the advice given the vignette.

Headers got moved / Rcpp.h got reorganized after the vignette was written. So
there is a chance something in there is outdated.

I am not sure if your example below qualifies for our intrusive/non-intrusive
schema: you are having your class in the same file, I think we consider that
'intrusive' as you can modify your source.

Main story: It works for you so ...

Dirk
 
| So my questions are:
| Should I be surprised that this example works even though it appears I have
| contradicted the advice in the vignette?
| Although my example appears to work, because I have contradicted the advice in
| the vignette, is there now something bad lurking in the application. i.e.
| things appear fine with my simple example but will something blow up if I
| continue to develop my class?
| 
| My example cpp code is as follows:
| 
| // File: DummyClass_with_Rcpp_member_example.cpp
| #include <RcppCommon.h>
| // If #include <Rcpp.h> not placed here then compilation fails
| #include <Rcpp.h>
| 
| class DummyClass {
|     public:
|         Rcpp::NumericVector data;
| };
| 
| namespace Rcpp {
|     // non-intrusive extension via template specialisation
|     template <> DummyClass as(SEXP dc_sexp);
|     // non-intrusive extension via template specialisation
|     template <> SEXP wrap(const DummyClass &flq);
| }
| 
| // Compilation fails if #include<Rcpp.h> placed here
| //#include <Rcpp.h>
| 
| // define template specialisations for as and wrap
| namespace Rcpp {
|     template <> DummyClass as(SEXP dc_sexp) {
|     S4 dc_s4 = Rcpp::as<S4>(dc_sexp);
|     DummyClass dc;
|     dc.data = dc_s4.slot(".Data");
|         return dc;
|     }
| 
|     template <> SEXP wrap(const DummyClass &dc) {
|     Rcpp::S4 dc_s4("DummyClass");
|     dc_s4.slot(".Data") = dc.data;
|         return Rcpp::wrap(dc_s4);
|     }
| }
| 
| // [[Rcpp::export]]
| DummyClass test_DC_as_wrap(DummyClass dc, double multiplier){
|     DummyClass new_dc;
|     new_dc.data = dc.data;
|     new_dc.data(0) = new_dc.data(0) * multiplier;
|     return new_dc;
| }
| 
| The R code that runs the example is:
| 
| library(Rcpp)
| sourceCpp("DummyClass_with_Rcpp_member_example.cpp")
| setClass("DummyClass", representation(.Data = "numeric"))
| dc <- new("DummyClass")
| dc at .Data <- 1:10
| test_DC_as_wrap(dc, 4)
| 
| Yours
| 
| Finlay
| 
| ----------------------------------------------------------------------
| _______________________________________________
| 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


More information about the Rcpp-devel mailing list