<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Dear all,<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>This is something I desire for a long time, but I do not have currently the time to do that.&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Let me summarize my desire:</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>1)&nbsp;<span class="Apple-tab-span" style="white-space:pre">        </span>Copy constructor</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>2)<span class="Apple-tab-span" style="white-space:pre">        </span>Rcpp module objects being wrapped so that they could become arguments in C++ functions.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-tab-span" style="white-space:pre">        </span>I</span>&nbsp;must apologize that sometimes ago, I did say that I want to help the effort, but it's time issue. Hope that I could get some time soon.</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Thank you very much.</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Maybe Dirk or Romain could give me some more specific hint on which part of the source code should I look at, to solve this problem... I could try to help...</div><div><br></div><div>Best regards,</div><div>Tama Ma</div><div><br></div><div><br></div><div><br><div><div>On Dec 21, 2011, at 11:00 PM, Richard Downe wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div bgcolor="#FFFFFF" text="#000000">
    A slightly hackish, if effective, workaround I've used in this
    instance is to have "as&lt;&gt;()" convert a string argument to a
    class return value, thereby avoiding the metaphysics of creating a
    c++ class in R.<br>
    <br>
    &nbsp;&nbsp;&nbsp; template &lt;&gt; fusionProject::TCFAFunctor *as( SEXP rs ) {<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using fusionProject::TCFAFunctor;<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!Rf_isString( rs ))<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw std::runtime_error("Invalid argument.&nbsp; String
    expected!");<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::string defName = as&lt;std::string&gt;( rs );<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCFAFunctor *retptr;<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (defName.compare("A")==0) <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retptr = new fusionProject::tcfaDefinitionA;<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // else if (defName.compare("B")==0)<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; retptr = new fusionProject::tcfaDefinitionB;<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (defName.compare("C")==0)<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retptr = new fusionProject::tcfaDefinitionC;<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw std::runtime_error("Unrecognized TCFA definition
    label.");<br>
    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return retptr;<br>
    &nbsp;&nbsp;&nbsp; }<br>
    <br>
    This works, with the caveat that the c++ code receiving the return
    value is responsible for deleting the pointer.&nbsp; Nonetheless,&nbsp; the
    syntax feels suitably "R-ish" on the R side, and I get what I wanted
    on the c++ side.<br>
    --rd<br>
    <br>
    <br>
    On 12/21/2011 03:55 PM, Yasir Suhail wrote:
    <blockquote cite="mid:CACvQymZgOf=Z9ru=BZZe=D68=arx3bygd0fgsqDf5_teRy6nVw@mail.gmail.com" type="cite">
      
      Hi
      <div><br>
      </div>
      <div>I looked up the previous thread <span style="background-color:rgb(255,255,255)">"</span><span style="background-color:rgb(255,255,255);font-family:Times">Creating
          pointers to objects and wrapping them"</span><font face="arial, helvetica, sans-serif"><span style="background-color:rgb(255,255,255)"> from&nbsp;</span><span style="background-color:rgb(255,255,255)"><a moz-do-not-send="true" href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-April/002170.html">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-April/002170.html</a>,
            and it seems that providing a specialization of the
            wrap&lt;&gt;() function can help us return classes. Perhaps
            similarly, one could provide a specialization of the
            as&lt;&gt;() function for arguments? While wrap is
            implemented with the Language("new" ... function, is there
            any example of an as&lt;&gt;() specialization for custom
            classes? The vignette (</span><a moz-do-not-send="true" href="http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pdf">http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pdf</a></font><span style="background-color:rgb(255,255,255);font-family:arial,helvetica,sans-serif">)
          shows a declaration for as, but no definitions.</span></div>
      <div>
        <div><font face="arial, helvetica, sans-serif"><br>
          </font></div>
        <div><font face="arial, helvetica, sans-serif">Thanks!</font></div>
        <div><font face="arial, helvetica, sans-serif"><br>
          </font></div>
        <div><font face="arial, helvetica, sans-serif">Yasir<br>
          </font>
          <div><font face="arial, helvetica, sans-serif"><span style="background-color:rgb(255,255,255)"><br>
              </span></font></div>
          <div><font face="arial, helvetica, sans-serif"><span style="background-color:rgb(255,255,255)"><br>
              </span><br>
            </font>
            <div class="gmail_quote">On Wed, Dec 21, 2011 at 5:45 AM, <span dir="ltr">&lt;<a moz-do-not-send="true" href="mailto:romain@r-enthusiasts.com">romain@r-enthusiasts.com</a>&gt;</span>
              wrote:<br>
              <blockquote class="gmail_quote" style="margin:0 0 0
                .8ex;border-left:1px #ccc solid;padding-left:1ex">
                <div bgcolor="#FFFFFF">
                  <div>Iirc, this is not implemented yet.&nbsp;</div>
                  <div><br>
                  </div>
                  <div>There is a chance we might add this feature in
                    the future.&nbsp;</div>
                  <div><br>
                  </div>
                  <div>Cheers,&nbsp;</div>
                  <div><br>
                  </div>
                  <div>Romain<br>
                    <br>
                    <div><br>
                    </div>
                  </div>
                  <div><br>
                    Le 21 déc. 2011 à 10:59, Yasir Suhail &lt;<a moz-do-not-send="true" href="mailto:yusuhail@gmail.com" target="_blank">yusuhail@gmail.com</a>&gt;
                    a écrit&nbsp;:<br>
                    <br>
                  </div>
                  <div>
                    <div class="h5">
                      <blockquote type="cite">
                        <div>I need to pass a class as an argument in a
                          function I am exposing to R. From the skeleton
                          package module, let us say that I have
                          <div><br>
                          </div>
                          <div>
                            <div>class World {</div>
                            <div>public:</div>
                            <div>&nbsp; &nbsp; World() : msg("hello"){}</div>
                            <div>&nbsp; &nbsp; void set(std::string msg) {
                              this-&gt;msg = msg; }</div>
                            <div>&nbsp; &nbsp; std::string greet() { return msg; }</div>
                            <div>&nbsp; &nbsp; void addMsg(World&amp; w2) {</div>
                            <div><span style="white-space:pre-wrap"> </span>msg
                              = msg + w2.msg;</div>
                            <div>&nbsp; &nbsp; }</div>
                            <div>private:</div>
                            <div>&nbsp; &nbsp; std::string msg;</div>
                            <div>};</div>
                            <div><br>
                            </div>
                          </div>
                          <div>However, this gives errors of the sort</div>
                          <div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:186:4:
                              error: invalid initialization of non-const
                              reference of type ‘World&amp;’ from an
                              rvalue of type ‘World’</div>
                            <div>In file included from
                              /usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:306:0,</div>
                            <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from
                              /usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27,</div>
                            <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from
                              rcpp_module.cpp:1:</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/traits/Exporter.h:
                              In constructor
                              ‘Rcpp::traits::Exporter&lt;T&gt;::Exporter(SEXPREC*)
                              [with T = World, SEXPREC* = SEXPREC*]’:</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:51:51:
                              &nbsp; instantiated from ‘T
                              Rcpp::internal::as(SEXPREC*,
                              Rcpp::traits::r_type_generic_tag) [with T
                              = World, SEXPREC* = SEXPREC*]’</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:75:89:
                              &nbsp; instantiated from ‘T Rcpp::as(SEXPREC*)
                              [with T = World, SEXPREC* = SEXPREC*]’</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:186:4:
                              &nbsp; instantiated from ‘SEXPREC*
                              Rcpp::CppMethod1&lt;Class, void,
                              U0&gt;::operator()(Class*, SEXPREC**)
                              [with Class = World, U0 = World&amp;,
                              SEXPREC* = SEXPREC*]’</div>
                            <div>rcpp_module.cpp:86:1: &nbsp; instantiated
                              from here</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/traits/Exporter.h:31:37:
                              error: no matching function for call to
                              ‘World::World(SEXPREC*&amp;)’</div>
                            <div>rcpp_module.cpp:30:5: note: candidates
                              are: World::World()</div>
                            <div>rcpp_module.cpp:28:13: note: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                              &nbsp; &nbsp; &nbsp; World::World(const World&amp;)</div>
                            <div>make: *** [rcpp_module.o] Error 1</div>
                          </div>
                          <div><br>
                          </div>
                          <div>Looking at&nbsp;<a moz-do-not-send="true" href="http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pdf" target="_blank">http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pdf</a>,
                            it seems that I can extend Rcpp::as to
                            account for my classes, but I still have two
                            questions:</div>
                          <div>1. How do I define the constructor
                            World(SEXP)? Is there a working example
                            somewhere for a user defined C++ class?</div>
                          <div>2. Can I also use pointers and
                            pass-by-reference as these are more often
                            used in real C++ code.</div>
                          <div><br>
                          </div>
                          <div>In short, I'd appreciate a way to pass my
                            C++ classes (as the class and also by ref.
                            or pointer) that have been exposed to R as
                            arguments to exposed functions. How do I
                            achieve that?</div>
                          <div><br>
                          </div>
                          <div>Thanks!</div>
                        </div>
                      </blockquote>
                    </div>
                  </div>
                  <blockquote type="cite">
                    <div><span>_______________________________________________</span><br>
                      <span>Rcpp-devel mailing list</span><br>
                      <span><a moz-do-not-send="true" href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a></span><br>
                      <span><a moz-do-not-send="true" 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></span></div>
                  </blockquote>
                </div>
              </blockquote>
            </div>
            <br>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Rcpp-devel mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></pre>
    </blockquote>
    <br>
  </div>

_______________________________________________<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>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div><span class="Apple-tab-span" style="white-space: pre; "><br class="Apple-interchange-newline"><span class="Apple-style-span" style="white-space: normal; ">Best regards,</span></span></div><div>Tama Ma&nbsp;</div><div>(+41 78 640 50 15)</div><div><br></div><div><a href="mailto:pingnang@phys.ethz.ch">pingnang@phys.ethz.ch</a></div><div><a href="http://www.phys.ethz.ch/~pingnang">www.phys.ethz.ch/~pingnang</a></div><div><br></div><div>HIT K 31.3</div><div>Institut für Theoretische Physik</div><div>Wolfgang-Pauli-Strasse 27</div><div>ETH Hönggerberg</div><div>8093 Zürich</div><div>Switzerland</div></span></span>
</div>
<br></div></body></html>