Dear List,<div><br></div><div>I am wondering if there is a way to check whether an object created from Rcpp's module is valid or not especially for objects loaded in another session. </div><div><br></div><div>In detail, we know that we could not save and load objects created from Rcpp's module [section 5 of <a href="http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-modules.pdf">http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-modules.pdf</a>]. But that does not prevent users doing that. So if an object created from Rcpp's module is loaded in another session, we would end up with errors or even segfault. It would be nice we have a method to check if it is valid or not so that when we are writing a package, we could do a check before calling those methods of that object. </div>
<div><br></div><div>An illustrative example:</div><div><br></div><div><div><font face="courier new, monospace">require("Rcpp")</font></div><div><font face="courier new, monospace">require("inline")</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">inc <- '</font></div><div><font face="courier new, monospace">class Foo {</font></div><div><font face="courier new, monospace">private:</font></div>
<div><font face="courier new, monospace"> int x_;</font></div><div><font face="courier new, monospace">public:</font></div><div><font face="courier new, monospace"> Foo(int x) : x_(x) { }</font></div><div><font face="courier new, monospace"> int getx() const { return x_;}</font></div>
<div><font face="courier new, monospace">}; </font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">RCPP_MODULE(foo) {</font></div><div><font face="courier new, monospace"> class_<Foo>("Foo")</font></div>
<div><font face="courier new, monospace"> .constructor<int>() </font></div><div><font face="courier new, monospace"> .method("getx", &Foo::getx) </font></div><div><font face="courier new, monospace"> ;</font></div>
<div><font face="courier new, monospace">}</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">fx <- cxxfunction(signature(), "", include = inc, plugin = "Rcpp", verbose = FALSE)</font></div>
<div><font face="courier new, monospace">m <- Module("foo", getDynLib(fx))</font></div><div><font face="courier new, monospace">cppo = new(m$Foo, 5)</font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace">print(cppo$getx())</font></div><div><font face="courier new, monospace">save("cppo", file = 'cppo.RData')</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">rm(list = ls())</font></div><div><font face="courier new, monospace">load("cppo.RData")</font></div><div><font face="courier new, monospace">print(cppo$getx())</font></div>
</div><div><font face="courier new, monospace"><br></font></div>For the above code, we know the last line would not work. If we have a function to test the validity of <font face="courier new, monospace">cppo,</font>then if not valid, we would not call <font face="courier new, monospace">cppo</font>'s function and are able to tell users that <font face="courier new, monospace">cppo </font>is not valid any more. <div>
<font face="courier new, monospace"><br></font></div>Best,<br>Jiqiang