[Rcpp-devel] check validity of object created from Rcpp module

Jiqiang Guo guojq28 at gmail.com
Tue Jun 26 23:21:31 CEST 2012


Dear List,

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.

In detail, we know that we could not save and load objects created from
Rcpp's module [section 5 of
http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-modules.pdf].
 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.

An illustrative example:

require("Rcpp")
require("inline")

inc <- '
class Foo {
private:
  int x_;
public:
  Foo(int x) : x_(x) { }
  int getx() const { return x_;}
};

RCPP_MODULE(foo) {
  class_<Foo>("Foo")
  .constructor<int>()
  .method("getx", &Foo::getx)
  ;
}

fx <- cxxfunction(signature(), "", include = inc, plugin = "Rcpp", verbose
= FALSE)
m <- Module("foo", getDynLib(fx))
cppo  = new(m$Foo, 5)

print(cppo$getx())
save("cppo", file = 'cppo.RData')

rm(list = ls())
load("cppo.RData")
print(cppo$getx())

For the above code, we know the last line would not work.  If we have a
function to test the validity of cppo,then if not valid, we would not call
cppo's function and are able to tell users that cppo is not valid any more.

Best,
Jiqiang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120626/743acfde/attachment.html>


More information about the Rcpp-devel mailing list