<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<>()" 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">
<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 </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>
</body>
</html>