<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Hi Finlay,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">
If you name your include file the same name as your package then it will be included automatically in RcppExports.cpp. The convention at work here is that any custom as/wrap handlers should be accumulated (or referenced from) that single file. This mirrors the existing convention used by Rcpp, RcppArmadillo, RcppEigen, etc. to have a single global header file for their C++ API.</div>
<div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">J.J.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Wed, Apr 24, 2013 at 8:22 AM, Finlay Scott <span dir="ltr"><<a href="mailto:drfinlayscott@gmail.com" target="_blank">drfinlayscott@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Hi<br>First of all I want to say how impressed I am with Rcpp. I think it is going to be very useful for some of the packages I am developing. Thank you very much for developing it.<br><br>I have a question regarding writing custom as and wrap functions for my own classes. Following the example in:<br>

<br><a href="http://gallery.rcpp.org/articles/custom-as-and-wrap-example/" target="_blank">http://gallery.rcpp.org/articles/custom-as-and-wrap-example/</a><br><br>I can get my own minimal example to work with a very simple class, and using the sourceCpp() function.<br>

<br>The cpp code saved as a *.cpp file:<br><br>    #include <RcppCommon.h><br><br>    class DummyClass {<br>        public:<br>            double value;<br>    };<br><br>    namespace Rcpp {<br>        // non-intrusive extension via template specialisation<br>

        template <> DummyClass as(SEXP dt);<br>        // non-intrusive extension via template specialisation<br>        template <> SEXP wrap(const DummyClass &d);<br>    }<br><br>    #include <Rcpp.h><br>

<br>    // define template specialisations for as and wrap<br>    namespace Rcpp {<br>        template <> DummyClass as(SEXP dtsexp) {<br>        S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br>        DummyClass dc;<br>
        dc.value = dc_s4.slot("value");<br>
        return dc;<br>        }<br><br>        template <> SEXP wrap(const DummyClass &d) {<br>        Rcpp::S4 dc_s4("DummyClass");<br>        dc_s4.slot("value") = d.value;<br>        return Rcpp::wrap(dc_s4);<br>

        }<br>    }<br><br>    // [[Rcpp::export]]<br>    DummyClass test_as_wrap(DummyClass dc, double multiplier){<br>        DummyClass dc_out;<br>        dc_out.value = dc.value * multiplier;<br>        return dc_out;<br>

    }<br><br><br>And the following R code compiles and calls the function:<br><br>    library(Rcpp)<br>    sourceCpp("DummyClass_example.cpp")<br>    setClass("DummyClass", representation(value = "numeric"))<br>

    dc <- new("DummyClass")<br>    dc@value <- 23<br>    test_as_wrap(dc, 4)<br><br>This works just fine (like magic!) and the test_as_wrap() function is happily called from R and returns an object of type DummyClass. I want to use a similar approach in a package, so I made a minimal package using:<br>

<br>Rcpp.package.skeleton("asWrapExample",attributes=TRUE)<br><br>I then split my original cpp file above into header and source code files. In the /inst/include directory I placed a file 'DummyClass_example.h' which has:<br>

<br>    #include <RcppCommon.h><br><br>    class DummyClass {<br>        public:<br>            double value;<br>    };<br><br>    namespace Rcpp {<br>        // non-intrusive extension via template specialisation<br>

        template <> DummyClass as(SEXP dt);<br>        // non-intrusive extension via template specialisation<br>        template <> SEXP wrap(const DummyClass &d);<br>    }<br><br>In the /src directory I placed a file 'DummyClass_example.cpp' which has:<br>

<br>    #include "../inst/include/DummyClass_example.h"<br>    #include <Rcpp.h><br><br>    // define template specialisations for as and wrap<br>    namespace Rcpp {<br>        template <> DummyClass as(SEXP dtsexp) {<br>

        S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br>        DummyClass dc;<br>        dc.value = dc_s4.slot("value");<br>        return dc;<br>        }<br><br>        template <> SEXP wrap(const DummyClass &d) {<br>

        Rcpp::S4 dc_s4("DummyClass");<br>        dc_s4.slot("value") = d.value;<br>        return Rcpp::wrap(dc_s4);<br>        }<br>    }<br><br>    // [[Rcpp::export]]<br>    DummyClass test_as_wrap(DummyClass dc, double multiplier){<br>

        DummyClass dc_out;<br>        dc_out.value = dc.value * multiplier;<br>        return dc_out;<br>    }<br><br>When I try to compile the package I get this error message:<br><br>    RcppExports.cpp:9:1: error: 'DummyClass' does not name a type<br>

<br>This is probably caused by the RcppExports.cpp not having an #include for my DummyClass_example.h.<br>I understand the RcppExports.cpp file is automatically generated by the magic of Rcpp so there is no point in adding it there by hand.<br>

I've looked at the documentation but it is not clear to me how I can tell RcppExports to also include my header file (if this is the source of the problem).<br>Have I missed something in the documentation, or is there an example I can follow?<br>

Any help is appreciated.<br><br>Yours<span class="HOEnZb"><font color="#888888"><br><br>Finlay<br><br><br><br><br></font></span></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">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></blockquote></div><br></div>