<div dir="ltr"><br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Finlay Scott</b> <span dir="ltr"><<a href="mailto:drfinlayscott@gmail.com">drfinlayscott@gmail.com</a>></span><br>
Date: Mon, Apr 29, 2013 at 1:28 PM<br>Subject: Re: [Rcpp-devel] Extending as-wrap question<br>To: Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>><br><br><br><div dir="ltr"><div><div>Great. Thanks for the reply.<br>
<br></div><div>I based my example on the non-intrusive example in the Rcpp gallery which I was why I was still calling it non-intrusive when it isn't really. I really just wanted to check that I hadn't laid some potential landmine in my code that would then blow up as development progressed. But as it seems to work for me, I'll continue...<br>

<br></div>Thanks again for all your work in developing Rcpp.<span class="HOEnZb"><font color="#888888"><br><br></font></span></div><span class="HOEnZb"><font color="#888888">Finlay<br></font></span></div><div class="HOEnZb">
<div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 29, 2013 at 1:19 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br>
On 29 April 2013 at 09:28, Finlay Scott wrote:<br>
| Hi,<br>
| I have some questions regarding extending 'as' and 'wrap' for custom classes.<br>
|<br>
| In my simple example I have declared a new class which has a single member.<br>
| This member is of type NumericVector, i.e. it is an Rcpp class. I have written<br>
| simple 'as' and 'wrap' extensions using non-intrusive extension. My simple<br>
| example compiles and appears to work as expected.<br>
|<br>
| My question is about the placement of #include <Rcpp.h>. In the Rcpp-extending<br>
| vignette, it says that for non-intrusive extension, the #include <Rcpp.h> must<br>
| appear after the specialisation otherwise the specialisation will not be seen<br>
| by Rcpp types. However, if I do this compilation fails because the<br>
| NumericVector member is not recognised by the compiler in the class definition.<br>
|<br>
| As an experiment, I moved the #include <Rcpp.h> to before the class definition.<br>
| My example then compiles and appears to run correctly. I was surprised because<br>
| what I did appears to contradict the advice given the vignette.<br>
<br>
</div>Headers got moved / Rcpp.h got reorganized after the vignette was written. So<br>
there is a chance something in there is outdated.<br>
<br>
I am not sure if your example below qualifies for our intrusive/non-intrusive<br>
schema: you are having your class in the same file, I think we consider that<br>
'intrusive' as you can modify your source.<br>
<br>
Main story: It works for you so ...<br>
<br>
Dirk<br>
<div><div><br>
| So my questions are:<br>
| Should I be surprised that this example works even though it appears I have<br>
| contradicted the advice in the vignette?<br>
| Although my example appears to work, because I have contradicted the advice in<br>
| the vignette, is there now something bad lurking in the application. i.e.<br>
| things appear fine with my simple example but will something blow up if I<br>
| continue to develop my class?<br>
|<br>
| My example cpp code is as follows:<br>
|<br>
| // File: DummyClass_with_Rcpp_member_example.cpp<br>
| #include <RcppCommon.h><br>
| // If #include <Rcpp.h> not placed here then compilation fails<br>
| #include <Rcpp.h><br>
|<br>
| class DummyClass {<br>
|     public:<br>
|         Rcpp::NumericVector data;<br>
| };<br>
|<br>
| namespace Rcpp {<br>
|     // non-intrusive extension via template specialisation<br>
|     template <> DummyClass as(SEXP dc_sexp);<br>
|     // non-intrusive extension via template specialisation<br>
|     template <> SEXP wrap(const DummyClass &flq);<br>
| }<br>
|<br>
| // Compilation fails if #include<Rcpp.h> placed here<br>
| //#include <Rcpp.h><br>
|<br>
| // define template specialisations for as and wrap<br>
| namespace Rcpp {<br>
|     template <> DummyClass as(SEXP dc_sexp) {<br>
|     S4 dc_s4 = Rcpp::as<S4>(dc_sexp);<br>
|     DummyClass dc;<br>
|     dc.data = dc_s4.slot(".Data");<br>
|         return dc;<br>
|     }<br>
|<br>
|     template <> SEXP wrap(const DummyClass &dc) {<br>
|     Rcpp::S4 dc_s4("DummyClass");<br>
|     dc_s4.slot(".Data") = dc.data;<br>
|         return Rcpp::wrap(dc_s4);<br>
|     }<br>
| }<br>
|<br>
| // [[Rcpp::export]]<br>
| DummyClass test_DC_as_wrap(DummyClass dc, double multiplier){<br>
|     DummyClass new_dc;<br>
|     new_dc.data = dc.data;<br>
|     new_dc.data(0) = new_dc.data(0) * multiplier;<br>
|     return new_dc;<br>
| }<br>
|<br>
| The R code that runs the example is:<br>
|<br>
| library(Rcpp)<br>
| sourceCpp("DummyClass_with_Rcpp_member_example.cpp")<br>
| setClass("DummyClass", representation(.Data = "numeric"))<br>
| dc <- new("DummyClass")<br>
| dc@.Data <- 1:10<br>
| test_DC_as_wrap(dc, 4)<br>
|<br>
| Yours<br>
|<br>
| Finlay<br>
|<br>
</div></div>| ----------------------------------------------------------------------<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<span><font color="#888888">--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div>
</div></div></div><br></div>