Skip to content

Documentation for .factory #937

@rstub

Description

@rstub

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions