Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for .factory #937

Closed
rstub opened this issue Feb 7, 2019 · 1 comment
Closed

Documentation for .factory #937

rstub opened this issue Feb 7, 2019 · 1 comment

Comments

@rstub
Copy link
Contributor

rstub commented Feb 7, 2019

As far as I can tell the .factory method for Rcpp Modules is not documented yet. Recently I wrote the following example for a SO answer:

#include <Rcpp.h>
using namespace Rcpp;

// abstract class
class Base {
  public:
    virtual ~Base() {}
    virtual std::string name() const = 0;
};

// derived class
class Derived1: public Base {
  public:
    Derived1() : Base() {}
    virtual std::string name() const { return "Derived1"; }
};

// derived class
class Derived2: public Base {
  public:
    Derived2() : Base() {}
    virtual std::string name() const { return "Derived2"; }
};

Base *newBase( const std::string &name ) {
  if ( name == "d1" ){
    return new Derived1 ;
  } else if ( name == "d2" ){
    return new Derived2 ;
  } else {
    return 0 ;
  }
}

RCPP_MODULE(mod) {
  Rcpp::class_< Base >("Base")
    .factory<const std::string&>(newBase)
    .method("name", &Base::name)
  ;
}


/*** R
(dv1 <- new(Base, "d1"))
dv1$name()
(dv2 <- new(Base, "d2"))
dv2$name()
*/

Would you be interested in a PR that adds something along these lines to the Rcpp modules vignette?

@eddelbuettel
Copy link
Member

That sounds like a useful addition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants