Excellent!
Thanks Romain. I'll get the new version and let you know how I go with both the
toy and the real code.
Thanks Romain (and also Dirk for help),
The following R + Rcpp works with the latest package build from R.Forge (the
complete code is lower down). My real code (far more complex) has also
compiled, pending correction of various other mistakes it should work.
Cheers,
Luke.
> require(Rcpp)
Loading required package: Rcpp
> sourceCpp("test.cpp")
> # Reference Class of B
> B_R <- setRefClass( "B_R",
+ fields = list(id="numeric")
+ )
> b1<-B_R$new(id=1)
> b2<-B_R$new(id=2)
> a<-A$new(vec_of_T=list(b1,b2))
> a$show()
vec_of_T:
Length: 2
Type T: 1B
id: 1
id: 2
> a2<-A$new(vec_to_T=list())
> a2$show()
vec_of_T:
Length: 20
Type T: 1B
id: 0
id: 1
id: 2
id: 3
id: 4
...etc..
test.cpp:
#include <RcppCommon.h>
class B {
public:
int id;
B (SEXP b);
B (int id=-1);
void show();
};
/*** R
# Reference Class of B
B_R <- setRefClass( "B_R",
fields = list(id="numeric")
)
*/
template <class T> class A {
public:
std::vector<T> vec_of_T;
A( std::vector<T> in_vec );
void show();
};
#include <Rcpp.h>
B::B(SEXP b){
Rcpp::Reference in_b(b);
id=in_b.field("id");
}
B::B(int id){
this->id=id;
}
void B::show(){
Rcpp::Rcout << "id: " << id << "\n";
}
template <class T> A<T>::A (std::vector<T> in_vec){
if (in_vec.empty()) {
this->vec_of_T.reserve(20);
for (int i=0; i<20; i++){
this->vec_of_T.push_back(T(i));
}
} else {
this->vec_of_T=in_vec;
}
}
template <class T> void A<T>::show(){
Rcpp::Rcout << "vec_of_T:\n";
Rcpp::Rcout << " Length: " << vec_of_T.size() << "\n";
if(!vec_of_T.empty()){
Rcpp::Rcout << " Type T: " << typeid(vec_of_T[0]).name() << "\n";
for( unsigned int i=0; i<vec_of_T.size(); i++) vec_of_T[i].show();
}
}
using namespace Rcpp;
RCPP_MODULE(testing) {
class_< A<B> >("A")
.constructor< std::vector<B> >()
.method("show", &A<B>::show)
;
}
_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel