[Rcpp-devel] Extending as-wrap question

Finlay Scott drfinlayscott at gmail.com
Mon Apr 29 09:28:36 CEST 2013


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.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130429/cca63d31/attachment.html>


More information about the Rcpp-devel mailing list