<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body 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">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      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&eacute;c. 2011 &agrave; 10:59, Yasir Suhail &lt;<a
                      moz-do-not-send="true"
                      href="mailto:yusuhail@gmail.com" target="_blank">yusuhail@gmail.com</a>&gt;
                    a &eacute;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 &#8216;World&amp;&#8217; from an
                              rvalue of type &#8216;World&#8217;</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
                              &#8216;Rcpp::traits::Exporter&lt;T&gt;::Exporter(SEXPREC*)
                              [with T = World, SEXPREC* = SEXPREC*]&#8217;:</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:51:51:
                              &nbsp; instantiated from &#8216;T
                              Rcpp::internal::as(SEXPREC*,
                              Rcpp::traits::r_type_generic_tag) [with T
                              = World, SEXPREC* = SEXPREC*]&#8217;</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:75:89:
                              &nbsp; instantiated from &#8216;T Rcpp::as(SEXPREC*)
                              [with T = World, SEXPREC* = SEXPREC*]&#8217;</div>
                            <div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_CppMethod.h:186:4:
                              &nbsp; instantiated from &#8216;SEXPREC*
                              Rcpp::CppMethod1&lt;Class, void,
                              U0&gt;::operator()(Class*, SEXPREC**)
                              [with Class = World, U0 = World&amp;,
                              SEXPREC* = SEXPREC*]&#8217;</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
                              &#8216;World::World(SEXPREC*&amp;)&#8217;</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>
  </body>
</html>