Hello,

I've added a few things in modules to extend the possibilities of return types from a method that is exposed.

So, it is now possible for a method to return a pointer to an other (or the same) class. There is a small example below.

Here we expose two classes FOO and Bar. The interesting bits are the methods "clone" and "mult" in FOO and the method "make_foo" in Bar.

The return type is result<T> instead of T*, but the method should return a T*, i.e. the result template class looks like this:

template <typename T>
class result {
public:
    result( T* ptr_ ) : ptr(ptr_){}
    operator T*(){ return ptr ; }
private:
    T* ptr;
} ;

As usual when we come up with new features, please try them, and send feedback. Maybe you feel result is not a good name, maybe you have another idea, ...

Romain





require(inline)
require(Rcpp)

fx <- cxxfunction( , '', includes = '

class FOO{
public:
    FOO( double x_, double y_): x(x_), y(y_){}

    double x ;
    double y ;

    void move( double dx, double dy){
        x += dx ;
        y += dy ;
    }

    result<FOO> clone(){
        return new FOO(x,y) ;
    }

    result<FOO> mult(double z){
        return new FOO(z*x,z*y) ;
    }

} ;

class Bar {
public:
    Bar( double x_ ) : x(x_){}

    double x ;

    result<FOO> make_foo(){
        return new FOO(x,x) ;
    }
} ;


RCPP_MODULE(mod){

    class_<FOO>("FOO" )

        .constructor<double,double>()

        .field( "x", &FOO::x )
        .field( "y", &FOO::y )

        .method( "move", &FOO::move )

        .method( "clone", &FOO::clone )
        .method( "mult", &FOO::mult )
        ;

   class_<Bar>("Bar")
        .constructor<double>()
        .field( "x", &Bar::x )
        .method( "make_foo", &Bar::make_foo )
        ;
}


', plugin = "Rcpp" )
mod <- Module( "mod", getDynLib(fx),mustStart = TRUE )

# grab the exposed C++ class
FOO <- mod$FOO
Bar <- mod$Bar

f <- new( FOO, 2, 3 )
g <- f$clone()
h <- f$mult(2)

b <- new( Bar, 2 )
z <- b$make_foo()
z$x
z$y

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
http://romain-francois.com
|- http://bit.ly/kaSV6U : Stand up set at Up The Creek
|- http://bit.ly/hdKhCy : Rcpp article in JSS
`- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011


_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to