[Rcpp-devel] New function for exposing C++ classes
John Chambers
jmc at r-project.org
Wed Oct 9 19:26:25 CEST 2013
The function exposeClass() has been added to Rcpp. The idea is to call it to create C++ and R files in a package source in order to export a C++ class, given basic information about the class.
The function writes the Module source in C++ and a corresponding call to setRcppClass in R.
Like compileAttributes(), it would be called with the working directory set to the top source directory of a package. It doesn't do any parsing, so needs more information than compileAttributes().
For direct fields and methods, just the names. For constructors and inherited fields and methods, type information as well. Optional arguments allow for read-only fields and renaming of members.
The function has also been added to the master git source for Rcpp11.
The example from the documentation is included below.
John
---------------
### Given the following C++ class, defined in file PopBD.h,
### the call to exposeClass() shown below will write a file
### src/PopBDModule.cpp containing a corresponding module definition.
### class PopBD {
### public:
### PopBD(void);
### PopBD(NumericVector initBirth, NumericVector initDeath);
###
### std::vector<double> birth;
### std::vector<double> death;
### std::vector<int> lineage;
### std::vector<long> size;
### void evolve(int);
###
### };
### A file R/PopBDClass.R will be written containing the one line:
### PopBD <- setRcppClass("PopBD")
###
### The call below exposes the lineage and size fields, read-only,
### and the evolve() method.
exposeClass("PopBD",
constructors =
list("", c("NumericVector", "NumericVector")),
fields = c("lineage", "size"),
methods = "evolve",
header = '#include "PopBD.h"',
readOnly = c("lineage", "size"))
### Example with inheritance: the class PopCount inherits from
### the previous class, and adds a method table(). It has the same
### constructors as the previous class.
### To expose the table() method, and the inherited evolve() method and size field:
exposeClass("PopCount",
constructors =
list("", c("NumericVector", "NumericVector")),
fields = c(size = "std::vector<long>"),
methods = list("table", evolve = c("void", "int")),
header = '#include "PopCount.h"',
readOnly = "size")
}
More information about the Rcpp-devel
mailing list