Skip to content

[Rcpp-devel] Create an instance of a reference class for a C++ class exported as a module from within C++?

3 messages · Douglas Bates, Romain Francois

#
Sorry for the long subject line but I hope it describes what I want to
do.  I have a C++ class, dgTMatrix, that is exported in a module
definition.

RCPP_MODULE(RcppSp) {
...
    class_<dgCMatrix>("dgCMatrix")
    ...
    ;
}

Many of the methods for this class return other members of this class.
 Can I create a new instance of the reference class in R from within
the module?  I'm sure it is possible, I'm just wondering if there is
an easy way to do this without duplicating large chunks of the code in
Rcpp/src/Module.cpp.  Can i somehow clone the SEXP representing the
current instance then swap the external pointer to the C++ object?
#
This is something we have to add at some point. 

At the moment the only way i see is to make an R call to "new" and construct the object this way...



Le 2 juin 2011 ? 21:31, Douglas Bates <bates at stat.wisc.edu> a ?crit :
4 days later
#
Le 02/06/11 22:31, Douglas Bates a ?crit :
As part of the changes I've made today [see the post "returning pointers 
in module methods" ], there is this function you might be interested in 
(in Module.h):

namespace internal {
template <typename Class>
SEXP make_new_object( Class* ptr ){
     Rcpp::XPtr<Class> xp( ptr, true ) ;
	Function maker = Environment::Rcpp_namespace()[ "cpp_object_maker"] ;
	return maker( typeid(Class).name() , xp ) ;
}
}

Maybe I should make it more public (i.e not have it in internal).


Anyway, if you give this function a pointer to an instance of a class 
that has been exposed through modules, it will create the associated R 
ref class object.


Romain