<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. </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) <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> 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<>()" convert a string argument to a
class return value, thereby avoiding the metaphysics of creating a
c++ class in R.<br>
<br>
template <> fusionProject::TCFAFunctor *as( SEXP rs ) {<br>
using fusionProject::TCFAFunctor;<br>
<br>
if (!Rf_isString( rs ))<br>
throw std::runtime_error("Invalid argument. String
expected!");<br>
<br>
std::string defName = as<std::string>( rs );<br>
TCFAFunctor *retptr;<br>
<br>
if (defName.compare("A")==0) <br>
retptr = new fusionProject::tcfaDefinitionA;<br>
// else if (defName.compare("B")==0)<br>
// retptr = new fusionProject::tcfaDefinitionB;<br>
else if (defName.compare("C")==0)<br>
retptr = new fusionProject::tcfaDefinitionC;<br>
else<br>
throw std::runtime_error("Unrecognized TCFA definition
label.");<br>
<br>
return retptr;<br>
}<br>
<br>
This works, with the caveat that the c++ code receiving the return
value is responsible for deleting the pointer. Nonetheless, 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 </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<>() function can help us return classes. Perhaps
similarly, one could provide a specialization of the
as<>() function for arguments? While wrap is
implemented with the Language("new" ... function, is there
any example of an as<>() 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"><<a moz-do-not-send="true" href="mailto:romain@r-enthusiasts.com">romain@r-enthusiasts.com</a>></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. </div>
<div><br>
</div>
<div>There is a chance we might add this feature in
the future. </div>
<div><br>
</div>
<div>Cheers, </div>
<div><br>
</div>
<div>Romain<br>
<br>
<div><br>
</div>
</div>
<div><br>
Le 21 déc. 2011 à 10:59, Yasir Suhail <<a moz-do-not-send="true" href="mailto:yusuhail@gmail.com" target="_blank">yusuhail@gmail.com</a>>
a écrit :<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> World() : msg("hello"){}</div>
<div> void set(std::string msg) {
this->msg = msg; }</div>
<div> std::string greet() { return msg; }</div>
<div> void addMsg(World& w2) {</div>
<div><span style="white-space:pre-wrap"> </span>msg
= msg + w2.msg;</div>
<div> }</div>
<div>private:</div>
<div> 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&’ 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> from
/usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27,</div>
<div> from
rcpp_module.cpp:1:</div>
<div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/traits/Exporter.h:
In constructor
‘Rcpp::traits::Exporter<T>::Exporter(SEXPREC*)
[with T = World, SEXPREC* = SEXPREC*]’:</div>
<div>/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:51:51:
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:
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:
instantiated from ‘SEXPREC*
Rcpp::CppMethod1<Class, void,
U0>::operator()(Class*, SEXPREC**)
[with Class = World, U0 = World&,
SEXPREC* = SEXPREC*]’</div>
<div>rcpp_module.cpp:86:1: 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*&)’</div>
<div>rcpp_module.cpp:30:5: note: candidates
are: World::World()</div>
<div>rcpp_module.cpp:28:13: note:
World::World(const World&)</div>
<div>make: *** [rcpp_module.o] Error 1</div>
</div>
<div><br>
</div>
<div>Looking at <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 </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>