What is the appropriate place to define generic methods on classes exposed using Rcpp modules? More concretely, suppose the following source file is part of an Rcpp package. I'd like to use the + operator with the toy class 'Double':
#include <Rcpp.h>
class Double {
public:
Double(double d);
double get() const;
private:
...
};
RCPP_MODULE(my_module) {
using namespace Rcpp;
class_<Double>("my.double")
.constructor<double>()
.property("value", &Double::get);
}
The following R code appears to give the desired result:
.onLoad <- function(libname, pkgname) {
loadRcppModules()
}
.onAttach <- function(libname, pkgname) {
setMethod("+", signature(e1=my.double, e2=my.double), function(e1, e2) {
new(my.double, e1$value + e2$value)
}, where=.GlobalEnv)
}
I am, however, interested in learning the right way to solve this problem.
I am doing exactly that in the RcppBDT package. I also find it tedious -- for many types, and many ops this gets repetitive quick -- but do not know of a better way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With