<div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></span></div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">dear readers: I am trying to learn how to interface a simple C++ class into the underbelly of R. I don't plan to use this regularly---just for one or two applications. I read the introduction and faq vignettes and hadley's tutorial. alas, I am mixing and matching badly. I also don't have the full template programming background, which dates me, and the examples are quickly going down roads that are not illustrative of where I want to go.</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">I would like to get a deliberately simple working example of a plain C++ class. if no good example exists, here is one I made up that should be fairly canonical. the R code should be something like</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><font face="courier new, monospace">library(<span style="background-color:rgb(255,255,204)">Rcpp</span>)<br>
sourceCpp("silly.cpp")<br>test <- function() {<br> silly.object <- Silly(100) ## initializer<br> silly.object <- update( silly.object, 10 ) ## updater<br> show( silly.object ) ## results<br> silly.object <- update( silly.object, 20 )<br>
show( silly.object )<br> ## don't leak but call destroy on the silly example<br>}<br>test()</font></blockquote><br>the C++ code in silly.cpp should wrap/glue the following:<br><br></div><blockquote style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255);margin:0px 0px 0px 40px;padding:0px;border:none">
<div><font face="courier new, monospace">struct Silly {</font></div><div><font face="courier new, monospace"> double *v; int k; // shows internal memory allocation</font></div><div><font face="courier new, monospace">public:</font></div>
<div><font face="courier new, monospace"> Silly(int i) { k=i; v=new double[k]; }</font></div><div><font face="courier new, monospace"> ~Silly() { delete [] v; }</font></div><div><font face="courier new, monospace"> void update( double x1 ) { for (int i=0; i<k; ++i) v[i]= v[i]+x1*i; }</font></div>
<div><font face="courier new, monospace"> int show() { for (int i=0; i<k; ++i) printf("i[%d]=%lf\n", i, v[i]); return k; }</font></div><div><font face="courier new, monospace">}</font></div></blockquote><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
<br>that's it. here, I know all the types. I think I don't need SEXPs (or maybe I do?), because I know I want to deal with numerics. silly.object is my class object (or pointer to one Silly struct) that I want to carry along in R, but not have R know more about. the argument to update() is known to be a double (or we could make it a NumericVector) for simplicity sake.<br>
<br>if this sort of example is easier done with C and structs instead of C++ and classes, I would be ok with this, too. If the example were embedded in a cppFunction() call, even better. I prefer as little ceremony (glue) as possible/necessary.</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><br></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
could someone please point me to a standalone example that is similar? once I have this, experimenting should become a whole lot easier. (PS: This might make a nice opening of a tutorial, or a nice piece for the R tutorial.)<br>
<br>advice appreciated.<br><br>/iaw<br>----<br>Ivo Welch (<a href="mailto:ivo.welch@gmail.com" target="_blank" style="color:rgb(17,85,204)">ivo.welch@gmail.com</a>)</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
----</div><br clear="all"><div><div>----</div><div>Ivo Welch (<a href="mailto:ivo.welch@gmail.com" target="_blank">ivo.welch@gmail.com</a>)</div><div><a href="http://www.ivo-welch.info/" target="_blank">http://www.ivo-welch.info/</a></div>
<div>J. Fred Weston Professor of Finance</div><div>Anderson School at UCLA, C519</div><div>Director, UCLA Anderson Fink Center for Finance and Investments</div><div>Free Finance Textbook, <a href="http://book.ivo-welch.info/" target="_blank">http://book.ivo-welch.info/</a></div>
<div>Editor, Critical Finance Review, <a href="http://www.critical-finance-review.org/" target="_blank">http://www.critical-finance-review.org/</a></div><div> </div></div>