[Rcpp-devel] RcppModules with templated class
Dr Gregory Jefferis
jefferis at mrc-lmb.cam.ac.uk
Mon Sep 1 23:29:20 CEST 2014
Dear Dirk,
On 1 Sep 2014, at 21:19, Dirk Eddelbuettel wrote:
> These very useful RCPP_EXPOSED_CLASS* macros are a tad
> underdocumented.
>
> Greg, would you be interested in writing up a simple Rcpp Gallery post
> on that?
> I would be happy to help if you'd prefer that.
Many thanks for the invitation, which I appreciate. Ordinarily yes, but
I am afraid that I will not have the chance to do this properly myself
for some time. So if you think it would be useful (and it's true, it
would have been) and you have a little time, then please feel to use
this package as an example. I have now merged the full templated class
into master.
https://github.com/jefferis/nabor/
so
https://github.com/jefferis/nabor/blob/master/src/WKNN.h
https://github.com/jefferis/nabor/blob/master/src/WKNN.cpp
https://github.com/jefferis/nabor/blob/master/R/WKNNClasses.R
provide the definition and implementation of the C++ templated class,
its instantiation in two real C++ classes, and their exposure to R.
Finding nearest neighbour points is probably a reasonably interesting
problem to motivate this for an article.
I think the minimal requirement is (assuming complete definition in a
single .h file):
---MyClass.h---
#include <Rcpp.h>
#include <RcppEigen.h>
template<typename T>
struct MyClass {
public:
MyClass(const Eigen::Map<Eigen::MatrixXd> data) {
// constructor body ...
}
// other methods ...
private:
Eigen::Matrix<T, Dynamic, Dynamic> data_pts;
}
typedef MyClass<double> MyClassD;
typedef MyClass<float> MyClassF;
---MyClass.cpp---
#include <Rcpp.h>
#include <RcppEigen.h>
#include "MyClass.h"
// [[Rcpp::depends(RcppEigen)]]
using namespace Rcpp;
RCPP_EXPOSED_CLASS_NODECL(MyClassD)
RCPP_MODULE(class_MyClassD) {
class_<MyClassD>( "MyClassD" )
.constructor<Eigen::Map<Eigen::MatrixXd> >()
// other methods ...
;
}
RCPP_EXPOSED_CLASS_NODECL(MyClassD)
RCPP_MODULE(class_MyClassF) {
class_<MyClassF>( "MyClassF" )
.constructor<Eigen::Map<Eigen::MatrixXd> >()
// other methods ...
;
}
Best wishes,
Greg.
--
Gregory Jefferis, PhD Tel: 01223 267048
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 OQH, UK
http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://flybrain.stanford.edu
More information about the Rcpp-devel
mailing list